-
Notifications
You must be signed in to change notification settings - Fork 0
/
phone.py
121 lines (113 loc) · 4.73 KB
/
phone.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
from flask_restful import Resource, reqparse
from models import db, Phone
phone_parser = reqparse.RequestParser()
phone_parser.add_argument('brand_id', type=int, help='Brand ID', required=True)
phone_parser.add_argument('name', type=str, help='Phone name', required=True)
phone_parser.add_argument('price', type=int, help='Phone price', required=True)
phone_parser.add_argument('status', type=str, help='Phone status', required=True)
phone_parser.add_argument('image_url', type=str, help='Image URL', required=False)
phone_parser.add_argument('description', type=str, help='Phone description', required=False)
class PhonesResource(Resource):
def get(self):
try:
phones = Phone.query.all()
return {
"phones": [
{
"id": phone.id,
"brand_id": phone.brand_id,
"name": phone.name,
"price": phone.price,
"status": phone.status,
"image_url": phone.image_url,
"description": phone.description # Add description field
}
for phone in phones
]
}
except Exception as e:
return {"message": "An error occurred while retrieving phones.", "error": str(e)}, 500
def post(self):
try:
args = phone_parser.parse_args()
new_phone = Phone(
brand_id=args['brand_id'],
name=args['name'],
price=args['price'],
status=args['status'],
image_url=args.get('image_url'),
description=args.get('description') # Add description field
)
db.session.add(new_phone)
db.session.commit()
return {
"phone": {
"id": new_phone.id,
"brand_id": new_phone.brand_id,
"name": new_phone.name,
"price": new_phone.price,
"status": new_phone.status,
"image_url": new_phone.image_url,
"description": new_phone.description # Add description field
}
}, 201
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while creating the phone.", "error": str(e)}, 500
class PhoneByIDResource(Resource):
def get(self, id):
try:
phone = Phone.query.get(id)
if not phone:
return {"message": "Phone not found."}, 404
return {
"phone": {
"id": phone.id,
"brand_id": phone.brand_id,
"name": phone.name,
"price": phone.price,
"status": phone.status,
"image_url": phone.image_url,
"description": phone.description # Add description field
}
}
except Exception as e:
return {"message": "An error occurred while fetching the phone.", "error": str(e)}, 500
def delete(self, id):
try:
phone = Phone.query.get(id)
if not phone:
return {"message": "Phone not found."}, 404
db.session.delete(phone)
db.session.commit()
return {"message": "Phone deleted successfully."}, 204
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while deleting the phone.", "error": str(e)}, 500
def patch(self, id):
try:
phone = Phone.query.get(id)
if not phone:
return {"message": "Phone not found."}, 404
args = phone_parser.parse_args()
phone.brand_id = args['brand_id']
phone.name = args['name']
phone.price = args['price']
phone.status = args['status']
phone.image_url = args.get('image_url', phone.image_url)
phone.description = args.get('description', phone.description) # Add description field
db.session.commit()
return {
"phone": {
"id": phone.id,
"brand_id": phone.brand_id,
"name": phone.name,
"price": phone.price,
"status": phone.status,
"image_url": phone.image_url,
"description": phone.description # Add description field
}
}
except Exception as e:
db.session.rollback()
return {"message": "An error occurred while updating the phone.", "error": str(e)}, 500