-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessory.py
110 lines (96 loc) · 4.03 KB
/
accessory.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
from flask_restful import Resource, reqparse
from models import db, Accessory
accessory_parser = reqparse.RequestParser()
accessory_parser.add_argument('name', type=str, help='Accessory name', required=True)
accessory_parser.add_argument('price', type=int, help='Accessory price', required=True)
accessory_parser.add_argument('image', type=str, help='Image URL', required=False)
accessory_parser.add_argument('description', type=str, help='Accessory description', required=False) # Add description field
class AccessoriesResource(Resource):
def get(self):
try:
accessories = Accessory.query.all()
return {
"accessories": [
{
"id": accessory.id,
"name": accessory.name,
"price": accessory.price,
"image": accessory.image,
"description": accessory.description # Add description field
}
for accessory in accessories
]
}
except Exception as e:
return {"message": "An error occurred while retrieving accessories.", "error": str(e)}, 500
def post(self):
args = accessory_parser.parse_args()
new_accessory = Accessory(
name=args['name'],
price=args['price'],
image=args.get('image'),
description=args.get('description') # Add description field
)
try:
db.session.add(new_accessory)
db.session.commit()
return {
"accessory": {
"id": new_accessory.id,
"name": new_accessory.name,
"price": new_accessory.price,
"image": new_accessory.image,
"description": new_accessory.description # Add description field
}
}, 201
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while creating the accessory.", "error": str(e)}, 500
class AccessoryByIDResource(Resource):
def get(self, id):
accessory = Accessory.query.get(id)
if not accessory:
return {"message": "Accessory not found."}, 404
return {
"accessory": {
"id": accessory.id,
"name": accessory.name,
"price": accessory.price,
"image": accessory.image,
"description": accessory.description # Add description field
}
}
def delete(self, id):
accessory = Accessory.query.get(id)
if not accessory:
return {"message": "Accessory not found."}, 404
try:
db.session.delete(accessory)
db.session.commit()
return {"message": "Accessory deleted successfully."}, 204
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while deleting the accessory.", "error": str(e)}, 500
def patch(self, id):
accessory = Accessory.query.get(id)
if not accessory:
return {"message": "Accessory not found."}, 404
args = accessory_parser.parse_args()
try:
accessory.name = args['name']
accessory.price = args['price']
accessory.image = args.get('image', accessory.image)
accessory.description = args.get('description', accessory.description) # Add description field
db.session.commit()
return {
"accessory": {
"id": accessory.id,
"name": accessory.name,
"price": accessory.price,
"image": accessory.image,
"description": accessory.description # Add description field
}
}
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while updating the accessory.", "error": str(e)}, 500