forked from InoWasTaken/ClickNCollect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
35 lines (31 loc) · 1.41 KB
/
api.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
from flask import Flask, request, jsonify
def api(app, db):
@app.route("/<product_type>", methods=['GET'])
def get_products(product_type):
if product_type != 'snacks' and product_type != 'drinks':
return "Product type not found", 404
cursor = db.execute(
f"SELECT name, in_stock, rowid FROM {product_type}")
products = cursor.fetchall()
return jsonify(products)
@app.route("/<product_type>/<id>", methods=['PATCH'])
def update_products(product_type, id):
if product_type != 'snacks' and product_type != 'drinks':
return "Product type not found", 404
cursor = db.execute(
f"SELECT rowid FROM {product_type} WHERE rowid = (?)", (id,))
if cursor.fetchone() == None:
return "Product not found", 404
cursor = db.execute(
f"UPDATE {product_type} SET in_stock = ? WHERE rowid = ?", (request.json['in_stock'], id))
cursor = db.execute(
f"SELECT name, in_stock, rowid FROM {product_type} WHERE rowid = ?", (id,))
db.commit()
products = cursor.fetchone()
return jsonify(products)
@app.route("/orders", methods=['GET'])
def get_orders():
cursor = db.execute(
"SELECT rowid, items FROM orders WHERE datetime(created_at, '+30 MINUTES') > datetime('now')")
products = cursor.fetchall()
return jsonify(products)