-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
129 lines (97 loc) · 3.2 KB
/
app.py
File metadata and controls
129 lines (97 loc) · 3.2 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# app init
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(
basedir, "db.sqlite"
)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
# Init db
db = SQLAlchemy(app)
# Init ma
ma = Marshmallow(app)
# Product Class/Model
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
description = db.Column(db.String(200))
price = db.Column(db.Float)
quantity = db.Column(db.Integer)
def __init__(self, name, description, price, quantity):
self.name = name
self.description = description
self.price = price
self.quantity = quantity
# Product Schema
class ProductSchema(ma.Schema):
class Meta:
fields = ("id", "name", "description", "price", "quantity")
# Init Schema
product_schema = ProductSchema()
products_schema = ProductSchema(many=True)
# Create the Tables
with app.app_context():
db.create_all()
@app.route("/", methods=["GET"])
def get():
return jsonify({
'Url Routes':{
"home-GET": "/",
"get all product-GET": "Product",
"add a product-POST": "Product",
"get a product detail-GET":"Product/id",
"edit a product detail-PUT":"Product/id",
"delete a product-DELETE":"Product/id"
},
})
# add product
@app.route("/product", methods=["POST"])
def add_product():
name = request.json["name"]
description = request.json["description"]
price = request.json["price"]
quantity = request.json["quantity"]
new_product = Product(name, description, price, quantity)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)
# get all product
@app.route("/product", methods=["GET"])
def get_products():
products = Product.query.all()
result = products_schema.dump(products)
return result
# get single product
@app.route("/product/<int:id>", methods=["GET"])
def get_product(id):
product = Product.query.get(id)
result = product_schema.dump(product)
return result
# edit a product
@app.route("/product/<int:id>", methods=["PUT"])
def edit_product(id):
product = Product.query.get(id)
name = request.json["name"]
description = request.json["description"]
price = request.json["price"]
quantity = request.json["quantity"]
product.name = name
product.price = price
product.description = description
product.quantity = quantity
db.session.commit()
return product_schema.jsonify(product)
# delete a product
@app.route("/product/<int:id>", methods=["DELETE"])
def delete_product(id):
product = Product.query.get(id)
db.session.delete(product)
db.session.commit()
return {"msg": "product deleted"}
# Run Server
if __name__ == "__main__":
app.run(debug=True)