feat: save order in database (backent)
This commit is contained in:
parent
4dd0a9c1e8
commit
69e634ded0
98
backend/main.py
Normal file
98
backend/main.py
Normal file
@ -0,0 +1,98 @@
|
||||
from flask import Flask, request, jsonify
|
||||
import sqlite3
|
||||
import os
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
DATABASE = './main.db'
|
||||
|
||||
def initDB():
|
||||
if os.path.isfile(DATABASE):
|
||||
db = sqlite3.connect(DATABASE)
|
||||
else:
|
||||
with open('schema.sql', 'r') as file:
|
||||
sql_statements = file.read()
|
||||
|
||||
db = sqlite3.connect(DATABASE)
|
||||
cursor = db.cursor()
|
||||
cursor.executescript(sql_statements)
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
return 'Hello, Flask!'
|
||||
|
||||
@app.route('/order', methods=['POST'])
|
||||
def process_data():
|
||||
data = request.get_json()
|
||||
|
||||
db = sqlite3.connect(DATABASE)
|
||||
cursor = db.cursor()
|
||||
cursor.execute("SELECT id from `shops` WHERE address=?", (data['from'], ))
|
||||
shop_id = cursor.fetchone()[0]
|
||||
|
||||
filter = []
|
||||
params = []
|
||||
for product in data['products']:
|
||||
product_id = product['product_id']
|
||||
filter.append('?')
|
||||
params.append(product_id)
|
||||
filter_str = ', '.join(filter)
|
||||
query = "SELECT id, price FROM products WHERE id IN ({})".format(filter_str)
|
||||
cursor.execute(query, params)
|
||||
prices = cursor.fetchall()
|
||||
|
||||
amount = 0
|
||||
for index, product in enumerate(data['products']):
|
||||
count = int(product['count'])
|
||||
amount += count * int(prices[index][1])
|
||||
|
||||
cursor.execute('INSERT INTO "orders"("id","shop_id","client_addr","amount") VALUES (NULL,?,NULL,?);', (shop_id, amount))
|
||||
order_id = cursor.lastrowid
|
||||
|
||||
for index, product in enumerate(data['products']):
|
||||
product_id = product['product_id']
|
||||
count = int(product['count'])
|
||||
cursor.execute('INSERT INTO "order_products"("id","order_id","product_id","count") VALUES (NULL,?,?,?);', (order_id, product_id, count))
|
||||
|
||||
db.commit()
|
||||
db.close()
|
||||
|
||||
return str(order_id)
|
||||
|
||||
@app.route('/order/<id>', methods=['GET'])
|
||||
def get_order(id):
|
||||
db = sqlite3.connect(DATABASE)
|
||||
cursor = db.cursor()
|
||||
|
||||
ans = {}
|
||||
|
||||
# products
|
||||
cursor.execute("SELECT `name`, `price`, `count` FROM `order_products`, `products` \
|
||||
WHERE `order_products`.`order_id`=? and `order_products`.`product_id`=`products`.`id`", (id, ))
|
||||
products = cursor.fetchall()
|
||||
|
||||
ans['products'] = []
|
||||
for product in products:
|
||||
ans['products'].append({
|
||||
'name': product[0],
|
||||
'price': product[1],
|
||||
'count': product[2]
|
||||
})
|
||||
|
||||
# shop name
|
||||
cursor.execute("SELECT `address`, `name`, `amount` FROM `shops`, `orders` WHERE `orders`.`shop_id`=`shops`.`id` and `orders`.`id`= ?", (id, ))
|
||||
result = cursor.fetchone()
|
||||
ans['shop'] = {
|
||||
'name': result[1],
|
||||
'address': result[0]
|
||||
}
|
||||
ans['amount'] = result[2]
|
||||
|
||||
return jsonify(ans)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
initDB()
|
||||
app.run()
|
||||
52
backend/schema.sql
Normal file
52
backend/schema.sql
Normal file
@ -0,0 +1,52 @@
|
||||
|
||||
CREATE TABLE "shops" (
|
||||
"id" INTEGER,
|
||||
"address" TEXT,
|
||||
"name" TEXT,
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
|
||||
---------------------------------------------------
|
||||
|
||||
CREATE TABLE "products" (
|
||||
"id" INTEGER,
|
||||
"shop_id" INTEGER,
|
||||
"name" TEXT,
|
||||
"code" TEXT,
|
||||
"price" INTEGER,
|
||||
FOREIGN KEY("shop_id") REFERENCES "shops"("id"),
|
||||
PRIMARY KEY("id" AUTOINCREMENT)
|
||||
);
|
||||
|
||||
---------------------------------------------------
|
||||
|
||||
CREATE TABLE "orders" (
|
||||
"id" INTEGER,
|
||||
"shop_id" INTEGER,
|
||||
"client_addr" TEXT,
|
||||
"amount" TEXT,
|
||||
PRIMARY KEY("id" AUTOINCREMENT),
|
||||
FOREIGN KEY("shop_id") REFERENCES "shops"("id")
|
||||
);
|
||||
|
||||
---------------------------------------------------
|
||||
|
||||
CREATE TABLE "order_products" (
|
||||
"id" INTEGER,
|
||||
"order_id" INTEGER,
|
||||
"product_id" INTEGER,
|
||||
"count" INTEGER,
|
||||
FOREIGN KEY("product_id") REFERENCES "products"("id"),
|
||||
PRIMARY KEY("id" AUTOINCREMENT),
|
||||
FOREIGN KEY("order_id") REFERENCES "orders"("id")
|
||||
);
|
||||
|
||||
---------------------------------------------------
|
||||
|
||||
INSERT INTO "main"."shops"("id","address","name") VALUES (NULL,"0x73D081e82b35D6E6f9f5e72EBBB6b637d4f46992","全家便利商店");
|
||||
INSERT INTO "main"."shops"("id","address","name") VALUES (NULL,"0xDa68136fcB885a2ec44db6dcE946F656aF457A76","暨大圖文部");
|
||||
|
||||
INSERT INTO "main"."products"("id","shop_id","name","code", "price") VALUES (NULL,1,"濃辛咖哩飯","7233957360139", "10000000000000000");
|
||||
INSERT INTO "main"."products"("id","shop_id","name","code", "price") VALUES (NULL,1,"霜淇淋","4718022345288", "5000000000000000");
|
||||
INSERT INTO "main"."products"("id","shop_id","name","code", "price") VALUES (NULL,2,"證件套","748009345271", "25000000000000000");
|
||||
INSERT INTO "main"."products"("id","shop_id","name","code", "price") VALUES (NULL,2,"便條紙","4979274503226", "4000000000000000");
|
||||
Loading…
Reference in New Issue
Block a user