-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathamenities.py
More file actions
executable file
·100 lines (81 loc) · 2.71 KB
/
Copy pathamenities.py
File metadata and controls
executable file
·100 lines (81 loc) · 2.71 KB
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
#!/usr/bin/python3
"""
Creates API endpoints for acessing, creating, deleting and
updating Amenity resources, it uses the app_views blueprint
"""
from api.v1.views import app_views
from models import storage
from models.amenity import Amenity
from flask import jsonify, request, abort
@app_views.route('/amenities', strict_slashes=False,
methods=['GET'])
def get_amenities():
"""
Retrieves all amenity objects and returns the JSON
representation of the dictionary
"""
amenities = []
all_amenities = storage.all(Amenity)
for amenity in all_amenities.values():
amenities.append(amenity.to_dict())
return (jsonify(amenities))
@app_views.route('/amenities/<amenity_id>', strict_slashes=False,
methods=['GET'])
def get_an_amenity(amenity_id):
"""
Retrieves a specific amenity based on it's id
404 error if the amenity does not exist
"""
amenity = storage.get(Amenity, amenity_id)
if amenity is not None:
return (jsonify(amenity.to_dict()))
abort(404)
@app_views.route('/amenities/<amenity_id>', strict_slashes=False,
methods=['DELETE'])
def delete_amenity(amenity_id):
"""
Deletes an amenity based on it's id
Returns an empty dictionary plus 200ok status on success
or 404 error page on failure
"""
amenity = storage.get(Amenity, amenity_id)
if amenity is None:
abort(404)
storage.delete(amenity)
storage.save()
return (jsonify({})), 200
@app_views.route('/amenities', strict_slashes=False, methods=['POST'])
def create_amenity():
"""
Creates an amenity and returns the dictionary representation of the
amenity, or an error dictionary
"""
data = request.get_json()
if not isinstance(data, dict):
return (jsonify({'error': 'Not a JSON'}))
if 'name' not in data.keys():
return (jsonify({'error': 'Missing name'}))
name = data.get('name')
amenity = Amenity(name=f'{data.get('name')}')
storage.new(amenity)
storage.save()
return (jsonify(amenity.to_dict())), 201
@app_views.route('/amenities/<amenity_id>', strict_slashes=False,
methods=['PUT'])
def update_amenity(amenity_id):
"""
Updates an amenity based on the id, returns a code 201
on success or an error dictionary
"""
data = request.get_json()
if not isinstance(data, dict):
return (jsonify({'error': 'Not a JSON'}))
amenity = storage.get(Amenity, amenity_id)
if amenity is None:
abort(404)
exclude = ['id', 'created_at', 'updated_at']
for k, v in data.items():
if k not in exclude:
setattr(amenity, k, v,)
amenity.save()
return (jsonify(amenity.to_dict())), 200