-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Expand file tree
/
Copy pathplaces.py
More file actions
executable file
·81 lines (61 loc) · 2.09 KB
/
Copy pathplaces.py
File metadata and controls
executable file
·81 lines (61 loc) · 2.09 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
#!/usr/bin/python3
""" Create a view for the Place Objects """
from models import storage
from models.place import Place
from api.v1.views import app_views
from flask import jsonify, request, abort
@app_views.route("/places", methods=["GET"])
def all_places():
""" Retrieve all Place objects """
places = storage.all(Place)
place_list = [place.to_dict() for place in places.values()]
return jsonify(place_list)
@app_views.route("/places/<place_id>", methods=["GET"])
def one_place(place_id):
""" Retrieves a Place Object """
place = storage.get(Place, place_id)
if not place:
abort(404)
return jsonify(place.to_dict())
@app_views.route("/places/<place_id>", methods=["DELETE"])
def delete_place(place_id):
""" Retrieves a Place Object """
place = storage.get(Place, place_id)
if not place:
abort(404)
storage.delete(place)
storage.save()
return {}, 200
@app_views.route("/cities/<city_id>", methods=["POST"])
def create_place(city_id):
""" Create a new Place object """
city = storage.get(City, city_id)
if not city:
abort(404)
try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400
if "user_id" not in data:
return jsonify({"error": "Missing user_id"}), 400
if "name" not in data:
return jsonify({"error": "Missing name"}), 400
new_place = Place(name=data["name"], user_id=data["user_id"])
storage.new(new_place)
storage.save()
return jsonify(new_place.to_dict()), 201
@app_views.route("/places/<place_id>", methods=["PUT"])
def update_place(place_id):
""" Update a Place object """
one_place = storage.get(Place, place_id)
if not one_place:
return jsonify({"error": "Not found"}), 404
try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400
for key, value in data.items():
if key not in ["id", "created_at", "updated_at"]:
setattr(one_place, key, value)
storage.save()
return jsonify(one_place.to_dict()), 200