-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
216 lines (202 loc) · 7.45 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from flask_restful import Resource
from flask import request, jsonify
from vault import agent, Product, Category, User
import vault
import json
class CategoryAPI(Resource):
def get(self, category_id=None):
if category_id:
category = agent.query(Category).filter(Category.id == category_id).first()
if category:
return {
"name": category.name,
"image": "http://127.0.0.1:5000/static/assets/categories/"
+ category.image,
}, 200
else:
return {"message": "Category not found"}, 404
else:
category = agent.query(Category).all()
if category:
result = {}
for cat in category:
result[cat.id] = {
"name": cat.name,
"image": "http://127.0.0.1:5000/static/assets/"
+ cat.image,
}
return result
def post(self):
data = request.get_json()
name = data.get("name")
email = data.get("email")
password = data.get("password")
image = "/static/temp.jpg"
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
add_category = Category(name=name, image=image)
agent.add(add_category)
agent.commit()
return {"message": "Category created successfully"}, 201
else:
return {"message": "Admin not found"}, 404
def put(self, category_id):
category = agent.query(Category).filter(Category.id == category_id).first()
data = request.get_json()
name = data.get("name")
email = data.get("email")
password = data.get("password")
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
if category:
category.name = name
agent.commit()
return {"message": "Category name changed"}, 201
else:
return {"message": "Category not found"}, 404
else:
return {"message": "Not authorized"}
def delete(self, category_id):
data = request.get_json()
email = data.get("email")
password = data.get("password")
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
try:
agent.query(Category).filter(Category.id == category_id).delete()
agent.flush()
agent.commit()
return {"message": "Category deleted"}, 201
except:
return {"message": "Category not found"}, 404
else:
return {"message": "Not authorized"}
class ProductAPI(Resource):
def get(self, product_id=None):
agent.flush()
if product_id:
product = agent.query(Product).filter(Product.id == product_id).first()
if product:
return {
"name": product.name,
"image": "http://127.0.0.1:5000/static/assets/"
+ product.image,
"quantity": product.quantity,
"price": product.price,
"cat": product.category,
}, 200
else:
return {"message": "product not found"}, 404
else:
product = agent.query(Product).all()
if product:
result = dict()
for pro in product:
result[pro.id] = {
"name": pro.name,
"image": "http://127.0.0.1:5000/static/assets/"
+ pro.image,
"quantity": pro.quantity,
"price": pro.price,
"cat": pro.category,
}
return result
def post(self):
agent.flush()
data = request.get_json()
name = data.get("name")
email = data.get("email")
password = data.get("password")
image = "/static/temp.jpg"
quantity = data.get("quantity")
price = data.get("price")
category = data.get("category")
category_id = data.get("category_id")
description = data.get("description")
si_unit = data.get("si_unit")
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
add_product = Product(
name=name, image=image, quantity=quantity, price=price, category=category, category_id=category_id, description=description, si_unit=si_unit
)
agent.add(add_product)
agent.commit()
return {"message": "product created successfully"}, 201
else:
return {"message": "Admin not found"}, 404
def put(self, product_id):
agent.flush()
product = agent.query(Product).filter(Product.id == product_id).first()
data = request.get_json()
name = data.get("name")
email = data.get("email")
password = data.get("password")
image = data.get("image")
quantity = data.get("quantity")
price = data.get("price")
category = data.get("category")
category_id = data.get("category_id")
description = data.get("description")
si_unit = data.get("si_unit")
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
if product:
product.name = name
product.image = image
product.quantity = quantity
product.price = price
product.category = category
product.category_id = category_id
product.description = description
product.si_unit = si_unit
agent.commit()
return {"message": "Product Updated Successfully!!"}, 201
else:
return {"message": "product not found"}, 404
else:
return {"message": "Not authorized, invalid credentials provided!!!"}
def delete(self, product_id):
agent.flush()
data = request.get_json()
email = data.get("email")
password = data.get("password")
admin = (
agent.query(User)
.filter(User.email == email)
.filter(User.password == password)
.first()
)
if admin and admin.admin == 1:
try:
agent.query(Product).filter(Product.id == product_id).delete()
agent.commit()
return {"message": "Product deleted successfully!"}, 201
except Exception as e:
return {"message": f"product not found,{e}"}, 404
else:
return {"message": "Not authorized, invalid credentials provided!!!"}