def state_by_id(state_id=None): """ Access the api call with on a specific state object returns a 404 if not found. - DELETE: Removes the state object - PUT: Updates the state object - GET: Default, return the state object. """ if state_id not in storage.all('State'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('State', state_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('State', state_id) for attrib in put_obj: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET""" instance = storage.get('State', state_id) return (jsonify(instance.to_json()))
def user_by_id(user_id=None): """ Access the api call with on a specific user object returns a 404 if not found. Delete method removes the object Defaults is to return the user object. """ if user_id not in storage.all('User'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('User', user_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('User', user_id) ignore_keys = ['id', 'email', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """Default: GET request returns the object in json form""" instance = storage.get('User', user_id) return (jsonify(instance.to_json()))
def delete_placeamenity(place_id=None, amenity_id=None): """Example endpoint deleting one placeamenity Deletes a placeamenity based on the place_id and amenity_id --- definitions: PlaceAmenity: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ place = storage.get("Place", place_id) if place is None: abort(404) amenity = storage.get("Amenity", amenity_id) if amenity is not None: try: place.amenities.remove(amenity) place.save() return jsonify({}), 200 except ValueError: abort(404) else: abort(404)
def create_amenity_in_place(place_id=None, amenity_id=None): """Example endpoint creates a link between a amenity and a place Creates a link based on an amentiy and a place based on the JSON body --- parameters: - name: place_id in: path type: string enum: ["279b355e-ff9a-4b85-8114-6db7ad2a4cd2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 201: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ place = storage.get("Place", place_id) if place is None: abort(404) amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) if amenity in place.amenities: return jsonify(amenity.to_json()), 200 place.amenities.append(amenity) place.save() return jsonify(amenity.to_json()), 201
def all_reviews(review_id=None): """ Access the api call with on a place object to get its reviews returns a 404 if not found. - GET: Default, gets a review at <review_id>, status 200 - DELETE: Deletes the review at id. Returns '{}', status 200 - PUT: """ if review_id not in storage.all('Review'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Review', review_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('Review', review_id) ignore_keys = ['id', 'user_id', 'place_id', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET """ instance_get = storage.get('Review', review_id) return (jsonify(instance_get.to_json()))
def get_city_obj(city_id=None): """ API call to interact with a specific city object returns a 404 if city_id is not found. - DELETE method: Deletes the resource and returns {}, status 200 - PUT method: Updates the resource with the supplied json, status 201 - GET method: Default, returns the city object """ if city_id not in storage.all('City'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('City', city_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('City', city_id) ignore_keys = ['id', 'state_id', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET """ city_get = storage.get('City', city_id) return (jsonify(city_get.to_json()))
def link_amenity_to_place(place_id=None, amenity_id=None): """ Link a Amenity object to a Place """ place = storage.get(Place, place_id) if place is None: return jsonify({'Error': 'Not found'}), 404 amenity = storage.get(Amenity, amenity_id) if amenity is None: return jsonify({'Error': 'Not found'}), 404 if getenv('HBNB_TYPE_STORAGE') == "db": if amenity in place.amenities: return jsonify(amenity.to_dict()), 200 else: place.amenities.append(amenity) else: if amenity_id in place.amenity_ids: return jsonify(amenity.to_dict()), 200 else: place.amenity_ids.append(amenity_id) storage.save() return jsonify(amenity.to_dict()), 201
def place_by_id(place_id=None): """ Access the api call with on a specific state object returns a 404 if not found. - DELETE: Removes the state object - PUT: Updates the state object - GET: Default, return the state object. """ if place_id not in storage.all('Place'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Place', place_id)) storage.save() return (jsonify({})) if request.method == 'PUT': put_obj = request.get_json() if put_obj is None: return ("Not a JSON", 400) instance = storage.get('Place', place_id) ignore_keys = ['id', 'user_id', 'city_id', 'created_at', 'updated_at'] for attrib in put_obj: if attrib not in ignore_keys: setattr(instance, attrib, put_obj[attrib]) instance.save() return (jsonify(instance.to_json())) """ Default: GET""" instance = storage.get('Place', place_id) return (jsonify(instance.to_json()))
def delete_amenity_by_place_id(place_id, amenity_id): """ deletes a amenity object by place_id """ place = storage.get(Place, place_id) if place is None: return jsonify({'Error': 'Not found'}), 404 amenity = storage.get(Amenity, amenity_id) if amenity is None: return jsonify({'Error': 'Not found'}), 404 if getenv('HBNB_TYPE_STORAGE') == "db": if amenity not in place.amenities: return jsonify({'Error': 'Not found'}), 404 place.amenities.remove(amenity) else: if amenity_id not in place.amenity_ids: return jsonify({'Error': 'Not found'}), 404 place.amenity_ids.remove(amenity_id) storage.save() return jsonify({}), 200
def create_place(city_id): '''This is the 'create_place' method. Creates a Place object. ''' city = storage.get("City", city_id) if city is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if 'user_id' not in r.keys(): return "Missing user_id", 400 user = storage.get("User", r.get("user_id")) if user is None: abort(404) if 'name' not in r.keys(): return "Missing name", 400 r["city_id"] = city_id place = Place(**r) place.save() return jsonify(place.to_json()), 201
def create_review(place_id): '''This is the 'create_review' method. Creates a Review object of a given place. ''' try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if "user_id" not in r.keys(): return "Missing user_id", 400 if "text" not in r.keys(): return "Missing text", 400 place = storage.get("Place", place_id) if place is None: abort(404) user = storage.get("User", r["user_id"]) if user is None: abort(404) review = Review(**r) review.place_id = place_id review.save() return jsonify(review.to_json()), 201
def create_new_place(city_id=None): """ create new resource by my list of places """ city_object = storage.get(City, city_id) if city_object is None: abort(404, "Not found") place_object_json = request.get_json() if place_object_json is None: abort(400, "Not a JSON") # looking for a "user_id" value for creating a object place user_id = place_object_json.get("user_id") if user_id is None: abort(400, 'Missing user_id') # creating a new user object as attribute of place object user_object = storage.get(User, user_id) if user_object is None: abort(404, "Not found") # verify if exist "name" value into POST request if place_object_json.get("name") is None: abort(400, 'Missing name') # If the city_id is not linked to any City object, raise a 404 error # this will be never raise a 404 error place_object_json["city_id"] = city_id place_object = Place(**place_object_json) place_object.save() return jsonify(place_object.to_dict()), 201
def create_amenity_in_place(place_id=None, amenity_id=None): """Example endpoint creates a link between a amenity and a place Creates a link based on an amentiy and a place based on the JSON body --- parameters: - name: place_id in: path type: string enum: ["279b355e-ff9a-4b85-8114-6db7ad2a4cd2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 201: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ place = storage.get("Place", place_id) if place is None: abort(404) amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) if amenity in place.amenities: return jsonify(amenity.to_json()), 200 place.amenities.append(amenity) place.save() return jsonify(amenity.to_json()), 201
def delete_placeamenity(place_id=None, amenity_id=None): """Example endpoint deleting one placeamenity Deletes a placeamenity based on the place_id and amenity_id --- definitions: PlaceAmenity: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ place = storage.get("Place", place_id) if place is None: abort(404) amenity = storage.get("Amenity", amenity_id) if amenity is not None: try: place.amenities.remove(amenity) place.save() return jsonify({}), 200 except ValueError: abort(404) else: abort(404)
def create_a_new_review(place_id): """ create new resource by my list of place """ place = storage.get(Place, place_id) if place is None: return jsonify({'Error': 'Not found'}), 404 review_object_json = request.get_json() if review_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 if 'user_id' not in review_object_json: return jsonify({'Error': 'Missing user_id'}), 400 user_id = review_object_json.get('user_id') user = storage.get(User, user_id) if user is None: return jsonify({'Error': 'Not found'}), 404 if 'text' not in review_object_json: return jsonify({'Error': 'Missing text'}), 400 review_object_json['place_id'] = place_id instance = Review(**review_object_json) instance.save() return jsonify(instance.to_dict()), 201
def view_place_amenity(place_id): '''This is the 'view_place_amenity' method. Recalls all Amenity objects in a given place. ''' place = storage.get("Place", place_id) if place is None: abort(404) amenity = [storage.get("Amenity", i) for i in place.amenities] return jsonify(amenity)
def delete_amenity_from(place_id, amenity_id): """Delete an Amenity object by it's ID.""" place = storage.get("Place", place_id) amenity = storage.get("Amenity", amenity_id) if place is None or amenity is None: abort(404) try: place.amenities.remove(amenity) place.save() return jsonify({}), 200 except: abort(404)
def link_amenity_to(place_id, amenity_id): """Link an Amenity object to a given Place.""" place = storage.get("Place", place_id) amenity = storage.get("Amenity", amenity_id) if place is None or amenity is None: abort(404) if amenity in place.amenities: return jsonify(amenity.to_json()), 200 try: place.amenities.append(amenity) place.save() return jsonify(amenity.to_json()), 201 except: abort(404)
def create_new_place(city_id=None): """ create new resource by my list of place """ print("New request:") # print("\t-path: {}".format(path)) print("\t-verb: {}".format(request.method)) print("\t-headers: ") for k, v in request.headers: print("\t\t{} = {}".format(k, v)) print("\t-query parameters: ") for qp in request.args: print("\t\t{} = {}".format(qp, request.args.get(qp))) print("\t-raw body: ") print("\t\t{}".format(request.data)) print("\t-form body: ") for fb in request.form: print("\t\t{} = {}".format(fb, request.form.get(fb))) print("\t-json body: ") print("\t\t{}".format(request.json)) city = storage.get(City, city_id) if city is None: return jsonify({'Error': 'Not found'}), 404 place_object_json = request.get_json() if place_object_json is None: return jsonify({'Error': 'Not a JSON'}), 400 if 'user_id' not in place_object_json.keys(): return jsonify({'Error': 'Missing user_id'}), 400 user = storage.get(User, place_object_json.get("user_id")) if user is None: return jsonify({'Error': 'Not found'}), 404 if 'name' not in place_object_json.keys(): return jsonify({'Error': 'Missing name'}), 400 place_object_json["city_id"] = city_id object_place = Place(**place_object_json) object_place.save() return jsonify(object_place.to_dict()), 201
def delete_amenity(amenity_id=None): """Example endpoint deleting one amenity Deletes a review based on the amenity_id --- definitions: Amenity: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) storage.delete(amenity) return jsonify({}), 200
def delete_user(user_id=None): """Example endpoint deleting one user Deletes a user based on the user_id --- definitions: User: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/User' examples: {} """ user = storage.get("User", user_id) if user is None: abort(404) storage.delete(user) return jsonify({}), 200
def delete_one_review(review_id): """Example endpoint deleting one review Deletes a review based on the place_id --- definitions: Review: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ review = storage.get("Review", review_id) if review is None: abort(404) storage.delete(review) return jsonify({}), 200
def delete_state(state_id=None): """Example endpoint deleting one state Deletes a state based on the state_id --- definitions: State: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/State' examples: {} """ if state_id is None: abort(404) state = storage.get("State", state_id) if state is None: abort(404) storage.delete(state) return jsonify({}), 200
def state_all_cities(state_id): """Example endpoint returning a list of all the cities of a state Retrieves all the cities of a given state_id --- parameters: - name: state_id in: path type: string enum: ['None', '10098698-bace-4bfb-8c0a-6bae0f7f5b8f'] required: true default: None definitions: City: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the city name: type: string description: name of the city state_id: type: string description: the id of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionaries of city object schema: $ref: '#/definitions/City' examples: [{"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "1da255c0-f023-4779-8134-2b1b40f87683", "name": "New Orleans", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}, {"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "45903748-fa39-4cd0-8a0b-c62bfe471702", "name": "Lafayette", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}] """ state = storage.get("State", state_id) if state is None: abort(404) all_cities = [city.to_json() for city in state.cities] return jsonify(all_cities)
def delete_one_city(city_id): """Example endpoint deleting one city Deletes a state based on the city_id --- definitions: City: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ city = storage.get("City", city_id) if city is None: abort(404) storage.delete(city) return jsonify({})
def delete_one_review(review_id): """Example endpoint deleting one review Deletes a review based on the place_id --- definitions: Review: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ review = storage.get("Review", review_id) if review is None: abort(404) storage.delete(review) return jsonify({}), 200
def city_by_state(state_id=None): """ Access the api call with on a state object to get its cities returns a 404 if not found. - POST: Creates a new city object with the state_object linked - GET: Default, returns all city objects linked to the state. """ if state_id not in storage.all('State'): abort(404) if request.method == 'POST': post_obj = request.get_json() if post_obj is None: return ("Not a JSON", 400) if 'name' not in post_obj: return ("Missing name", 400) new_obj = City(**post_obj) new_obj.state_id = state_id new_obj.save() return (jsonify(new_obj.to_json()), 201) """Default: GET""" all_cities = storage.get('State', state_id).cities rtn_json = [] for city in all_cities: rtn_json.append(city.to_json()) return (jsonify(rtn_json))
def get_city(city_id): """Return a City object given it's ID.""" try: city = storage.get("City", city_id).to_json() except: abort(404) return jsonify(city), 200
def state_all_cities(state_id): """Example endpoint returning a list of all the cities of a state Retrieves all the cities of a given state_id --- parameters: - name: state_id in: path type: string enum: ['None', '10098698-bace-4bfb-8c0a-6bae0f7f5b8f'] required: true default: None definitions: City: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the city name: type: string description: name of the city state_id: type: string description: the id of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionaries of city object schema: $ref: '#/definitions/City' examples: [{"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "1da255c0-f023-4779-8134-2b1b40f87683", "name": "New Orleans", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}, {"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "45903748-fa39-4cd0-8a0b-c62bfe471702", "name": "Lafayette", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}] """ state = storage.get("State", state_id) if state is None: abort(404) all_cities = [city.to_json() for city in state.cities] return jsonify(all_cities)
def update_place(place_id=None): '''This is the 'update_place' method. Updates a give Place object. ''' try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 place = storage.get("Place", place_id) if place is None: abort(404) for instance in ("id", "user_id", "city_id", "created_at", "updated_at"): r.pop(instance, None) for key, value in r.items(): setattr(place, key, value) place.save() return jsonify(place.to_json()), 200
def delete_one_city(city_id): """Example endpoint deleting one city Deletes a state based on the city_id --- definitions: City: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ city = storage.get("City", city_id) if city is None: abort(404) storage.delete(city) return jsonify({})
def update_state(state_id=None): '''This is the 'update_state' module. Updates a State object. ''' try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 state = storage.get("State", state_id) if state is None: abort(404) for instance in ("id", "created_at", "updated_at"): r.pop(instance, None) for key, value in r.items(): setattr(state, key, value) state.save() return jsonify(state.to_json()), 200
def manipulate_amenties_place(place_id=None): """ Access the api call with on a place object to get its amenities returns a 404 if not found. - DELETE: Deletes the link between Amenity objects and Place objects If the Amenity is not linked to the Place before the request, raise a 404 error Returns an empty dictionary with the status code 200 - POST: Link a Amenity object to a Place If the Amenity is already linked to the Place, return the Amenity with the status code 200 Returns the Amenity with the status code 201 """ if place_id not in storage.all('Place'): abort(404) if amenity_id not in storage.all('Amenity'): abort(404) if request.method == 'DELETE': storage.delete(storage.get('Place', place_id)) storage.save() return (jsonify({})) if request.method == 'POST': post_obj = request.get_json() if post_obj is None: return ("Not a JSON", 400) if 'name' not in post_obj: return ("Missing name", 400) new_obj = City(**post_obj) new_obj.state_id = state_id new_obj.save() return (jsonify(new_obj.to_json()), 201)
def get_review(review_id): """Return a Review object given it's ID.""" try: review = storage.get('Review', review_id) return jsonify(review.to_json()) except: abort(404)
def get_user(user_id): """Return a User object given it's ID.""" try: user = storage.get("User", user_id) return jsonify(user.to_json()) except: abort(404)
def all_reviews(place_id): """Return a JSON list of all Review objects for a given Place.""" try: place = storage.get('Place', place_id) return jsonify([review.to_json() for review in place.reviews]) except: abort(404)
def delete_place_amenity(place_id=None, amenity_id=None): '''This is the 'delete_place_amenity' method. Deletes an Amenity object of a place. ''' place = storage.get("Place", place_id) if place is None: abort(404) amenity = storage.get("Amenity", amenity_id) if amenity is not None: place.amenities.remove(amenity) place.save() return jsonify(place.amenities), 200 return jsonify({}), 200
def delete_amenity(amenity_id=None): """Example endpoint deleting one amenity Deletes a review based on the amenity_id --- definitions: Amenity: type: object Color: type: string items: $ref: '#/definitions/Color' responses: 200: description: An empty dictionary schema: $ref: '#/definitions/City' examples: {} """ amenity = storage.get("Amenity", amenity_id) if amenity is None: abort(404) storage.delete(amenity) return jsonify({}), 200
def update_state(state_id=None): """Example endpoint updates a state Updates a State object based on the JSON body --- parameters: - name: state_id in: path type: string enum: ['None', '10098698-bace-4bfb-8c0a-6bae0f7f5b8f'] required: true default: None definitions: State: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the state name: type: string description: name of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 201: description: A list of a single dictionary of a State schema: $ref: '#/definitions/State' examples: [{'__class__': 'State', 'created_at': '2017-03-25T02:17:06', 'id': '10098698-bace-4bfb-8c0a-6bae0f7f5b8f', 'name': 'Oregon', 'updated_at': '2017-03-25T02:17:06'}] """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 state = storage.get("State", state_id) if state is None: abort(404) for k in ("id", "created_at", "updated_at"): r.pop(k, None) for k, v in r.items(): setattr(state, k, v) state.save() return jsonify(state.to_json()), 200
def all_reviews(place_id): """Example endpoint returning a list of all reviews Retrieves a list of all reviews associated with a place --- parameters: - name: place_id in: path type: string enum: ['279b355e-ff9a-4b85-8114-6db7ad2a4cd2'] required: true default: '279b355e-ff9a-4b85-8114-6db7ad2a4cd2' definitions: State: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the review place_id: type: string description: the id of the place text: type: string description: the text of the review updated_at: type: string description: The date the object was updated user_id: type: string description: The user id items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionaries of all reviews objects schema: $ref: '#/definitions/State' examples: [{"__class__": "Review", "created_at": "2017-03-25T02:17:07", "id": "3f54d114-582d-4dab-8559-f0682dbf1fa6", "place_id": "279b355e-ff9a-4b85-8114-6db7ad2a4cd2", "text": "Really nice place and really nice people. Secluded.", "updated_at": "2017-03-25T02:17:07", "user_id": "887dcd8d-d5ee-48de-9626-73ff4ea732fa"}] """ place = storage.get("Place", place_id) if place is None: abort(404) reviews = [review.to_json() for review in place.reviews] return jsonify(reviews)
def view_amenity(amenity_id=None): """Example endpoint returning a list of all amenities or of one specified Retrieves a list of all amenties or of one specified by amenity_id --- parameters: - name: amenity_id in: path type: string enum: ["all", cf701d1a-3c19-4bac-bd99-15321f1140f2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ if amenity_id is None: all_amenities = [state.to_json() for state in storage.all("Amenity").values()] return jsonify(all_amenities) s = storage.get("Amenity", amenity_id) if s is None: abort(404) return jsonify(s.to_json())
def update_amenity(amenity_id=None): """Example endpoint updates an amenity Updates an amenity based on amenity_id with the JSON body --- definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dict, the dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 a = storage.get("Amenity", amenity_id) if a is None: abort(404) for k in ("id", "created_at", "updated_at"): r.pop(k, None) for k, v in r.items(): setattr(a, k, v) a.save() return jsonify(a.to_json()), 200
def view_amenities_in_place(place_id): """Example endpoint returning a list of all amenities of a place Retrieves a list of all amenties specified by place_id --- parameters: - name: place_id in: path type: string enum: ["279b355e-ff9a-4b85-8114-6db7ad2a4cd2", None] required: true default: None definitions: Amenity: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dicts or dict, each dict is an amenity schema: $ref: '#/definitions/Amenity' examples: [{"__class__": "Amenity", "created_at": "2017-03-25T02:17:06", "id": "cf701d1a-3c19-4bac-bd99-15321f1140f2", "name": "Dog(s)", "updated_at": "2017-03-25T02:17:06"}] """ place = storage.get("Place", place_id) if place is None: abort(404) result = [p.to_json() for p in place.amenities] return jsonify(result)
def one_city(city_id): """Example endpoint returning one city Retrieves one city of a given city_id --- parameters: - name: city_id in: path type: string enum: ['None', '1da255c0-f023-4779-8134-2b1b40f87683'] required: true default: None definitions: City: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the city name: type: string description: name of the city state_id: type: string description: the id of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dictionary of a city object schema: $ref: '#/definitions/City' examples: [{"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "1da255c0-f023-4779-8134-2b1b40f87683", "name": "New Orleans", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}] """ city = storage.get("City", city_id) if city is None: abort(404) return jsonify(city.to_json())
def view_one_state(state_id=None): """Example endpoint returning a list of one state Retrieves a state by a given id --- parameters: - name: state_id in: path type: string enum: ['None', '10098698-bace-4bfb-8c0a-6bae0f7f5b8f'] required: true default: None definitions: State: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the state name: type: string description: name of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of one dictionary of the desired State object schema: $ref: '#/definitions/State' examples: [{'__class__': 'State', 'created_at': '2017-03-25T02:17:06', 'id': '10098698-bace-4bfb-8c0a-6bae0f7f5b8f', 'name': 'Oregon', 'updated_at': '2017-03-25T02:17:06'}] """ if state_id is None: abort(404) state = storage.get("State", state_id) if state is None: abort(404) return jsonify(state.to_json())
def update_one_city(city_id): """Example endpoint updates one city Updates one city tied with the given state_id based on the JSON body --- parameters: - name: city_id in: path type: string enum: ['None', "1da255c0-f023-4779-8134-2b1b40f87683"] required: true default: None definitions: City: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the city name: type: string description: name of the city state_id: type: string description: the id of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dictionary of a city object schema: $ref: '#/definitions/City' examples: [{"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "1da255c0-f023-4779-8134-2b1b40f87683", "name": "New Orleans", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}] """ city = storage.get("City", city_id) if city is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 for k in ("id", "created_at", "updated_at", "state_id"): r.pop(k, None) for k, v in r.items(): setattr(city, k, v) city.save() return jsonify(city.to_json()), 200
def update_user(user_id=None): """Example endpoint updates a user Updates a user based on the JSON body --- parameters: - name: user_id in: path type: string enum: ["32c11d3d-99a1-4406-ab41-7b6ccb7dd760"] required: true default: "32c11d3d-99a1-4406-ab41-7b6ccb7dd760" definitions: User: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dictionary, each dict is a user schema: $ref: '#/definitions/User' examples: [{"__class__": "User", "_password": "******", "created_at": "2017-03-25T02:17:06", "email": "*****@*****.**", "first_name": "Susan", "id": "32c11d3d-99a1-4406-ab41-7b6ccb7dd760", "last_name": "Finney", "updated_at": "2017-03-25T02:17:06"}] """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 a = storage.get("User", user_id) if a is None: abort(404) for k in ("id", "email", "created_at", "updated_at"): r.pop(k, None) for k, v in r.items(): setattr(a, k, v) a.save() return jsonify(a.to_json()), 200
def view_user(user_id=None): """Example endpoint returning a list of all users or of one specified Retrieves a list of all users or of one specified by user_id --- parameters: - name: user_id in: path type: string enum: ["all", "32c11d3d-99a1-4406-ab41-7b6ccb7dd760"] required: true default: None definitions: User: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created email: type: string first_name: type: string last_name: type: string id: type: string description: the id of the user updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of dictionarys or dictionary, each dict is a user schema: $ref: '#/definitions/User' examples: [{"__class__": "User", "_password": "******", "created_at": "2017-03-25T02:17:06", "email": "*****@*****.**", "first_name": "Susan", "id": "32c11d3d-99a1-4406-ab41-7b6ccb7dd760", "last_name": "Finney", "updated_at": "2017-03-25T02:17:06"}] """ if user_id is None: all_users = [state.to_json() for state in storage.all("User").values()] return jsonify(all_users) s = storage.get("User", user_id) if s is None: abort(404) return jsonify(s.to_json())
def create_one_city(state_id): """Example endpoint creating one city Creates one city tied with the given state_id based on the JSON body --- parameters: - name: city_id in: path type: string enum: ['None', '10098698-bace-4bfb-8c0a-6bae0f7f5b8f'] required: true default: None definitions: City: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the city name: type: string description: name of the city state_id: type: string description: the id of the state updated_at: type: string description: The date the object was updated items: $ref: '#/definitions/Color' Color: type: string responses: 201: description: A list of a dictionary of a city object schema: $ref: '#/definitions/City' examples: [{"__class__": "City", "created_at": "2017-03-25T02:17:06", "id": "1da255c0-f023-4779-8134-2b1b40f87683", "name": "New Orleans", "state_id": "2b9a4627-8a9e-4f32-a752-9a84fa7f4efd", "updated_at": "2017-03-25T02:17:06"}] """ try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 if 'name' not in r.keys(): return "Missing name", 400 s = storage.get("State", state_id) if s is None: abort(404) # creates the dictionary r as kwargs to create a city object c = City(**r) c.state_id = state_id c.save() return jsonify(c.to_json()), 201
def update_review(review_id): """Example endpoint creates one review Creates one review associated with a place_id based on the JSON body --- parameters: - name: place_id in: path type: string enum: ["3f54d114-582d-4dab-8559-f0682dbf1fa6"] required: true default: "3f54d114-582d-4dab-8559-f0682dbf1fa6" definitions: State: type: object properties: __class__: type: string description: The string of class object created_at: type: string description: The date the object created id: type: string description: the id of the review place_id: type: string description: the id of the place text: type: string description: written review updated_at: type: string description: The date the object was updated user_id: type: string description: The user id items: $ref: '#/definitions/Color' Color: type: string responses: 200: description: A list of a dictionary of a Review objects schema: $ref: '#/definitions/State' examples: [{"__class__": "Review", "created_at": "2017-03-25T02:17:07", "id": "3f54d114-582d-4dab-8559-f0682dbf1fa6", "place_id": "279b355e-ff9a-4b85-8114-6db7ad2a4cd2", "text": "Really nice place and really nice people. Secluded.", "updated_at": "2017-03-25T02:17:07", "user_id": "887dcd8d-d5ee-48de-9626-73ff4ea732fa"}] """ review = storage.get("Review", review_id) if review is None: abort(404) try: r = request.get_json() except: r = None if r is None: return "Not a JSON", 400 for k in ("id", "user_id", "place_id", "created_at", "updated_at"): r.pop(k, None) for key, value in r.items(): setattr(review, key, value) review.save() return jsonify(review.to_json()), 200