From 0e0c3809a0163bc9e78f5689a9145452a504827f Mon Sep 17 00:00:00 2001 From: Alexa Orrico Date: Fri, 1 May 2020 10:18:18 -0700 Subject: [PATCH 01/12] Fix shebang --- models/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/user.py b/models/user.py index 999cac209a6..36b1b70b994 100755 --- a/models/user.py +++ b/models/user.py @@ -1,4 +1,4 @@ -#!/usr/bin/python +#!/usr/bin/python3 """ holds class User""" import models from models.base_model import BaseModel, Base From c6e1d492eab1b6cbd464d242d8e5017fa2397e5a Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:01:43 +0200 Subject: [PATCH 02/12] task 1 --- README.md | 1 + models/engine/db_storage.py | 30 ++++++++++++++++++++++++++++++ models/engine/file_storage.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/README.md b/README.md index f1d72de6355..c055a5b27a3 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ No known bugs at this time. ## Authors Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twitter.com/alexa_orrico) Jennifer Huang - [Github](https://github.com/jhuang10123) / [Twitter](https://twitter.com/earthtojhuang) +Nigel Masiyazi-Ngorima - [Gitgub](https://github.com/NigelT97) / [Twitter](https://twitter.com/pstarnigel) Second part of Airbnb: Joann Vuong ## License diff --git a/models/engine/db_storage.py b/models/engine/db_storage.py index b8e7d291e6f..521e47328fd 100755 --- a/models/engine/db_storage.py +++ b/models/engine/db_storage.py @@ -74,3 +74,33 @@ def reload(self): def close(self): """call remove() method on the private session attribute""" self.__session.remove() + + def get(self, cls, id): + """ + Returns the object based on the class name and its ID, or + None if not found + """ + if cls not in classes.values(): + return None + + all_cls = models.storage.all(cls) + for value in all_cls.values(): + if (value.id == id): + return value + + return None + + def count(self, cls=None): + """ + count the number of objects in storage + """ + all_class = classes.values() + + if not cls: + count = 0 + for clas in all_class: + count += len(models.storage.all(clas).values()) + else: + count = len(models.storage.all(cls).values()) + + return count diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index c8cb8c1764d..4dd13c82269 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -68,3 +68,33 @@ def delete(self, obj=None): def close(self): """call reload() method for deserializing the JSON file to objects""" self.reload() + + def get(self, cls, id): + """ + Returns the object based on the class name and its ID, or + None if not found + """ + if cls not in classes.values(): + return None + + all_cls = models.storage.all(cls) + for value in all_cls.values(): + if (value.id == id): + return value + + return None + + def count(self, cls=None): + """ + count the number of objects in storage + """ + all_class = classes.values() + + if not cls: + count = 0 + for clas in all_class: + count += len(models.storage.all(clas).values()) + else: + count = len(models.storage.all(cls).values()) + + return count \ No newline at end of file From fdc14914863ad744514a78245864a27826be563d Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:07:35 +0200 Subject: [PATCH 03/12] test and corrections on task 1 --- models/engine/file_storage.py | 1 + .../test_engine/test_db_storage.py | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/models/engine/file_storage.py b/models/engine/file_storage.py index 4dd13c82269..78c51863d72 100755 --- a/models/engine/file_storage.py +++ b/models/engine/file_storage.py @@ -4,6 +4,7 @@ """ import json +import models from models.amenity import Amenity from models.base_model import BaseModel from models.city import City diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index 766e625b5af..fd4e389b7a5 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -18,6 +18,7 @@ import os import pep8 import unittest +from models import storage DBStorage = db_storage.DBStorage classes = {"Amenity": Amenity, "City": City, "Place": Place, "Review": Review, "State": State, "User": User} @@ -86,3 +87,24 @@ def test_new(self): @unittest.skipIf(models.storage_t != 'db', "not testing db storage") def test_save(self): """Test that save properly saves objects to file.json""" + + def test_get_db(self): + """ Tests method for obtaining an instance db storage""" + dic = {"name": "Cundinamarca"} + instance = State(**dic) + storage.new(instance) + storage.save() + get_instance = storage.get(State, instance.id) + self.assertEqual(get_instance, instance) + + def test_count(self): + """ Tests count method db storage """ + dic = {"name": "Vecindad"} + state = State(**dic) + storage.new(state) + dic = {"name": "Mexico", "state_id": state.id} + city = City(**dic) + storage.new(city) + storage.save() + c = storage.count() + self.assertEqual(len(storage.all()), c) From 014cba36649230a9eae7a5b419e0a265240d7cc8 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:08:21 +0200 Subject: [PATCH 04/12] adding comment --- tests/test_models/test_engine/test_db_storage.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index fd4e389b7a5..c2cf09ea944 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -16,7 +16,7 @@ from models.user import User import json import os -import pep8 +import pep8 # type: ignore import unittest from models import storage DBStorage = db_storage.DBStorage From 699d953c94d0064f36f31c40e9087eaf38ade80e Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:24:16 +0200 Subject: [PATCH 05/12] task 2 beginning of API --- api/__init__.py | 0 api/v1/__init__.py | 0 api/v1/app.py | 45 +++++++++++++++++++++++++++++++++++++++ api/v1/views/__init__.py | 10 +++++++++ api/v1/views/index.py | 46 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 101 insertions(+) create mode 100644 api/__init__.py create mode 100644 api/v1/__init__.py create mode 100644 api/v1/app.py create mode 100644 api/v1/views/__init__.py create mode 100644 api/v1/views/index.py diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/v1/__init__.py b/api/v1/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/v1/app.py b/api/v1/app.py new file mode 100644 index 00000000000..4058c7d4d61 --- /dev/null +++ b/api/v1/app.py @@ -0,0 +1,45 @@ +#!/usr/bin/python3 +""" +app +""" + +from flask import Flask, jsonify +from flask_cors import CORS # type: ignore +from os import getenv + +from api.v1.views import app_views +from models import storage + + +app = Flask(__name__) + +CORS(app, resources={r"/*": {"origins": "0.0.0.0"}}) + +app.register_blueprint(app_views) + + +@app.teardown_appcontext +def teardown(exception): + """ + teardown function + """ + storage.close() + + +@app.errorhandler(404) +def handle_404(exception): + """ + handles 404 error + :return: returns 404 json + """ + data = { + "error": "Not found" + } + + resp = jsonify(data) + resp.status_code = 404 + + return(resp) + +if __name__ == "__main__": + app.run(getenv("HBNB_API_HOST"), getenv("HBNB_API_PORT")) \ No newline at end of file diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py new file mode 100644 index 00000000000..eaf68be95a3 --- /dev/null +++ b/api/v1/views/__init__.py @@ -0,0 +1,10 @@ +#!/usr/bin/python3 +""" +views +""" + +from flask import Blueprint + +app_views = Blueprint('/api/v1', __name__, url_prefix="/api/v1") + +from api.v1.views.index import * \ No newline at end of file diff --git a/api/v1/views/index.py b/api/v1/views/index.py new file mode 100644 index 00000000000..ed4712744ed --- /dev/null +++ b/api/v1/views/index.py @@ -0,0 +1,46 @@ +#!/usr/bin/python3 +""" +index +""" + +from flask import jsonify +from api.v1.views import app_views + +from models import storage + + +@app_views.route("/status", methods=['GET'], strict_slashes=False) +def status(): + """ + status route + :return: response with json + """ + data = { + "status": "OK" + } + + resp = jsonify(data) + resp.status_code = 200 + + return resp + + +@app_views.route("/stats", methods=['GET'], strict_slashes=False) +def stats(): + """ + stats of all objs route + :return: json of all objs + """ + data = { + "amenities": storage.count("Amenity"), + "cities": storage.count("City"), + "places": storage.count("Place"), + "reviews": storage.count("Review"), + "states": storage.count("State"), + "users": storage.count("User"), + } + + resp = jsonify(data) + resp.status_code = 200 + + return resp \ No newline at end of file From d54a731964833e3643fa7238a2b7fef87e061cb4 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:28:51 +0200 Subject: [PATCH 06/12] states added --- api/v1/views/__init__.py | 3 +- api/v1/views/states.py | 97 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 api/v1/views/states.py diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py index eaf68be95a3..ccb3c521537 100644 --- a/api/v1/views/__init__.py +++ b/api/v1/views/__init__.py @@ -7,4 +7,5 @@ app_views = Blueprint('/api/v1', __name__, url_prefix="/api/v1") -from api.v1.views.index import * \ No newline at end of file +from api.v1.views.index import * +from api.v1.views.states import * \ No newline at end of file diff --git a/api/v1/views/states.py b/api/v1/views/states.py new file mode 100644 index 00000000000..6c238c6f5cf --- /dev/null +++ b/api/v1/views/states.py @@ -0,0 +1,97 @@ +#!/usr/bin/python3 +""" +route for handling State objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.state import State + + +@app_views.route("/states", methods=["GET"], strict_slashes=False) +def state_get_all(): + """ + retrieves all State objects + :return: json of all states + """ + state_list = [] + state_obj = storage.all("State") + for obj in state_obj.values(): + state_list.append(obj.to_json()) + + return jsonify(state_list) + + +@app_views.route("/states", methods=["POST"], strict_slashes=False) +def state_create(): + """ + create state route + :return: newly created state obj + """ + state_json = request.get_json(silent=True) + if state_json is None: + abort(400, 'Not a JSON') + if "name" not in state_json: + abort(400, 'Missing name') + + new_state = State(**state_json) + new_state.save() + resp = jsonify(new_state.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/states/", methods=["GET"], strict_slashes=False) +def state_by_id(state_id): + """ + gets a specific State object by ID + :param state_id: state object id + :return: state obj with the specified id or error + """ + + fetched_obj = storage.get("State", str(state_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/states/", methods=["PUT"], strict_slashes=False) +def state_put(state_id): + """ + updates specific State object by ID + :param state_id: state object ID + :return: state object and 200 on success, or 400 or 404 on failure + """ + state_json = request.get_json(silent=True) + if state_json is None: + abort(400, 'Not a JSON') + fetched_obj = storage.get("State", str(state_id)) + if fetched_obj is None: + abort(404) + for key, val in state_json.items(): + if key not in ["id", "created_at", "updated_at"]: + setattr(fetched_obj, key, val) + fetched_obj.save() + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/states/", methods=["DELETE"], + strict_slashes=False) +def state_delete_by_id(state_id): + """ + deletes State by id + :param state_id: state object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("State", str(state_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file From 10fc0ab5a0c43755d574a35182d6433675922822 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:39:13 +0200 Subject: [PATCH 07/12] complete main task --- api/v1/views/__init__.py | 7 +- api/v1/views/amenities.py | 99 +++++++++++++++++++++++++++ api/v1/views/cities.py | 110 ++++++++++++++++++++++++++++++ api/v1/views/places.py | 115 +++++++++++++++++++++++++++++++ api/v1/views/places_reviews.py | 120 +++++++++++++++++++++++++++++++++ api/v1/views/users.py | 104 ++++++++++++++++++++++++++++ 6 files changed, 554 insertions(+), 1 deletion(-) create mode 100644 api/v1/views/amenities.py create mode 100644 api/v1/views/cities.py create mode 100644 api/v1/views/places.py create mode 100644 api/v1/views/places_reviews.py create mode 100644 api/v1/views/users.py diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py index ccb3c521537..f2c503616d3 100644 --- a/api/v1/views/__init__.py +++ b/api/v1/views/__init__.py @@ -8,4 +8,9 @@ app_views = Blueprint('/api/v1', __name__, url_prefix="/api/v1") from api.v1.views.index import * -from api.v1.views.states import * \ No newline at end of file +from api.v1.views.states import * +from api.v1.views.cities import * +from api.v1.views.places import * +from api.v1.views.places_reviews import * +from api.v1.views.users import * +from api.v1.views.amenities import * \ No newline at end of file diff --git a/api/v1/views/amenities.py b/api/v1/views/amenities.py new file mode 100644 index 00000000000..d0711197c12 --- /dev/null +++ b/api/v1/views/amenities.py @@ -0,0 +1,99 @@ +#!/usr/bin/python3 +""" +route for handling Amenity objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.amenity import Amenity + + +@app_views.route("/amenities", methods=["GET"], strict_slashes=False) +def amenity_get_all(): + """ + retrieves all Amenity objects + :return: json of all states + """ + am_list = [] + am_obj = storage.all("Amenity") + for obj in am_obj.values(): + am_list.append(obj.to_json()) + + return jsonify(am_list) + + +@app_views.route("/amenities", methods=["POST"], strict_slashes=False) +def amenity_create(): + """ + create amenity route + :return: newly created amenity obj + """ + am_json = request.get_json(silent=True) + if am_json is None: + abort(400, 'Not a JSON') + if "name" not in am_json: + abort(400, 'Missing name') + + new_am = Amenity(**am_json) + new_am.save() + resp = jsonify(new_am.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/amenities/", methods=["GET"], + strict_slashes=False) +def amenity_by_id(amenity_id): + """ + gets a specific Amenity object by ID + :param amenity_id: amenity object id + :return: state obj with the specified id or error + """ + + fetched_obj = storage.get("Amenity", str(amenity_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/amenities/", methods=["PUT"], + strict_slashes=False) +def amenity_put(amenity_id): + """ + updates specific Amenity object by ID + :param amenity_id: amenity object ID + :return: amenity object and 200 on success, or 400 or 404 on failure + """ + am_json = request.get_json(silent=True) + if am_json is None: + abort(400, 'Not a JSON') + fetched_obj = storage.get("Amenity", str(amenity_id)) + if fetched_obj is None: + abort(404) + for key, val in am_json.items(): + if key not in ["id", "created_at", "updated_at"]: + setattr(fetched_obj, key, val) + fetched_obj.save() + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/amenities/", methods=["DELETE"], + strict_slashes=False) +def amenity_delete_by_id(amenity_id): + """ + deletes Amenity by id + :param amenity_id: Amenity object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("Amenity", str(amenity_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file diff --git a/api/v1/views/cities.py b/api/v1/views/cities.py new file mode 100644 index 00000000000..c8776aec3e5 --- /dev/null +++ b/api/v1/views/cities.py @@ -0,0 +1,110 @@ +#!/usr/bin/python3 +""" +route for handling State objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.city import City + + +@app_views.route("/states//cities", methods=["GET"], + strict_slashes=False) +def city_by_state(state_id): + """ + retrieves all City objects from a specific state + :return: json of all cities in a state or 404 on error + """ + city_list = [] + state_obj = storage.get("State", state_id) + + if state_obj is None: + abort(404) + for obj in state_obj.cities: + city_list.append(obj.to_json()) + + return jsonify(city_list) + + +@app_views.route("/states//cities", methods=["POST"], + strict_slashes=False) +def city_create(state_id): + """ + create city route + param: state_id - state id + :return: newly created city obj + """ + city_json = request.get_json(silent=True) + if city_json is None: + abort(400, 'Not a JSON') + + if not storage.get("State", str(state_id)): + abort(404) + + if "name" not in city_json: + abort(400, 'Missing name') + + city_json["state_id"] = state_id + + new_city = City(**city_json) + new_city.save() + resp = jsonify(new_city.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/cities/", methods=["GET"], + strict_slashes=False) +def city_by_id(city_id): + """ + gets a specific City object by ID + :param city_id: city object id + :return: city obj with the specified id or error + """ + + fetched_obj = storage.get("City", str(city_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("cities/", methods=["PUT"], strict_slashes=False) +def city_put(city_id): + """ + updates specific City object by ID + :param city_id: city object ID + :return: city object and 200 on success, or 400 or 404 on failure + """ + city_json = request.get_json(silent=True) + if city_json is None: + abort(400, 'Not a JSON') + fetched_obj = storage.get("City", str(city_id)) + if fetched_obj is None: + abort(404) + for key, val in city_json.items(): + if key not in ["id", "created_at", "updated_at", "state_id"]: + setattr(fetched_obj, key, val) + fetched_obj.save() + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/cities/", methods=["DELETE"], + strict_slashes=False) +def city_delete_by_id(city_id): + """ + deletes City by id + :param city_id: city object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("City", str(city_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file diff --git a/api/v1/views/places.py b/api/v1/views/places.py new file mode 100644 index 00000000000..ef71546da40 --- /dev/null +++ b/api/v1/views/places.py @@ -0,0 +1,115 @@ +#!/usr/bin/python3 +""" +route for handling Place objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.place import Place + + +@app_views.route("/cities//places", methods=["GET"], + strict_slashes=False) +def places_by_city(city_id): + """ + retrieves all Place objects by city + :return: json of all Places + """ + place_list = [] + city_obj = storage.get("City", str(city_id)) + for obj in city_obj.places: + place_list.append(obj.to_json()) + + return jsonify(place_list) + + +@app_views.route("/cities//places", methods=["POST"], + strict_slashes=False) +def place_create(city_id): + """ + create place route + :return: newly created Place obj + """ + place_json = request.get_json(silent=True) + if place_json is None: + abort(400, 'Not a JSON') + if not storage.get("User", place_json["user_id"]): + abort(404) + if not storage.get("City", city_id): + abort(404) + if "user_id" not in place_json: + abort(400, 'Missing user_id') + if "name" not in place_json: + abort(400, 'Missing name') + + place_json["city_id"] = city_id + + new_place = Place(**place_json) + new_place.save() + resp = jsonify(new_place.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/places/", methods=["GET"], + strict_slashes=False) +def place_by_id(place_id): + """ + gets a specific Place object by ID + :param place_id: place object id + :return: place obj with the specified id or error + """ + + fetched_obj = storage.get("Place", str(place_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/places/", methods=["PUT"], + strict_slashes=False) +def place_put(place_id): + """ + updates specific Place object by ID + :param place_id: Place object ID + :return: Place object and 200 on success, or 400 or 404 on failure + """ + place_json = request.get_json(silent=True) + + if place_json is None: + abort(400, 'Not a JSON') + + fetched_obj = storage.get("Place", str(place_id)) + + if fetched_obj is None: + abort(404) + + for key, val in place_json.items(): + if key not in ["id", "created_at", "updated_at", "user_id", "city_id"]: + setattr(fetched_obj, key, val) + + fetched_obj.save() + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/places/", methods=["DELETE"], + strict_slashes=False) +def place_delete_by_id(place_id): + """ + deletes Place by id + :param place_id: Place object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("Place", str(place_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file diff --git a/api/v1/views/places_reviews.py b/api/v1/views/places_reviews.py new file mode 100644 index 00000000000..564fb412d1c --- /dev/null +++ b/api/v1/views/places_reviews.py @@ -0,0 +1,120 @@ +#!/usr/bin/python3 +""" +route for handling Review objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.review import Review + + +@app_views.route("/places//reviews", methods=["GET"], + strict_slashes=False) +def reviews_by_place(place_id): + """ + retrieves all Review objects by place + :return: json of all reviews + """ + review_list = [] + place_obj = storage.get("Place", str(place_id)) + + if place_obj is None: + abort(404) + + for obj in place_obj.reviews: + review_list.append(obj.to_json()) + + return jsonify(review_list) + + +@app_views.route("/places//reviews", methods=["POST"], + strict_slashes=False) +def review_create(place_id): + """ + create REview route + :return: newly created Review obj + """ + review_json = request.get_json(silent=True) + if review_json is None: + abort(400, 'Not a JSON') + if not storage.get("Place", place_id): + abort(404) + if not storage.get("User", review_json["user_id"]): + abort(404) + if "user_id" not in review_json: + abort(400, 'Missing user_id') + if "text" not in review_json: + abort(400, 'Missing text') + + review_json["place_id"] = place_id + + new_review = Review(**review_json) + new_review.save() + resp = jsonify(new_review.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/reviews/", methods=["GET"], + strict_slashes=False) +def review_by_id(review_id): + """ + gets a specific Review object by ID + :param review_id: place object id + :return: review obj with the specified id or error + """ + + fetched_obj = storage.get("Review", str(review_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/reviews/", methods=["PUT"], + strict_slashes=False) +def review_put(review_id): + """ + updates specific Review object by ID + :param review_id: Review object ID + :return: Review object and 200 on success, or 400 or 404 on failure + """ + place_json = request.get_json(silent=True) + + if place_json is None: + abort(400, 'Not a JSON') + + fetched_obj = storage.get("Review", str(review_id)) + + if fetched_obj is None: + abort(404) + + for key, val in place_json.items(): + if key not in ["id", "created_at", "updated_at", "user_id", + "place_id"]: + setattr(fetched_obj, key, val) + + fetched_obj.save() + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/reviews/", methods=["DELETE"], + strict_slashes=False) +def review_delete_by_id(review_id): + """ + deletes Review by id + :param : Review object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("Review", str(review_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file diff --git a/api/v1/views/users.py b/api/v1/views/users.py new file mode 100644 index 00000000000..df10b7a3d95 --- /dev/null +++ b/api/v1/views/users.py @@ -0,0 +1,104 @@ +#!/usr/bin/python3 +""" +route for handling User objects and operations +""" +from flask import jsonify, abort, request +from api.v1.views import app_views, storage +from models.user import User + + +@app_views.route("/users", methods=["GET"], strict_slashes=False) +def user_get_all(): + """ + retrieves all User objects + :return: json of all users + """ + user_list = [] + user_obj = storage.all("User") + for obj in user_obj.values(): + user_list.append(obj.to_json()) + + return jsonify(user_list) + + +@app_views.route("/users", methods=["POST"], strict_slashes=False) +def user_create(): + """ + create user route + :return: newly created user obj + """ + user_json = request.get_json(silent=True) + if user_json is None: + abort(400, 'Not a JSON') + if "email" not in user_json: + abort(400, 'Missing email') + if "password" not in user_json: + abort(400, 'Missing password') + + new_user = User(**user_json) + new_user.save() + resp = jsonify(new_user.to_json()) + resp.status_code = 201 + + return resp + + +@app_views.route("/users/", methods=["GET"], strict_slashes=False) +def user_by_id(user_id): + """ + gets a specific User object by ID + :param user_id: user object id + :return: user obj with the specified id or error + """ + + fetched_obj = storage.get("User", str(user_id)) + + if fetched_obj is None: + abort(404) + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/users/", methods=["PUT"], strict_slashes=False) +def user_put(user_id): + """ + updates specific User object by ID + :param user_id: user object ID + :return: user object and 200 on success, or 400 or 404 on failure + """ + user_json = request.get_json(silent=True) + + if user_json is None: + abort(400, 'Not a JSON') + + fetched_obj = storage.get("User", str(user_id)) + + if fetched_obj is None: + abort(404) + + for key, val in user_json.items(): + if key not in ["id", "created_at", "updated_at", "email"]: + setattr(fetched_obj, key, val) + + fetched_obj.save() + + return jsonify(fetched_obj.to_json()) + + +@app_views.route("/users/", methods=["DELETE"], strict_slashes=False) +def user_delete_by_id(user_id): + """ + deletes User by id + :param user_id: user object id + :return: empty dict with 200 or 404 if not found + """ + + fetched_obj = storage.get("User", str(user_id)) + + if fetched_obj is None: + abort(404) + + storage.delete(fetched_obj) + storage.save() + + return jsonify({}) \ No newline at end of file From 767f7e30e5be3b4f74974398981790ecdc98917e Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:42:53 +0200 Subject: [PATCH 08/12] advance done --- api/v1/views/__init__.py | 3 +- api/v1/views/places_amenities.py | 105 +++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 api/v1/views/places_amenities.py diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py index f2c503616d3..1e2baf915a2 100644 --- a/api/v1/views/__init__.py +++ b/api/v1/views/__init__.py @@ -13,4 +13,5 @@ from api.v1.views.places import * from api.v1.views.places_reviews import * from api.v1.views.users import * -from api.v1.views.amenities import * \ No newline at end of file +from api.v1.views.amenities import * +from api.v1.views.places_amenities import * \ No newline at end of file diff --git a/api/v1/views/places_amenities.py b/api/v1/views/places_amenities.py new file mode 100644 index 00000000000..ca94e81f442 --- /dev/null +++ b/api/v1/views/places_amenities.py @@ -0,0 +1,105 @@ +#!/usr/bin/python3 +""" +route for handling place and amenities linking +""" +from flask import jsonify, abort +from os import getenv + +from api.v1.views import app_views, storage + + +@app_views.route("/places//amenities", + methods=["GET"], + strict_slashes=False) +def amenity_by_place(place_id): + """ + get all amenities of a place + :param place_id: amenity id + :return: all amenities + """ + fetched_obj = storage.get("Place", str(place_id)) + + all_amenities = [] + + if fetched_obj is None: + abort(404) + + for obj in fetched_obj.amenities: + all_amenities.append(obj.to_json()) + + return jsonify(all_amenities) + + +@app_views.route("/places//amenities/", + methods=["DELETE"], + strict_slashes=False) +def unlink_amenity_from_place(place_id, amenity_id): + """ + unlinks an amenity in a place + :param place_id: place id + :param amenity_id: amenity id + :return: empty dict or error + """ + if not storage.get("Place", str(place_id)): + abort(404) + if not storage.get("Amenity", str(amenity_id)): + abort(404) + + fetched_obj = storage.get("Place", place_id) + found = 0 + + for obj in fetched_obj.amenities: + if str(obj.id) == amenity_id: + if getenv("HBNB_TYPE_STORAGE") == "db": + fetched_obj.amenities.remove(obj) + else: + fetched_obj.amenity_ids.remove(obj.id) + fetched_obj.save() + found = 1 + break + + if found == 0: + abort(404) + else: + resp = jsonify({}) + resp.status_code = 201 + return resp + + +@app_views.route("/places//amenities/", + methods=["POST"], + strict_slashes=False) +def link_amenity_to_place(place_id, amenity_id): + """ + links a amenity with a place + :param place_id: place id + :param amenity_id: amenity id + :return: return Amenity obj added or error + """ + + fetched_obj = storage.get("Place", str(place_id)) + amenity_obj = storage.get("Amenity", str(amenity_id)) + found_amenity = None + + if not fetched_obj or not amenity_obj: + abort(404) + + for obj in fetched_obj.amenities: + if str(obj.id) == amenity_id: + found_amenity = obj + break + + if found_amenity is not None: + return jsonify(found_amenity.to_json()) + + if getenv("HBNB_TYPE_STORAGE") == "db": + fetched_obj.amenities.append(amenity_obj) + else: + fetched_obj.amenities = amenity_obj + + fetched_obj.save() + + resp = jsonify(amenity_obj.to_json()) + resp.status_code = 201 + + return resp \ No newline at end of file From 5ddc525ed5ba080c6ff33872a5aaba4b91be8677 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:46:15 +0200 Subject: [PATCH 09/12] corrections --- api/v1/views/places_amenities.py | 138 ++++++++++++++----------------- 1 file changed, 64 insertions(+), 74 deletions(-) diff --git a/api/v1/views/places_amenities.py b/api/v1/views/places_amenities.py index ca94e81f442..6a8246eb60e 100644 --- a/api/v1/views/places_amenities.py +++ b/api/v1/views/places_amenities.py @@ -1,105 +1,95 @@ #!/usr/bin/python3 -""" -route for handling place and amenities linking -""" -from flask import jsonify, abort -from os import getenv +""" objects that handle all default RestFul API actions for Place - Amenity """ +from models.place import Place +from models.amenity import Amenity +from models import storage +from api.v1.views import app_views +from os import environ +from flask import abort, jsonify, make_response, request +from flasgger.utils import swag_from # type: ignore -from api.v1.views import app_views, storage - -@app_views.route("/places//amenities", - methods=["GET"], +@app_views.route('places//amenities', methods=['GET'], strict_slashes=False) -def amenity_by_place(place_id): +@swag_from('documentation/place_amenity/get_places_amenities.yml', + methods=['GET']) +def get_place_amenities(place_id): """ - get all amenities of a place - :param place_id: amenity id - :return: all amenities + Retrieves the list of all Amenity objects of a Place """ - fetched_obj = storage.get("Place", str(place_id)) - - all_amenities = [] + place = storage.get(Place, place_id) - if fetched_obj is None: + if not place: abort(404) - for obj in fetched_obj.amenities: - all_amenities.append(obj.to_json()) + if environ.get('HBNB_TYPE_STORAGE') == "db": + amenities = [amenity.to_dict() for amenity in place.amenities] + else: + amenities = [storage.get(Amenity, amenity_id).to_dict() + for amenity_id in place.amenity_ids] - return jsonify(all_amenities) + return jsonify(amenities) -@app_views.route("/places//amenities/", - methods=["DELETE"], - strict_slashes=False) -def unlink_amenity_from_place(place_id, amenity_id): +@app_views.route('/places//amenities/', + methods=['DELETE'], strict_slashes=False) +@swag_from('documentation/place_amenity/delete_place_amenities.yml', + methods=['DELETE']) +def delete_place_amenity(place_id, amenity_id): """ - unlinks an amenity in a place - :param place_id: place id - :param amenity_id: amenity id - :return: empty dict or error + Deletes a Amenity object of a Place """ - if not storage.get("Place", str(place_id)): - abort(404) - if not storage.get("Amenity", str(amenity_id)): - abort(404) + place = storage.get(Place, place_id) - fetched_obj = storage.get("Place", place_id) - found = 0 + if not place: + abort(404) - for obj in fetched_obj.amenities: - if str(obj.id) == amenity_id: - if getenv("HBNB_TYPE_STORAGE") == "db": - fetched_obj.amenities.remove(obj) - else: - fetched_obj.amenity_ids.remove(obj.id) - fetched_obj.save() - found = 1 - break + amenity = storage.get(Amenity, amenity_id) - if found == 0: + if not amenity: abort(404) + + if environ.get('HBNB_TYPE_STORAGE') == "db": + if amenity not in place.amenities: + abort(404) + place.amenities.remove(amenity) else: - resp = jsonify({}) - resp.status_code = 201 - return resp + if amenity_id not in place.amenity_ids: + abort(404) + place.amenity_ids.remove(amenity_id) + + storage.save() + return make_response(jsonify({}), 200) -@app_views.route("/places//amenities/", - methods=["POST"], +@app_views.route('/places//amenities/', methods=['POST'], strict_slashes=False) -def link_amenity_to_place(place_id, amenity_id): +@swag_from('documentation/place_amenity/post_place_amenities.yml', + methods=['POST']) +def post_place_amenity(place_id, amenity_id): """ - links a amenity with a place - :param place_id: place id - :param amenity_id: amenity id - :return: return Amenity obj added or error + Link a Amenity object to a Place """ + place = storage.get(Place, place_id) - fetched_obj = storage.get("Place", str(place_id)) - amenity_obj = storage.get("Amenity", str(amenity_id)) - found_amenity = None - - if not fetched_obj or not amenity_obj: + if not place: abort(404) - for obj in fetched_obj.amenities: - if str(obj.id) == amenity_id: - found_amenity = obj - break + amenity = storage.get(Amenity, amenity_id) - if found_amenity is not None: - return jsonify(found_amenity.to_json()) + if not amenity: + abort(404) - if getenv("HBNB_TYPE_STORAGE") == "db": - fetched_obj.amenities.append(amenity_obj) + if environ.get('HBNB_TYPE_STORAGE') == "db": + if amenity in place.amenities: + return make_response(jsonify(amenity.to_dict()), 200) + else: + place.amenities.append(amenity) else: - fetched_obj.amenities = amenity_obj - - fetched_obj.save() - - resp = jsonify(amenity_obj.to_json()) - resp.status_code = 201 + if amenity_id in place.amenity_ids: + return make_response(jsonify(amenity.to_dict()), 200) + else: + place.amenity_ids.append(amenity_id) - return resp \ No newline at end of file + storage.save() + return make_response(jsonify(amenity.to_dict()), 201) \ No newline at end of file From 8cca4093e788db080dfd4a197c81c986ebdec87d Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:48:40 +0200 Subject: [PATCH 10/12] executables --- api/v1/views/__init__.py | 0 api/v1/views/amenities.py | 0 api/v1/views/cities.py | 0 api/v1/views/index.py | 0 api/v1/views/places.py | 0 api/v1/views/places_amenities.py | 0 api/v1/views/places_reviews.py | 0 api/v1/views/states.py | 0 api/v1/views/users.py | 0 9 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 api/v1/views/__init__.py mode change 100644 => 100755 api/v1/views/amenities.py mode change 100644 => 100755 api/v1/views/cities.py mode change 100644 => 100755 api/v1/views/index.py mode change 100644 => 100755 api/v1/views/places.py mode change 100644 => 100755 api/v1/views/places_amenities.py mode change 100644 => 100755 api/v1/views/places_reviews.py mode change 100644 => 100755 api/v1/views/states.py mode change 100644 => 100755 api/v1/views/users.py diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py old mode 100644 new mode 100755 diff --git a/api/v1/views/amenities.py b/api/v1/views/amenities.py old mode 100644 new mode 100755 diff --git a/api/v1/views/cities.py b/api/v1/views/cities.py old mode 100644 new mode 100755 diff --git a/api/v1/views/index.py b/api/v1/views/index.py old mode 100644 new mode 100755 diff --git a/api/v1/views/places.py b/api/v1/views/places.py old mode 100644 new mode 100755 diff --git a/api/v1/views/places_amenities.py b/api/v1/views/places_amenities.py old mode 100644 new mode 100755 diff --git a/api/v1/views/places_reviews.py b/api/v1/views/places_reviews.py old mode 100644 new mode 100755 diff --git a/api/v1/views/states.py b/api/v1/views/states.py old mode 100644 new mode 100755 diff --git a/api/v1/views/users.py b/api/v1/views/users.py old mode 100644 new mode 100755 From 6ea5ec9f28632839b7007172b325aca0b88bf212 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 15:54:36 +0200 Subject: [PATCH 11/12] corrections 2 --- api/v1/views/__init__.py | 24 +++---- api/v1/views/amenities.py | 144 ++++++++++++++++---------------------- 2 files changed, 71 insertions(+), 97 deletions(-) diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py index 1e2baf915a2..b90fb4bd6de 100755 --- a/api/v1/views/__init__.py +++ b/api/v1/views/__init__.py @@ -1,17 +1,15 @@ #!/usr/bin/python3 -""" -views -""" - +"""create blueprint""" from flask import Blueprint -app_views = Blueprint('/api/v1', __name__, url_prefix="/api/v1") +app_views = Blueprint('app_views', __name__, url_prefix='/api/v1') -from api.v1.views.index import * -from api.v1.views.states import * -from api.v1.views.cities import * -from api.v1.views.places import * -from api.v1.views.places_reviews import * -from api.v1.views.users import * -from api.v1.views.amenities import * -from api.v1.views.places_amenities import * \ No newline at end of file +if app_views is not None: + from api.v1.views.index import * + from api.v1.views.states import * + from api.v1.views.cities import * + from api.v1.views.amenities import * + from api.v1.views.users import * + from api.v1.views.places import * + from api.v1.views.places_reviews import * + from api.v1.views.places_amenities import * \ No newline at end of file diff --git a/api/v1/views/amenities.py b/api/v1/views/amenities.py index d0711197c12..1b4bcc9f95e 100755 --- a/api/v1/views/amenities.py +++ b/api/v1/views/amenities.py @@ -1,99 +1,75 @@ #!/usr/bin/python3 -""" -route for handling Amenity objects and operations -""" +"""amenities""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage from models.amenity import Amenity +from datetime import datetime +import uuid -@app_views.route("/amenities", methods=["GET"], strict_slashes=False) -def amenity_get_all(): - """ - retrieves all Amenity objects - :return: json of all states - """ - am_list = [] - am_obj = storage.all("Amenity") - for obj in am_obj.values(): - am_list.append(obj.to_json()) +@app_views.route('/amenities/', methods=['GET']) +def list_amenities(): + '''Retrieves a list of all Amenity objects''' + list_amenities = [obj.to_dict() for obj in storage.all("Amenity").values()] + return jsonify(list_amenities) - return jsonify(am_list) - -@app_views.route("/amenities", methods=["POST"], strict_slashes=False) -def amenity_create(): - """ - create amenity route - :return: newly created amenity obj - """ - am_json = request.get_json(silent=True) - if am_json is None: - abort(400, 'Not a JSON') - if "name" not in am_json: - abort(400, 'Missing name') - - new_am = Amenity(**am_json) - new_am.save() - resp = jsonify(new_am.to_json()) - resp.status_code = 201 - - return resp - - -@app_views.route("/amenities/", methods=["GET"], - strict_slashes=False) -def amenity_by_id(amenity_id): - """ - gets a specific Amenity object by ID - :param amenity_id: amenity object id - :return: state obj with the specified id or error - """ - - fetched_obj = storage.get("Amenity", str(amenity_id)) - - if fetched_obj is None: +@app_views.route('/amenities/', methods=['GET']) +def get_amenity(amenity_id): + '''Retrieves an Amenity object''' + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: abort(404) - - return jsonify(fetched_obj.to_json()) + return jsonify(amenity_obj[0]) -@app_views.route("/amenities/", methods=["PUT"], - strict_slashes=False) -def amenity_put(amenity_id): - """ - updates specific Amenity object by ID - :param amenity_id: amenity object ID - :return: amenity object and 200 on success, or 400 or 404 on failure - """ - am_json = request.get_json(silent=True) - if am_json is None: - abort(400, 'Not a JSON') - fetched_obj = storage.get("Amenity", str(amenity_id)) - if fetched_obj is None: +@app_views.route('/amenities/', methods=['DELETE']) +def delete_amenity(amenity_id): + '''Deletes an Amenity object''' + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: abort(404) - for key, val in am_json.items(): - if key not in ["id", "created_at", "updated_at"]: - setattr(fetched_obj, key, val) - fetched_obj.save() - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/amenities/", methods=["DELETE"], - strict_slashes=False) -def amenity_delete_by_id(amenity_id): - """ - deletes Amenity by id - :param amenity_id: Amenity object id - :return: empty dict with 200 or 404 if not found - """ + amenity_obj.remove(amenity_obj[0]) + for obj in all_amenities: + if obj.id == amenity_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/amenities/', methods=['POST']) +def create_amenity(): + '''Creates an Amenity''' + if not request.get_json(): + abort(400, 'Not a JSON') + if 'name' not in request.get_json(): + abort(400, 'Missing name') + amenities = [] + new_amenity = Amenity(name=request.json['name']) + storage.new(new_amenity) + storage.save() + amenities.append(new_amenity.to_dict()) + return jsonify(amenities[0]), 201 - fetched_obj = storage.get("Amenity", str(amenity_id)) - if fetched_obj is None: +@app_views.route('/amenities/', methods=['PUT']) +def updates_amenity(amenity_id): + '''Updates an Amenity object''' + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: abort(404) - - storage.delete(fetched_obj) + if not request.get_json(): + abort(400, 'Not a JSON') + amenity_obj[0]['name'] = request.json['name'] + for obj in all_amenities: + if obj.id == amenity_id: + obj.name = request.json['name'] storage.save() - - return jsonify({}) \ No newline at end of file + return jsonify(amenity_obj[0]), 200 \ No newline at end of file From af120d9af8714608ac6cd618d80a2175b2f45b82 Mon Sep 17 00:00:00 2001 From: NigelT97 Date: Sun, 26 May 2024 16:08:16 +0200 Subject: [PATCH 12/12] all corrections --- api/v1/views/cities.py | 162 +++++++++------------- api/v1/views/places.py | 204 ++++++++++++++------------- api/v1/views/places_amenities.py | 229 ++++++++++++++++++++----------- api/v1/views/places_reviews.py | 176 ++++++++++-------------- api/v1/views/states.py | 139 ++++++++----------- api/v1/views/users.py | 166 +++++++++++----------- 6 files changed, 528 insertions(+), 548 deletions(-) diff --git a/api/v1/views/cities.py b/api/v1/views/cities.py index c8776aec3e5..e25dfcfc51a 100755 --- a/api/v1/views/cities.py +++ b/api/v1/views/cities.py @@ -1,110 +1,84 @@ #!/usr/bin/python3 -""" -route for handling State objects and operations -""" +"""cities""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage from models.city import City - - -@app_views.route("/states//cities", methods=["GET"], - strict_slashes=False) -def city_by_state(state_id): - """ - retrieves all City objects from a specific state - :return: json of all cities in a state or 404 on error - """ - city_list = [] - state_obj = storage.get("State", state_id) - - if state_obj is None: +from models.state import State +from datetime import datetime +import uuid + + +@app_views.route('/states//cities', methods=['GET']) +@app_views.route('/states//cities/', methods=['GET']) +def list_cities_of_state(state_id): + '''Retrieves a list of all City objects''' + all_states = storage.all("State").values() + state_obj = [obj.to_dict() for obj in all_states if obj.id == state_id] + if state_obj == []: abort(404) - for obj in state_obj.cities: - city_list.append(obj.to_json()) + list_cities = [obj.to_dict() for obj in storage.all("City").values() + if state_id == obj.state_id] + return jsonify(list_cities) - return jsonify(city_list) - -@app_views.route("/states//cities", methods=["POST"], - strict_slashes=False) -def city_create(state_id): - """ - create city route - param: state_id - state id - :return: newly created city obj - """ - city_json = request.get_json(silent=True) - if city_json is None: +@app_views.route('/states//cities', methods=['POST']) +@app_views.route('/states//cities/', methods=['POST']) +def create_city(state_id): + '''Creates a City''' + if not request.get_json(): abort(400, 'Not a JSON') - - if not storage.get("State", str(state_id)): - abort(404) - - if "name" not in city_json: + if 'name' not in request.get_json(): abort(400, 'Missing name') - - city_json["state_id"] = state_id - - new_city = City(**city_json) - new_city.save() - resp = jsonify(new_city.to_json()) - resp.status_code = 201 - - return resp - - -@app_views.route("/cities/", methods=["GET"], - strict_slashes=False) -def city_by_id(city_id): - """ - gets a specific City object by ID - :param city_id: city object id - :return: city obj with the specified id or error - """ - - fetched_obj = storage.get("City", str(city_id)) - - if fetched_obj is None: + all_states = storage.all("State").values() + state_obj = [obj.to_dict() for obj in all_states if obj.id == state_id] + if state_obj == []: abort(404) - - return jsonify(fetched_obj.to_json()) + cities = [] + new_city = City(name=request.json['name'], state_id=state_id) + storage.new(new_city) + storage.save() + cities.append(new_city.to_dict()) + return jsonify(cities[0]), 201 -@app_views.route("cities/", methods=["PUT"], strict_slashes=False) -def city_put(city_id): - """ - updates specific City object by ID - :param city_id: city object ID - :return: city object and 200 on success, or 400 or 404 on failure - """ - city_json = request.get_json(silent=True) - if city_json is None: - abort(400, 'Not a JSON') - fetched_obj = storage.get("City", str(city_id)) - if fetched_obj is None: +@app_views.route('/cities/', methods=['GET']) +def get_city(city_id): + '''Retrieves a City object''' + all_cities = storage.all("City").values() + city_obj = [obj.to_dict() for obj in all_cities if obj.id == city_id] + if city_obj == []: abort(404) - for key, val in city_json.items(): - if key not in ["id", "created_at", "updated_at", "state_id"]: - setattr(fetched_obj, key, val) - fetched_obj.save() - return jsonify(fetched_obj.to_json()) - + return jsonify(city_obj[0]) -@app_views.route("/cities/", methods=["DELETE"], - strict_slashes=False) -def city_delete_by_id(city_id): - """ - deletes City by id - :param city_id: city object id - :return: empty dict with 200 or 404 if not found - """ - fetched_obj = storage.get("City", str(city_id)) - - if fetched_obj is None: +@app_views.route('/cities/', methods=['DELETE']) +def delete_city(city_id): + '''Deletes a City object''' + all_cities = storage.all("City").values() + city_obj = [obj.to_dict() for obj in all_cities if obj.id == city_id] + if city_obj == []: abort(404) - - storage.delete(fetched_obj) + city_obj.remove(city_obj[0]) + for obj in all_cities: + if obj.id == city_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/cities/', methods=['PUT']) +def updates_city(city_id): + '''Updates a City object''' + all_cities = storage.all("City").values() + city_obj = [obj.to_dict() for obj in all_cities if obj.id == city_id] + if city_obj == []: + abort(404) + if not request.get_json(): + abort(400, 'Not a JSON') + city_obj[0]['name'] = request.json['name'] + for obj in all_cities: + if obj.id == city_id: + obj.name = request.json['name'] storage.save() - - return jsonify({}) \ No newline at end of file + return jsonify(city_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/places.py b/api/v1/views/places.py index ef71546da40..07fa6c4778e 100755 --- a/api/v1/views/places.py +++ b/api/v1/views/places.py @@ -1,115 +1,123 @@ #!/usr/bin/python3 -""" -route for handling Place objects and operations -""" +"""places""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage +from models.city import City from models.place import Place +from datetime import datetime +import uuid -@app_views.route("/cities//places", methods=["GET"], - strict_slashes=False) -def places_by_city(city_id): - """ - retrieves all Place objects by city - :return: json of all Places - """ - place_list = [] - city_obj = storage.get("City", str(city_id)) - for obj in city_obj.places: - place_list.append(obj.to_json()) - - return jsonify(place_list) - - -@app_views.route("/cities//places", methods=["POST"], - strict_slashes=False) -def place_create(city_id): - """ - create place route - :return: newly created Place obj - """ - place_json = request.get_json(silent=True) - if place_json is None: - abort(400, 'Not a JSON') - if not storage.get("User", place_json["user_id"]): +@app_views.route('/cities//places', methods=['GET']) +@app_views.route('/cities//places/', methods=['GET']) +def list_places_of_city(city_id): + '''Retrieves a list of all Place objects in city''' + all_cities = storage.all("City").values() + city_obj = [obj.to_dict() for obj in all_cities if obj.id == city_id] + if city_obj == []: abort(404) - if not storage.get("City", city_id): - abort(404) - if "user_id" not in place_json: - abort(400, 'Missing user_id') - if "name" not in place_json: - abort(400, 'Missing name') - - place_json["city_id"] = city_id + list_places = [obj.to_dict() for obj in storage.all("Place").values() + if city_id == obj.city_id] + return jsonify(list_places) - new_place = Place(**place_json) - new_place.save() - resp = jsonify(new_place.to_json()) - resp.status_code = 201 - return resp - - -@app_views.route("/places/", methods=["GET"], - strict_slashes=False) -def place_by_id(place_id): - """ - gets a specific Place object by ID - :param place_id: place object id - :return: place obj with the specified id or error - """ - - fetched_obj = storage.get("Place", str(place_id)) - - if fetched_obj is None: +@app_views.route('/places/', methods=['GET']) +def get_place(place_id): + '''Retrieves a Place object''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) + return jsonify(place_obj[0]) - return jsonify(fetched_obj.to_json()) - -@app_views.route("/places/", methods=["PUT"], - strict_slashes=False) -def place_put(place_id): - """ - updates specific Place object by ID - :param place_id: Place object ID - :return: Place object and 200 on success, or 400 or 404 on failure - """ - place_json = request.get_json(silent=True) - - if place_json is None: +@app_views.route('/places/', methods=['DELETE']) +def delete_place(place_id): + '''Deletes a Place object''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places + if obj.id == place_id] + if place_obj == []: + abort(404) + place_obj.remove(place_obj[0]) + for obj in all_places: + if obj.id == place_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/cities//places', methods=['POST']) +def create_place(city_id): + '''Creates a Place''' + if not request.get_json(): abort(400, 'Not a JSON') - - fetched_obj = storage.get("Place", str(place_id)) - - if fetched_obj is None: + if 'user_id' not in request.get_json(): + abort(400, 'Missing user_id') + if 'name' not in request.get_json(): + abort(400, 'Missing name') + all_cities = storage.all("City").values() + city_obj = [obj.to_dict() for obj in all_cities + if obj.id == city_id] + if city_obj == []: abort(404) + places = [] + new_place = Place(name=request.json['name'], + user_id=request.json['user_id'], city_id=city_id) + all_users = storage.all("User").values() + user_obj = [obj.to_dict() for obj in all_users + if obj.id == new_place.user_id] + if user_obj == []: + abort(404) + storage.new(new_place) + storage.save() + places.append(new_place.to_dict()) + return jsonify(places[0]), 201 - for key, val in place_json.items(): - if key not in ["id", "created_at", "updated_at", "user_id", "city_id"]: - setattr(fetched_obj, key, val) - - fetched_obj.save() - - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/places/", methods=["DELETE"], - strict_slashes=False) -def place_delete_by_id(place_id): - """ - deletes Place by id - :param place_id: Place object id - :return: empty dict with 200 or 404 if not found - """ - - fetched_obj = storage.get("Place", str(place_id)) - if fetched_obj is None: +@app_views.route('/places/', methods=['PUT']) +def updates_place(place_id): + '''Updates a Place object''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) - - storage.delete(fetched_obj) + if not request.get_json(): + abort(400, 'Not a JSON') + if 'name' in request.get_json(): + place_obj[0]['name'] = request.json['name'] + if 'description' in request.get_json(): + place_obj[0]['description'] = request.json['description'] + if 'number_rooms' in request.get_json(): + place_obj[0]['number_rooms'] = request.json['number_rooms'] + if 'number_bathrooms' in request.get_json(): + place_obj[0]['number_bathrooms'] = request.json['number_bathrooms'] + if 'max_guest' in request.get_json(): + place_obj[0]['max_guest'] = request.json['max_guest'] + if 'price_by_night' in request.get_json(): + place_obj[0]['price_by_night'] = request.json['price_by_night'] + if 'latitude' in request.get_json(): + place_obj[0]['latitude'] = request.json['latitude'] + if 'longitude' in request.get_json(): + place_obj[0]['longitude'] = request.json['longitude'] + for obj in all_places: + if obj.id == place_id: + if 'name' in request.get_json(): + obj.name = request.json['name'] + if 'description' in request.get_json(): + obj.description = request.json['description'] + if 'number_rooms' in request.get_json(): + obj.number_rooms = request.json['number_rooms'] + if 'number_bathrooms' in request.get_json(): + obj.number_bathrooms = request.json['number_bathrooms'] + if 'max_guest' in request.get_json(): + obj.max_guest = request.json['max_guest'] + if 'price_by_night' in request.get_json(): + obj.price_by_night = request.json['price_by_night'] + if 'latitude' in request.get_json(): + obj.latitude = request.json['latitude'] + if 'longitude' in request.get_json(): + obj.longitude = request.json['longitude'] storage.save() - - return jsonify({}) \ No newline at end of file + return jsonify(place_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/places_amenities.py b/api/v1/views/places_amenities.py index 6a8246eb60e..45ce89ec488 100755 --- a/api/v1/views/places_amenities.py +++ b/api/v1/views/places_amenities.py @@ -1,95 +1,158 @@ #!/usr/bin/python3 -""" objects that handle all default RestFul API actions for Place - Amenity """ +"""places_amenities""" +from api.v1.views import app_views +from flask import jsonify, abort, request +from models import storage from models.place import Place from models.amenity import Amenity -from models import storage -from api.v1.views import app_views -from os import environ -from flask import abort, jsonify, make_response, request -from flasgger.utils import swag_from # type: ignore - - -@app_views.route('places//amenities', methods=['GET'], - strict_slashes=False) -@swag_from('documentation/place_amenity/get_places_amenities.yml', - methods=['GET']) -def get_place_amenities(place_id): - """ - Retrieves the list of all Amenity objects of a Place - """ - place = storage.get(Place, place_id) - - if not place: - abort(404) - - if environ.get('HBNB_TYPE_STORAGE') == "db": - amenities = [amenity.to_dict() for amenity in place.amenities] - else: - amenities = [storage.get(Amenity, amenity_id).to_dict() - for amenity_id in place.amenity_ids] - - return jsonify(amenities) - - -@app_views.route('/places//amenities/', - methods=['DELETE'], strict_slashes=False) -@swag_from('documentation/place_amenity/delete_place_amenities.yml', - methods=['DELETE']) -def delete_place_amenity(place_id, amenity_id): - """ - Deletes a Amenity object of a Place - """ - place = storage.get(Place, place_id) - - if not place: - abort(404) - - amenity = storage.get(Amenity, amenity_id) - - if not amenity: - abort(404) - - if environ.get('HBNB_TYPE_STORAGE') == "db": - if amenity not in place.amenities: +from datetime import datetime +import uuid +from os import getenv + +if getenv('HBNB_TYPE_STORAGE') == 'db': + @app_views.route('/places//amenities', methods=['GET']) + @app_views.route('/places//amenities/', methods=['GET']) + def list_amenities_of_place(place_id): + ''' Retrieves a list of all Amenity objects of a Place ''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) - place.amenities.remove(amenity) - else: - if amenity_id not in place.amenity_ids: + list_amenities = [] + for obj in all_places: + if obj.id == place_id: + for amenity in obj.amenities: + list_amenities.append(amenity.to_dict()) + return jsonify(list_amenities) + + @app_views.route('/places//amenities/', + methods=['POST']) + def create_place_amenity(place_id, amenity_id): + '''Creates a Amenity''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) - place.amenity_ids.remove(amenity_id) - storage.save() - return make_response(jsonify({}), 200) + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: + abort(404) + amenities = [] + for place in all_places: + if place.id == place_id: + for amenity in all_amenities: + if amenity.id == amenity_id: + place.amenities.append(amenity) + storage.save() + amenities.append(amenity.to_dict()) + return jsonify(amenities[0]), 200 + return jsonify(amenities[0]), 201 + + @app_views.route('/places//amenities/', + methods=['DELETE']) + def delete_place_amenity(place_id, amenity_id): + '''Deletes a Amenity object''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: + abort(404) -@app_views.route('/places//amenities/', methods=['POST'], - strict_slashes=False) -@swag_from('documentation/place_amenity/post_place_amenities.yml', - methods=['POST']) -def post_place_amenity(place_id, amenity_id): - """ - Link a Amenity object to a Place - """ - place = storage.get(Place, place_id) + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: + abort(404) + amenity_obj.remove(amenity_obj[0]) + + for obj in all_places: + if obj.id == place_id: + if obj.amenities == []: + abort(404) + for amenity in obj.amenities: + if amenity.id == amenity_id: + storage.delete(amenity) + storage.save() + return jsonify({}), 200 +""" +else: + @app_views.route('/places//amenities', methods=['GET']) + @app_views.route('/places//amenities/', methods=['GET']) + def list_amenities_of_place(place_id): + ''' Retrieves a list of all Amenity objects of a Place ''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: + abort(404) + list_amenities = [] + for obj in all_places: + if obj.id == place_id: + for amenity in obj.amenities: + list_amenities.append(amenity.to_dict()) + return jsonify(list_amenities) + + @app_views.route('/places//amenities/', + methods=['POST']) + def create_place_amenity(place_id, amenity_id): + '''Creates a Amenity''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: + abort(404) - if not place: - abort(404) + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: + abort(404) - amenity = storage.get(Amenity, amenity_id) + amenities = [] + for place in all_places: + if place.id == place_id: + for amenity in all_amenities: + if amenity.id == amenity_id: + place.amenities.append(amenity) + storage.save() + amenities.append(amenity.to_dict()) + return jsonify(amenities[0]), 200 + return jsonify(amenities[0]), 201 + + @app_views.route('/places//amenities/', + methods=['DELETE']) + def delete_place_amenity(place_id, amenity_id): + '''Deletes a Amenity object''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: + abort(404) - if not amenity: + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: + abort(404) + amenity_obj.remove(amenity_obj[0]) + + for obj in all_places: + if obj.id == place_id: + if obj.amenities == []: + abort(404) + for amenity in obj.amenities: + if amenity.id == amenity_id: + storage.delete(amenity) + storage.save() + return jsonify({}), 200 +""" + + +@app_views.route('/amenities/', methods=['GET']) +def get_place_amenity(amenity_id): + '''Retrieves a Amenity object ''' + all_amenities = storage.all("Amenity").values() + amenity_obj = [obj.to_dict() for obj in all_amenities + if obj.id == amenity_id] + if amenity_obj == []: abort(404) - - if environ.get('HBNB_TYPE_STORAGE') == "db": - if amenity in place.amenities: - return make_response(jsonify(amenity.to_dict()), 200) - else: - place.amenities.append(amenity) - else: - if amenity_id in place.amenity_ids: - return make_response(jsonify(amenity.to_dict()), 200) - else: - place.amenity_ids.append(amenity_id) - - storage.save() - return make_response(jsonify(amenity.to_dict()), 201) \ No newline at end of file + return jsonify(amenity_obj[0]) \ No newline at end of file diff --git a/api/v1/views/places_reviews.py b/api/v1/views/places_reviews.py index 564fb412d1c..7703ff67559 100755 --- a/api/v1/views/places_reviews.py +++ b/api/v1/views/places_reviews.py @@ -1,120 +1,92 @@ #!/usr/bin/python3 -""" -route for handling Review objects and operations -""" +"""places_reviews""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage +from models.place import Place from models.review import Review +from datetime import datetime +import uuid -@app_views.route("/places//reviews", methods=["GET"], - strict_slashes=False) -def reviews_by_place(place_id): - """ - retrieves all Review objects by place - :return: json of all reviews - """ - review_list = [] - place_obj = storage.get("Place", str(place_id)) - - if place_obj is None: +@app_views.route('/places//reviews', methods=['GET']) +@app_views.route('/places//reviews/', methods=['GET']) +def list_reviews_of_place(place_id): + ''' Retrieves a list of all Review objects of a Place ''' + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) + list_reviews = [obj.to_dict() for obj in storage.all("Review").values() + if place_id == obj.place_id] + return jsonify(list_reviews) - for obj in place_obj.reviews: - review_list.append(obj.to_json()) - - return jsonify(review_list) - -@app_views.route("/places//reviews", methods=["POST"], - strict_slashes=False) -def review_create(place_id): - """ - create REview route - :return: newly created Review obj - """ - review_json = request.get_json(silent=True) - if review_json is None: +@app_views.route('/places//reviews', methods=['POST']) +def create_review(place_id): + '''Creates a Review''' + if not request.get_json(): abort(400, 'Not a JSON') - if not storage.get("Place", place_id): - abort(404) - if not storage.get("User", review_json["user_id"]): - abort(404) - if "user_id" not in review_json: + if 'user_id' not in request.get_json(): abort(400, 'Missing user_id') - if "text" not in review_json: + user_id = request.json['user_id'] + if 'text' not in request.get_json(): abort(400, 'Missing text') - - review_json["place_id"] = place_id - - new_review = Review(**review_json) - new_review.save() - resp = jsonify(new_review.to_json()) - resp.status_code = 201 - - return resp - - -@app_views.route("/reviews/", methods=["GET"], - strict_slashes=False) -def review_by_id(review_id): - """ - gets a specific Review object by ID - :param review_id: place object id - :return: review obj with the specified id or error - """ - - fetched_obj = storage.get("Review", str(review_id)) - - if fetched_obj is None: + all_places = storage.all("Place").values() + place_obj = [obj.to_dict() for obj in all_places if obj.id == place_id] + if place_obj == []: abort(404) - - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/reviews/", methods=["PUT"], - strict_slashes=False) -def review_put(review_id): - """ - updates specific Review object by ID - :param review_id: Review object ID - :return: Review object and 200 on success, or 400 or 404 on failure - """ - place_json = request.get_json(silent=True) - - if place_json is None: - abort(400, 'Not a JSON') - - fetched_obj = storage.get("Review", str(review_id)) - - if fetched_obj is None: + all_users = storage.all("User").values() + user_obj = [obj.to_dict() for obj in all_users if obj.id == user_id] + if user_obj == []: abort(404) + reviews = [] + new_review = Review(text=request.json['text'], place_id=place_id, + user_id=user_id) + storage.new(new_review) + storage.save() + reviews.append(new_review.to_dict()) + return jsonify(reviews[0]), 201 - for key, val in place_json.items(): - if key not in ["id", "created_at", "updated_at", "user_id", - "place_id"]: - setattr(fetched_obj, key, val) - - fetched_obj.save() - - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/reviews/", methods=["DELETE"], - strict_slashes=False) -def review_delete_by_id(review_id): - """ - deletes Review by id - :param : Review object id - :return: empty dict with 200 or 404 if not found - """ - - fetched_obj = storage.get("Review", str(review_id)) - if fetched_obj is None: +@app_views.route('/reviews/', methods=['GET']) +def get_review(review_id): + '''Retrieves a Review object ''' + all_reviews = storage.all("Review").values() + review_obj = [obj.to_dict() for obj in all_reviews if obj.id == review_id] + if review_obj == []: abort(404) + return jsonify(review_obj[0]) - storage.delete(fetched_obj) - storage.save() - return jsonify({}) \ No newline at end of file +@app_views.route('/reviews/', methods=['DELETE']) +def delete_review(review_id): + '''Deletes a Review object''' + all_reviews = storage.all("Review").values() + review_obj = [obj.to_dict() for obj in all_reviews if obj.id == review_id] + if review_obj == []: + abort(404) + review_obj.remove(review_obj[0]) + for obj in all_reviews: + if obj.id == review_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/reviews/', methods=['PUT']) +def updates_review(review_id): + '''Updates a Review object''' + all_reviews = storage.all("Review").values() + review_obj = [obj.to_dict() for obj in all_reviews if obj.id == review_id] + if review_obj == []: + abort(404) + if not request.get_json(): + abort(400, 'Not a JSON') + if 'text' in request.get_json(): + review_obj[0]['text'] = request.json['text'] + for obj in all_reviews: + if obj.id == review_id: + obj.text = request.json['text'] + storage.save() + return jsonify(review_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/states.py b/api/v1/views/states.py index 6c238c6f5cf..6cc0cfdc1b0 100755 --- a/api/v1/views/states.py +++ b/api/v1/views/states.py @@ -1,97 +1,72 @@ #!/usr/bin/python3 -""" -route for handling State objects and operations -""" +"""states""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage from models.state import State +from datetime import datetime +import uuid -@app_views.route("/states", methods=["GET"], strict_slashes=False) -def state_get_all(): - """ - retrieves all State objects - :return: json of all states - """ - state_list = [] - state_obj = storage.all("State") - for obj in state_obj.values(): - state_list.append(obj.to_json()) +@app_views.route('/states/', methods=['GET']) +def list_states(): + '''Retrieves a list of all State objects''' + list_states = [obj.to_dict() for obj in storage.all("State").values()] + return jsonify(list_states) - return jsonify(state_list) - -@app_views.route("/states", methods=["POST"], strict_slashes=False) -def state_create(): - """ - create state route - :return: newly created state obj - """ - state_json = request.get_json(silent=True) - if state_json is None: - abort(400, 'Not a JSON') - if "name" not in state_json: - abort(400, 'Missing name') - - new_state = State(**state_json) - new_state.save() - resp = jsonify(new_state.to_json()) - resp.status_code = 201 - - return resp - - -@app_views.route("/states/", methods=["GET"], strict_slashes=False) -def state_by_id(state_id): - """ - gets a specific State object by ID - :param state_id: state object id - :return: state obj with the specified id or error - """ - - fetched_obj = storage.get("State", str(state_id)) - - if fetched_obj is None: +@app_views.route('/states/', methods=['GET']) +def get_state(state_id): + '''Retrieves a State object''' + all_states = storage.all("State").values() + state_obj = [obj.to_dict() for obj in all_states if obj.id == state_id] + if state_obj == []: abort(404) - - return jsonify(fetched_obj.to_json()) + return jsonify(state_obj[0]) -@app_views.route("/states/", methods=["PUT"], strict_slashes=False) -def state_put(state_id): - """ - updates specific State object by ID - :param state_id: state object ID - :return: state object and 200 on success, or 400 or 404 on failure - """ - state_json = request.get_json(silent=True) - if state_json is None: - abort(400, 'Not a JSON') - fetched_obj = storage.get("State", str(state_id)) - if fetched_obj is None: +@app_views.route('/states/', methods=['DELETE']) +def delete_state(state_id): + '''Deletes a State object''' + all_states = storage.all("State").values() + state_obj = [obj.to_dict() for obj in all_states if obj.id == state_id] + if state_obj == []: abort(404) - for key, val in state_json.items(): - if key not in ["id", "created_at", "updated_at"]: - setattr(fetched_obj, key, val) - fetched_obj.save() - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/states/", methods=["DELETE"], - strict_slashes=False) -def state_delete_by_id(state_id): - """ - deletes State by id - :param state_id: state object id - :return: empty dict with 200 or 404 if not found - """ + state_obj.remove(state_obj[0]) + for obj in all_states: + if obj.id == state_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/states/', methods=['POST']) +def create_state(): + '''Creates a State''' + if not request.get_json(): + abort(400, 'Not a JSON') + if 'name' not in request.get_json(): + abort(400, 'Missing name') + states = [] + new_state = State(name=request.json['name']) + storage.new(new_state) + storage.save() + states.append(new_state.to_dict()) + return jsonify(states[0]), 201 - fetched_obj = storage.get("State", str(state_id)) - if fetched_obj is None: +@app_views.route('/states/', methods=['PUT']) +def updates_state(state_id): + '''Updates a State object''' + all_states = storage.all("State").values() + state_obj = [obj.to_dict() for obj in all_states if obj.id == state_id] + if state_obj == []: abort(404) - - storage.delete(fetched_obj) + if not request.get_json(): + abort(400, 'Not a JSON') + state_obj[0]['name'] = request.json['name'] + for obj in all_states: + if obj.id == state_id: + obj.name = request.json['name'] storage.save() - - return jsonify({}) \ No newline at end of file + return jsonify(state_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/users.py b/api/v1/views/users.py index df10b7a3d95..b1080d5539c 100755 --- a/api/v1/views/users.py +++ b/api/v1/views/users.py @@ -1,104 +1,92 @@ #!/usr/bin/python3 -""" -route for handling User objects and operations -""" +"""users""" +from api.v1.views import app_views from flask import jsonify, abort, request -from api.v1.views import app_views, storage +from models import storage from models.user import User +from datetime import datetime +import uuid -@app_views.route("/users", methods=["GET"], strict_slashes=False) -def user_get_all(): - """ - retrieves all User objects - :return: json of all users - """ - user_list = [] - user_obj = storage.all("User") - for obj in user_obj.values(): - user_list.append(obj.to_json()) +@app_views.route('/users/', methods=['GET']) +@app_views.route('/users', methods=['GET']) +def list_users(): + '''Retrieves a list of all User objects''' + list_users = [obj.to_dict() for obj in storage.all("User").values()] + return jsonify(list_users) - return jsonify(user_list) - -@app_views.route("/users", methods=["POST"], strict_slashes=False) -def user_create(): - """ - create user route - :return: newly created user obj - """ - user_json = request.get_json(silent=True) - if user_json is None: - abort(400, 'Not a JSON') - if "email" not in user_json: - abort(400, 'Missing email') - if "password" not in user_json: - abort(400, 'Missing password') - - new_user = User(**user_json) - new_user.save() - resp = jsonify(new_user.to_json()) - resp.status_code = 201 - - return resp - - -@app_views.route("/users/", methods=["GET"], strict_slashes=False) -def user_by_id(user_id): - """ - gets a specific User object by ID - :param user_id: user object id - :return: user obj with the specified id or error - """ - - fetched_obj = storage.get("User", str(user_id)) - - if fetched_obj is None: +@app_views.route('/users/', methods=['GET']) +def get_user(user_id): + '''Retrieves a User object''' + all_users = storage.all("User").values() + user_obj = [obj.to_dict() for obj in all_users if obj.id == user_id] + if user_obj == []: abort(404) + return jsonify(user_obj[0]) - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/users/", methods=["PUT"], strict_slashes=False) -def user_put(user_id): - """ - updates specific User object by ID - :param user_id: user object ID - :return: user object and 200 on success, or 400 or 404 on failure - """ - user_json = request.get_json(silent=True) - - if user_json is None: - abort(400, 'Not a JSON') - fetched_obj = storage.get("User", str(user_id)) - - if fetched_obj is None: +@app_views.route('/users/', methods=['DELETE']) +def delete_user(user_id): + '''Deletes a User object''' + all_users = storage.all("User").values() + user_obj = [obj.to_dict() for obj in all_users if obj.id == user_id] + if user_obj == []: abort(404) + user_obj.remove(user_obj[0]) + for obj in all_users: + if obj.id == user_id: + storage.delete(obj) + storage.save() + return jsonify({}), 200 + + +@app_views.route('/users/', methods=['POST']) +def create_user(): + '''Creates a User''' + if not request.get_json(): + abort(400, 'Not a JSON') + if 'email' not in request.get_json(): + abort(400, 'Missing name') + if 'password' not in request.get_json(): + abort(400, 'Missing name') + users = [] + new_user = User(email=request.json['email'], + password=request.json['password']) + storage.new(new_user) + storage.save() + users.append(new_user.to_dict()) + return jsonify(users[0]), 201 - for key, val in user_json.items(): - if key not in ["id", "created_at", "updated_at", "email"]: - setattr(fetched_obj, key, val) - - fetched_obj.save() - - return jsonify(fetched_obj.to_json()) - - -@app_views.route("/users/", methods=["DELETE"], strict_slashes=False) -def user_delete_by_id(user_id): - """ - deletes User by id - :param user_id: user object id - :return: empty dict with 200 or 404 if not found - """ - - fetched_obj = storage.get("User", str(user_id)) - if fetched_obj is None: +@app_views.route('/users/', methods=['PUT']) +def updates_user(user_id): + '''Updates a User object''' + all_users = storage.all("User").values() + user_obj = [obj.to_dict() for obj in all_users if obj.id == user_id] + if user_obj == []: abort(404) - - storage.delete(fetched_obj) + if not request.get_json(): + abort(400, 'Not a JSON') + try: + user_obj[0]['first_name'] = request.json['first_name'] + except: + pass + try: + user_obj[0]['last_name'] = request.json['last_name'] + except: + pass + for obj in all_users: + if obj.id == user_id: + try: + if request.json['first_name'] is not None: + obj.first_name = request.json['first_name'] + except: + pass + try: + if request.json['last_name'] is not None: + obj.last_name = request.json['last_name'] + except: + pass storage.save() - - return jsonify({}) \ No newline at end of file + return jsonify(user_obj[0]), 200 \ No newline at end of file