-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
120 lines (103 loc) · 3.59 KB
/
app.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
119
import pymongo
from flask import Flask, Response, request
import json
from bson.objectid import ObjectId
import os
# mongo uri for env variable
try:
mongo_uri = os.environ["MONGO_URI"]
mongo = pymongo.MongoClient(mongo_uri, serverSelectionTimeoutMS=1000)
except:
print("Error - connection error to db")
app = Flask(__name__)
try:
mongo = pymongo.MongoClient(host="localhost", port=27017, serverSelectionTimeoutMS=1000)
db = mongo.user_db
mongo.server_info() # exception trigger
except:
print("Error - connection error to db")
@app.route("/users",methods=["POST"])
def create_user():
try:
user = {"name": request.form["name"],
"email":request.form["email"],
"password":request.form["password"]}
dbResponse = db.users.insert_one(user)
# for attr in dir(dbResponse):
# print(attr)
return Response(
response=json.dumps({"message":"user created", "id":f"{dbResponse.inserted_id}"}),
status=200,
mimetype="application/json"
)
except Exception as ex:
print(ex)
@app.route("/users",methods=["GET"])
def get_user():
try:
data = list(db.users.find())
for user in data:
user["_id"]=str(user["_id"])
return Response(
response=json.dumps(data),
status=500,
mimetype="application/json"
)
except Exception as ex:
print(ex)
return Response(
response=json.dumps({"message": "cannot read users"}),
status=500,
mimetype="application/json"
)
@app.route("/users/<id>", methods=["PATCH"])
def update_user(id):
try:
dbResponse = db.users.update_many({"_id":ObjectId(id)},
{"$set":{"name":request.form["name"],
"email":request.form["email"],
"password": request.form["password"]
}})
for attr in dir(dbResponse):
print(f"{attr}") # used to see the response content
if dbResponse.modified_count == 1:
return Response(
response=json.dumps({"message": "Details updated"}),
status=200,
mimetype="application/json"
)
else:
return Response(
response=json.dumps({"message": "Details entered were the same as before"}),
status=200,
mimetype="application/json"
)
except Exception as ex:
return Response(
response=json.dumps({"message": "Failed to update"}),
status=500,
mimetype="application/json"
)
@app.route("/users/<id>", methods=["DELETE"])
def delete_user(id):
try:
dbResponse = db.users.delete_one({"_id":ObjectId(id)})
if dbResponse.deleted_count == 1:
return Response(
response=json.dumps({"message": "User successfully deleted", "id":f"{id}"}),
status=200,
mimetype="application/json"
)
return Response(
response=json.dumps({"message": "invalid id or the user does not exist"}),
status=200,
mimetype="application/json"
)
except Exception as ex:
return Response(
response=json.dumps({"message": "Failed to delete the user"}),
status=500,
mimetype="application/json"
)
if __name__ == "__main__":
app.run(port=80, debug=True)