-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp_postgres.py
118 lines (94 loc) · 3.55 KB
/
app_postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import os
import psycopg2
from flask import Flask, jsonify, request
DATABASE = "dbname=postgres user=otel.sample host=localhost port=5432"
def get_db():
conn = psycopg2.connect(DATABASE)
return conn
def setup_database():
with get_db() as conn:
with conn.cursor() as cur:
cur.execute(
"""
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL UNIQUE
);
"""
)
def create_app():
app = Flask(__name__)
setup_database()
@app.route("/users", methods=["GET"])
def get_users():
with get_db() as conn:
cur = conn.cursor()
cur.execute("SELECT id, name, email FROM users")
users = cur.fetchall()
return jsonify(users)
@app.route("/user/<int:user_id>", methods=["GET"])
def get_user(user_id):
with get_db() as conn:
cur = conn.cursor()
cur.execute("SELECT id, name, email FROM users WHERE id = %s", (user_id,))
user = cur.fetchone()
if user:
return jsonify(user)
return jsonify({"error": "User not found"}), 404
@app.route("/user/name/<string:name>", methods=["GET"])
def get_user_by_name(name):
with get_db() as conn:
cur = conn.cursor()
cur.execute("SELECT id, name, email FROM users WHERE name = %s", (name,))
user = cur.fetchone()
if user:
return jsonify(user)
return jsonify({"error": "User not found"}), 404
@app.route("/user", methods=["POST"])
def create_user():
if not request.json:
return jsonify({"error": "JSON payload expected"}), 400
name = request.json.get("name")
email = request.json.get("email")
with get_db() as conn:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO users (name, email) VALUES (%s, %s) RETURNING id;",
(name, email),
)
user_id = cur.fetchone()[0]
conn.commit()
return (
jsonify({"message": "User created successfully", "user_id": user_id}),
201,
)
@app.route("/user/<int:user_id>", methods=["PUT"])
def update_user(user_id):
if not request.json:
return jsonify({"error": "JSON payload expected"}), 400
name = request.json.get("name")
email = request.json.get("email")
with get_db() as conn:
with conn.cursor() as cur:
cur.execute(
"UPDATE users SET name=%s, email=%s WHERE id=%s",
(name, email, user_id),
)
if cur.rowcount == 0:
return jsonify({"error": "User not found"}), 404
conn.commit()
return jsonify({"message": "User updated successfully"}), 200
@app.route("/user/<int:user_id>", methods=["DELETE"])
def delete_user(user_id):
with get_db() as conn:
with conn.cursor() as cur:
cur.execute("DELETE FROM users WHERE id=%s", (user_id,))
if cur.rowcount == 0:
return jsonify({"error": "User not found"}), 404
conn.commit()
return jsonify({"message": "User deleted successfully"}), 200
return app
if __name__ == "__main__":
app = create_app()
app.run(debug=False, host="0.0.0.0", port=os.environ.get("SERVER_PORT", 9090))