-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.py
115 lines (107 loc) · 4.3 KB
/
order.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
from flask_restful import Resource, reqparse
from models import db, Order, OrderItem
order_parser = reqparse.RequestParser()
order_parser.add_argument('user_id', type=int, help='User ID', required=True)
order_parser.add_argument('total_price', type=int, help='Total price', required=True)
order_parser.add_argument('order_date', type=str, help='Order date', required=True)
class OrderResource(Resource):
def get(self):
try:
orders = Order.query.all()
return {
"orders": [
{
"id": order.id,
"user_id": order.user_id,
"total_price": order.total_price,
"order_date": order.order_date.isoformat(),
"order_items": [
{
"component_type": item.component_type,
"component_id": item.component_id,
"quantity": item.quantity
}
for item in order.order_items
]
}
for order in orders
]
}
except Exception as e:
return {"message": "An error occurred while retrieving orders.", "error": str(e)}, 500
def post(self):
try:
args = order_parser.parse_args()
new_order = Order(
user_id=args['user_id'],
total_price=args['total_price'],
order_date=args['order_date']
)
db.session.add(new_order)
db.session.commit()
return {
"order": {
"id": new_order.id,
"user_id": new_order.user_id,
"total_price": new_order.total_price,
"order_date": new_order.order_date.isoformat()
}
}, 201
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while creating the order.", "error": str(e)}, 500
class OrderByIDResource(Resource):
def get(self, id):
try:
order = Order.query.get(id)
if order:
return {
'id': order.id,
'user_id': order.user_id,
'total_price': order.total_price,
'order_date': order.order_date.isoformat(),
'order_items': [
{
'component_type': item.component_type,
'component_id': item.component_id,
'quantity': item.quantity
}
for item in order.order_items
]
}
else:
return {'message': 'Order not found'}, 404
except Exception as e:
return {"message": "An error occurred while retrieving the order.", "error": str(e)}, 500
def delete(self, id):
try:
order = Order.query.get(id)
if not order:
return {"message": "Order not found."}, 404
db.session.delete(order)
db.session.commit()
return {"message": "Order deleted successfully."}, 204
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while deleting the order.", "error": str(e)}, 500
def patch(self, id):
try:
args = order_parser.parse_args()
order = Order.query.get(id)
if not order:
return {"message": "Order not found."}, 404
order.user_id = args['user_id']
order.total_price = args['total_price']
order.order_date = args['order_date']
db.session.commit()
return {
"order": {
"id": order.id,
"user_id": order.user_id,
"total_price": order.total_price,
"order_date": order.order_date.isoformat()
}
}
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while updating the order.", "error": str(e)}, 500