-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflask.py
More file actions
68 lines (53 loc) · 1.64 KB
/
Copy pathflask.py
File metadata and controls
68 lines (53 loc) · 1.64 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
#flask Methods
from flask import Flask, request
import psycopg2
# Connect to the database
conn = psycopg2.connect(database="zenith_ftc", user="user", password="password", host="localhost", port="5432")
# Create a cursor
cur = conn.cursor()
app = Flask(__name__)
# Route to create a new part
@app.route('/parts', methods=['POST'])
def create_part():
# Get the data from the request body
data = request.get_json()
name = data['name']
description = data['description']
quantity = data['quantity']
# Call the create_part function
create_part(name, description, quantity)
# Return a success message
return "Part successfully created"
# Route to read a part
@app.route('/parts/<name>', methods=['GET'])
def read_part(name):
# Call the read_part function
part = read_part(name)
# Return the part data
return {
"id": part[0],
"name": part[1],
"description": part[2],
"quantity": part[3]
}
# Route to update a part
@app.route('/parts/<id>', methods=['PUT'])
def update_part(id):
# Get the data from the request body
data = request.get_json()
name = data['name']
description = data['description']
quantity = data['quantity']
# Call the update_part function
update_part(id, name, description, quantity)
# Return a success message
return "Part successfully updated"
# Route to delete a part
@app.route('/parts/<id>', methods=['DELETE'])
def delete_part(id):
# Call the delete_part function
delete_part(id)
# Return a success message
return "Part successfully deleted"
if __name__ == '__main__':
app.run()