This repository was archived by the owner on Oct 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
116 lines (101 loc) · 3.81 KB
/
server.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
from flask import Flask, send_from_directory, jsonify, request
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
import os
import random
app = Flask(__name__, static_folder='frontend/public')
CORS(app)
basedir = os.path.abspath(os.path.dirname(__file__))
db_dir = os.path.join(basedir, 'DB')
os.makedirs(db_dir, exist_ok=True)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(db_dir, 'test.db')
db = SQLAlchemy(app)
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), unique=True, nullable=False)
description = db.Column(db.String(120), nullable=False)
quantity = db.Column(db.Integer, nullable=False)
def to_dict(self):
return {
"id": self.id,
"name": self.name,
"description": self.description,
"quantity": self.quantity
}
class Order(db.Model):
id = db.Column(db.Integer, primary_key=True)
product_id = db.Column(db.Integer, db.ForeignKey('product.id'), nullable=False)
product = db.relationship('Product', backref=db.backref('orders', lazy=True))
def to_dict(self):
return {
"id": self.id,
"product_id": self.product_id
}
@app.route("/products", methods=["GET", "POST"])
def products():
if request.method == "GET":
products = Product.query.all()
return jsonify([product.to_dict() for product in products])
else:
new_product_data = request.json
new_product = Product(name=new_product_data["name"], description=new_product_data["description"], quantity=new_product_data["quantity"])
db.session.add(new_product)
db.session.commit()
return jsonify(new_product.to_dict()), 201
@app.route("/products/<int:id>", methods=["PUT"])
def update_product(id):
product = Product.query.get(id)
product_data = request.json
product.name = product_data["name"]
product.description = product_data["description"]
product.quantity = product_data["quantity"]
db.session.commit()
return jsonify(product.to_dict())
@app.route("/orders", methods=["GET", "POST"])
def orders():
if request.method == "GET":
orders = Order.query.all()
return jsonify([order.to_dict() for order in orders])
else:
new_order_data = request.json
product = Product.query.get(new_order_data["productId"])
# Check if the product exists
if product is None:
return jsonify({"message": "Product not found"}), 404
product.quantity -= new_order_data["quantity"]
new_order = Order(product_id=product.id)
db.session.add(new_order)
db.session.commit()
return jsonify(new_order.to_dict()), 201
@app.route("/products/<int:id>", methods=["DELETE"])
def delete_product(id):
product = Product.query.get(id)
if product:
db.session.delete(product)
db.session.commit()
return jsonify({"message": f"Product {id} deleted"}), 200
else:
return jsonify({"message": "Product not found"}), 404
@app.route("/orders/<int:id>", methods=["DELETE"])
def delete_order(id):
order = Order.query.get(id)
if order:
db.session.delete(order)
db.session.commit()
return jsonify({"message": f"Order {id} deleted"}), 200
else:
return jsonify({"message": "Order not found"}), 404
@app.route("/rand")
def hello():
return str(random.randint(0, 100))
@app.route("/", defaults={"path": ''})
@app.route("/<path:path>")
def home(path):
if path != "" and os.path.exists(app.static_folder + '/' + path):
return send_from_directory(app.static_folder, path)
else:
return send_from_directory(app.static_folder, 'index.html')
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(debug=True, port=5001)