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