-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstormiServ.py
More file actions
68 lines (54 loc) · 1.95 KB
/
stormiServ.py
File metadata and controls
68 lines (54 loc) · 1.95 KB
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
from flask import Flask, request, jsonify
import json
import os
app = Flask(__name__)
dataBase = "users2.json"
#Check if the json file exits, and loads data to the file.
#If it doesnt, then it creates the file
def loadData():
if os.path.exists(dataBase):
with open(dataBase, "r") as file:
return json.load(file)
return {"clients": [], "photoshoots": []}
#writes back to the data file using json.dump
def saveInfo(data):
with open(dataBase, "w") as file:
json.dump(data, file, indent=4)
#Defines POST endpoint (/clients)
#Extracts data
@app.route("/clients", methods=["POST"])
def addClient():
data = loadData()
newClient = request.json
#if CANCEL is input, then it will display client was CXL, 400
if not newClient or newClient.get("name") == "CANCEL":
return jsonify({"message": "Client Canceled"}), 400
#201, Success, add client
data["clients"].append(newClient)
saveInfo(data)
return jsonify({"message": "Client added successfully"}), 201
#Add new ps post /phootoshoots, similar to clients
@app.route("/photoshoots", methods=["POST"])
def addPS():
data = loadData()
newPS = request.json
#similar thing here, if user inputs CANCEL, then it cancels
if not newPS or newPS.get("client") == "CANCEL":
return jsonify({"message": "Photoshoot was Canceled"}), 400
#save here
data["photoshoots"].append(newPS)
saveInfo(data)
return jsonify({"message": "Photoshoot scheduled successfully"}), 201
# View all scheduled photoshoots (GET /appts)
@app.route("/appts", methods=["GET"])
def viewAppt():
data = loadData()
numAppt = len(data["photoshoots"])
return jsonify({
"total appts": numAppt,
"appointments": data["photoshoots"]
})
#make sure code runs only when executed DIRECTLY
#True enables a restart
if __name__ == "__main__":
app.run(debug=True, port=5000)