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/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 100755 index 00000000000..b90fb4bd6de --- /dev/null +++ b/api/v1/views/__init__.py @@ -0,0 +1,15 @@ +#!/usr/bin/python3 +"""create blueprint""" +from flask import Blueprint + +app_views = Blueprint('app_views', __name__, url_prefix='/api/v1') + +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 new file mode 100755 index 00000000000..1b4bcc9f95e --- /dev/null +++ b/api/v1/views/amenities.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 +"""amenities""" +from api.v1.views import app_views +from flask import jsonify, abort, request +from models import storage +from models.amenity import Amenity +from datetime import datetime +import uuid + + +@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) + + +@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(amenity_obj[0]) + + +@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) + 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 + + +@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) + 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(amenity_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/cities.py b/api/v1/views/cities.py new file mode 100755 index 00000000000..e25dfcfc51a --- /dev/null +++ b/api/v1/views/cities.py @@ -0,0 +1,84 @@ +#!/usr/bin/python3 +"""cities""" +from api.v1.views import app_views +from flask import jsonify, abort, request +from models import storage +from models.city import City +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) + list_cities = [obj.to_dict() for obj in storage.all("City").values() + if state_id == obj.state_id] + return jsonify(list_cities) + + +@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 'name' not in request.get_json(): + abort(400, 'Missing name') + 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) + 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=['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) + return jsonify(city_obj[0]) + + +@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) + 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(city_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/index.py b/api/v1/views/index.py new file mode 100755 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 diff --git a/api/v1/views/places.py b/api/v1/views/places.py new file mode 100755 index 00000000000..07fa6c4778e --- /dev/null +++ b/api/v1/views/places.py @@ -0,0 +1,123 @@ +#!/usr/bin/python3 +"""places""" +from api.v1.views import app_views +from flask import jsonify, abort, request +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']) +@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) + list_places = [obj.to_dict() for obj in storage.all("Place").values() + if city_id == obj.city_id] + return jsonify(list_places) + + +@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]) + + +@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') + 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 + + +@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) + 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(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 new file mode 100755 index 00000000000..45ce89ec488 --- /dev/null +++ b/api/v1/views/places_amenities.py @@ -0,0 +1,158 @@ +#!/usr/bin/python3 +"""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 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) + 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) + + 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) + + 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) + + 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) + + 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) + 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 new file mode 100755 index 00000000000..7703ff67559 --- /dev/null +++ b/api/v1/views/places_reviews.py @@ -0,0 +1,92 @@ +#!/usr/bin/python3 +"""places_reviews""" +from api.v1.views import app_views +from flask import jsonify, abort, request +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']) +@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) + + +@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 'user_id' not in request.get_json(): + abort(400, 'Missing user_id') + user_id = request.json['user_id'] + if 'text' not in request.get_json(): + abort(400, 'Missing text') + 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) + 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 + + +@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]) + + +@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 new file mode 100755 index 00000000000..6cc0cfdc1b0 --- /dev/null +++ b/api/v1/views/states.py @@ -0,0 +1,72 @@ +#!/usr/bin/python3 +"""states""" +from api.v1.views import app_views +from flask import jsonify, abort, request +from models import storage +from models.state import State +from datetime import datetime +import uuid + + +@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) + + +@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(state_obj[0]) + + +@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) + 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 + + +@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) + 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(state_obj[0]), 200 \ No newline at end of file diff --git a/api/v1/views/users.py b/api/v1/views/users.py new file mode 100755 index 00000000000..b1080d5539c --- /dev/null +++ b/api/v1/views/users.py @@ -0,0 +1,92 @@ +#!/usr/bin/python3 +"""users""" +from api.v1.views import app_views +from flask import jsonify, abort, request +from models import storage +from models.user import User +from datetime import datetime +import uuid + + +@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) + + +@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]) + + +@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 + + +@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) + 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(user_obj[0]), 200 \ No newline at end of file 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..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 @@ -68,3 +69,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 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 diff --git a/tests/test_models/test_engine/test_db_storage.py b/tests/test_models/test_engine/test_db_storage.py index 766e625b5af..c2cf09ea944 100755 --- a/tests/test_models/test_engine/test_db_storage.py +++ b/tests/test_models/test_engine/test_db_storage.py @@ -16,8 +16,9 @@ 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 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)