diff --git a/README.md b/README.md index f1d72de6355..975f536be4d 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) +Kyakuwa Peninnah - [Github] (https://github.com/KyakuwaPeninnah) 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/__pycache__/__init__.cpython-38.pyc b/api/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..2744ac8c3f5 Binary files /dev/null and b/api/__pycache__/__init__.cpython-38.pyc differ diff --git a/api/v1/__init__.py b/api/v1/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/api/v1/__pycache__/__init__.cpython-38.pyc b/api/v1/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..243752273ff Binary files /dev/null and b/api/v1/__pycache__/__init__.cpython-38.pyc differ diff --git a/api/v1/__pycache__/app.cpython-38.pyc b/api/v1/__pycache__/app.cpython-38.pyc new file mode 100644 index 00000000000..6c407bb898b Binary files /dev/null and b/api/v1/__pycache__/app.cpython-38.pyc differ diff --git a/api/v1/app.py b/api/v1/app.py new file mode 100644 index 00000000000..2409983a1e1 --- /dev/null +++ b/api/v1/app.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +'''Flask app''' + + +from flask import Flask, make_response, jsonify +from flask_cors import CORS +from models import storage +from api.v1.views import app_views +import os + + +app = Flask(__name__) +app.register_blueprint(app_views) +cors = CORS(app, resources={r'/*': {'origins': '0.0.0.0'}}) + + +@app.teardown_appcontext +def teardown(exception): + '''something should happen when the app is getting teared down''' + if storage is not None: + storage.close() + + +@app.errorhandler(404) +def errorhandler(error): + '''handling 404 error''' + return make_response(jsonify({'error': 'Not found'}), 404) + + +if __name__ == '__main__': + host = os.getenv('HBNB_API_HOST', '0.0.0.0') + port = os.getenv('HBNB_API_PORT', '5000') + app.run(host=host, port=port, threaded=True) diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py new file mode 100644 index 00000000000..804bd4d5849 --- /dev/null +++ b/api/v1/views/__init__.py @@ -0,0 +1,16 @@ +#!/usr/bin/python3 +'''init in views''' + +from flask import Blueprint + + +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.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 * diff --git a/api/v1/views/__pycache__/__init__.cpython-38.pyc b/api/v1/views/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..a96bdb3bfe3 Binary files /dev/null and b/api/v1/views/__pycache__/__init__.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/amenities.cpython-38.pyc b/api/v1/views/__pycache__/amenities.cpython-38.pyc new file mode 100644 index 00000000000..e9334b89b29 Binary files /dev/null and b/api/v1/views/__pycache__/amenities.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/cities.cpython-38.pyc b/api/v1/views/__pycache__/cities.cpython-38.pyc new file mode 100644 index 00000000000..555f2bf530d Binary files /dev/null and b/api/v1/views/__pycache__/cities.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/index.cpython-38.pyc b/api/v1/views/__pycache__/index.cpython-38.pyc new file mode 100644 index 00000000000..d1468d49d05 Binary files /dev/null and b/api/v1/views/__pycache__/index.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/places.cpython-38.pyc b/api/v1/views/__pycache__/places.cpython-38.pyc new file mode 100644 index 00000000000..4704cf6275a Binary files /dev/null and b/api/v1/views/__pycache__/places.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/places_amenities.cpython-38.pyc b/api/v1/views/__pycache__/places_amenities.cpython-38.pyc new file mode 100644 index 00000000000..d7c24fda5f3 Binary files /dev/null and b/api/v1/views/__pycache__/places_amenities.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/places_reviews.cpython-38.pyc b/api/v1/views/__pycache__/places_reviews.cpython-38.pyc new file mode 100644 index 00000000000..39dcf6e0d2f Binary files /dev/null and b/api/v1/views/__pycache__/places_reviews.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/states.cpython-38.pyc b/api/v1/views/__pycache__/states.cpython-38.pyc new file mode 100644 index 00000000000..98b257bf9d7 Binary files /dev/null and b/api/v1/views/__pycache__/states.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/users.cpython-38.pyc b/api/v1/views/__pycache__/users.cpython-38.pyc new file mode 100644 index 00000000000..852287c8ada Binary files /dev/null and b/api/v1/views/__pycache__/users.cpython-38.pyc differ diff --git a/api/v1/views/amenities.py b/api/v1/views/amenities.py new file mode 100755 index 00000000000..71545ccb502 --- /dev/null +++ b/api/v1/views/amenities.py @@ -0,0 +1,77 @@ +#!/usr/bin/python3 +'''amenities module''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage +from models.amenity import Amenity + + +@app_views.route('/amenities', methods=['GET'], strict_slashes=False) +def getamenities(): + '''get all amenities''' + amens = storage.all(Amenity) + return jsonify([amen.to_dict() for amen in amens.values()]) + + +@app_views.route('/amenities/', + methods=['GET'], + strict_slashes=False) +def getAmenityById(amenity_id=None): + '''gets amenity by id''' + if amenity_id is None: + abort(404) + amen = storage.get(Amenity, amenity_id) + if amen is None: + abort(404) + return jsonify(amen.to_dict()) + + +@app_views.route('/amenities/', + methods=['DELETE'], + strict_slashes=False) +def deleteAmenity(amenity_id=None): + '''deletes an amenity''' + if amenity_id is not None: + result = storage.get(Amenity, amenity_id) + if res is not None: + storage.delete(result) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/amenities', + methods=['POST'], + strict_slashes=False) +def postAmenity(): + '''posts a new amenity''' + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + if 'name' not in body.keys(): + abort(400, 'Missing name') + amen = Amenity(**body) + amen.save() + return make_response(jsonify(amen.to_dict()), 201) + + +@app_views.route('/amenities/', + methods=['PUT'], + strict_slashes=False) +def updateAmenity(amenity_id=None): + '''updates amenity''' + if amenity_id is None: + abort(404) + obj = storage.get(Amenity, amenity_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + for key in body.keys(): + if key not in ['id', 'created_at', 'updated_at']: + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/api/v1/views/cities.py b/api/v1/views/cities.py new file mode 100755 index 00000000000..9dec1573536 --- /dev/null +++ b/api/v1/views/cities.py @@ -0,0 +1,88 @@ +#!/usr/bin/python3 +'''cities blueprint''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage +from models.state import State +from models.city import City + + +@app_views.route('/cities/', methods=['GET'], strict_slashes=False) +def getCity(city_id=None): + '''get a city with the id''' + if city_id is None: + abort(404) + ct = storage.get(City, city_id) + if ct is None: + abort(404) + return jsonify(ct.to_dict()) + + +@app_views.route('/states//cities', + methods=['GET'], + strict_slashes=False) +def getCitiesInState(state_id=None): + '''gets all cities in state with the id passed''' + if state_id is None: + abort(404) + st = storage.get(State, state_id) + if st is None: + abort(404) + cts = st.cities + return jsonify([ct.to_dict() for ct in cts]) + + +@app_views.route('/cities/', + methods=['DELETE'], + strict_slashes=False) +def deleteCity(city_id=None): + '''deletes a city''' + if city_id is not None: + res = storage.get(City, city_id) + if res is not None: + storage.delete(res) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/states//cities', + methods=['POST'], + strict_slashes=False) +def postCity(state_id=None): + '''posts a new city to a specific state''' + if state_id is None: + abort(404) + st = storage.get(State, state_id) + if st is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + if 'name' not in body.keys(): + abort(400, 'Missing name') + body['state_id'] = st.id + obj = City(**body) + obj.save() + return make_response(jsonify(obj.to_dict()), 201) + + +@app_views.route('/cities/', methods=['PUT'], strict_slashes=False) +def updateCity(city_id=None): + '''updates a city''' + if city_id is None: + abort(404) + obj = storage.get(City, city_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + for key in body.keys(): + if key not in ['id', 'created_at', 'updated_at', 'state_id']: + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/api/v1/views/index.py b/api/v1/views/index.py new file mode 100755 index 00000000000..2ac90029a2d --- /dev/null +++ b/api/v1/views/index.py @@ -0,0 +1,33 @@ +#!/usr/bin/python3 +'''index module''' + +from api.v1.views import app_views +from flask import jsonify +from models import storage + +from models.amenity import Amenity +from models.city import City +from models.place import Place +from models.review import Review +from models.state import State +from models.user import User + + +classes = {"amenities": Amenity, "cities": City, + "places": Place, "reviews": Review, "states": State, "users": User} + + +@app_views.route('/status') +def status(): + '''returns json status OK''' + return jsonify({"status": "OK"}) + + +@app_views.route('/stats') +def stats(): + '''retrieves the number of each objects by type''' + result = {} + for cls in classes: + counter = storage.count(classes[cls]) + result[cls] = counter + return jsonify(result) diff --git a/api/v1/views/places.py b/api/v1/views/places.py new file mode 100755 index 00000000000..99ba31c2e26 --- /dev/null +++ b/api/v1/views/places.py @@ -0,0 +1,186 @@ +#!/usr/bin/python3 +'''places blueprint''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage, storage_t +from models.place import Place +from models.state import State +from models.city import City +from models.user import User + + +@app_views.route('/cities//places', + methods=['GET'], + strict_slashes=False) +def getPlacesInCity(city_id=None): + '''get all places in a city''' + if city_id is None: + abort(404) + ct = storage.get(City, city_id) + if ct is None: + abort(404) + + places = storage.all(Place) + res = [] + for place in places.values(): + if place.city_id == ct.id: + res.append(place) + + return jsonify([place.to_dict() for place in res]) + + +@app_views.route('/places/', + methods=['GET'], + strict_slashes=False) +def getPlaceById(place_id=None): + '''gets place by id''' + if place_id is None: + abort(404) + place = storage.get(Place, place_id) + if place is None: + abort(404) + return jsonify(place.to_dict()) + + +@app_views.route('/places/', + methods=['DELETE'], + strict_slashes=False) +def deletePlace(place_id=None): + '''deletes a place''' + if place_id is not None: + res = storage.get(Place, place_id) + if res is not None: + storage.delete(res) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/cities//places', + methods=['POST'], + strict_slashes=False) +def postPlace(city_id): + '''posts a new place''' + if city_id is None: + abort(404) + ct = storage.get(City, city_id) + if ct is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + if 'user_id' not in body.keys(): + abort(400, 'Missing user_id') + + user = storage.get(User, body['user_id']) + if user is None: + abort(404) + if 'name' not in body.keys(): + abort(400, 'Missing name') + + body['city_id'] = ct.id + place = Place(**body) + place.save() + return make_response(jsonify(place.to_dict()), 201) + + +@app_views.route('/places_search', methods=['POST'], strict_slashes=False) +def placesSearch(): + '''retrives all place objects depending on the request body''' + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + keys = body.keys() + if len(body) <= 0\ + or (('states' not in keys or len(body['states']) <= 0) and + ('cities' not in keys or len(body['cities']) <= 0)): + if 'amenities' not in keys: + places = storage.all(Place).values() + dcts = [pl.to_dict() for pl in places] + if storage_t == 'db': + for idx in range(len(dcts)): + if 'amenities' in dcts[idx].keys(): + del dcts[idx]['amenities'] + return jsonify(dcts) + else: + places = storage.all(Place).values() + unwanted = [] + for idx, place in enumerate(places): + if storage_t == 'db': + amens_ids = [m.id for m in place.amenities] + else: + amens_ids = place.amenity_ids + for amenity_id in body['amenities']: + if amenity_id not in amens_ids: + unwanted.append(idx) + break + for idx, i in enumerate(unwanted): + del places[i - idx] + dcts = [pl.to_dict() for pl in places] + if storage_t == 'db': + for idx in range(len(dcts)): + if 'amenities' in dcts[idx].keys(): + del dcts[idx]['amenities'] + return jsonify(dcts) + + places = storage.all(Place).values() + wanted_places = [] + cities = {} + + if 'cities' in keys: + for cityId in body['cities']: + cities[cityId] = cityId + + if 'states' in keys: + for state in storage.all(State).values(): + if state.id in body['states']: + for city in state.cities: + cities[city.id] = city.id + + for place in places: + if len(cities) > 0: + if place.city_id in cities: + wanted_places.append(place) + unwanted = [] + if 'amenities' in keys: + for idx, place in enumerate(wanted_places): + if storage_t == 'db': + amens_ids = [m.id for m in place.amenities] + else: + amens_ids = place.amenity_ids + for amenity_id in body['amenities']: + if amenity_id not in amens_ids: + unwanted.append(idx) + break + + for idx, i in enumerate(unwanted): + del wanted_places[i - idx] + dcts = [pl.to_dict() for pl in wanted_places] + if storage_t == 'db': + for idx in range(len(dcts)): + if 'amenities' in dcts[idx].keys(): + del dcts[idx]['amenities'] + return jsonify(dcts) + + +@app_views.route('/places/', + methods=['PUT'], + strict_slashes=False) +def updatePlace(place_id=None): + '''updates a user''' + if place_id is None: + abort(404) + obj = storage.get(Place, place_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + for key in body.keys(): + if key not in ['id', 'created_at', 'updated_at', 'city_id', 'user_id']: + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/api/v1/views/places_amenities.py b/api/v1/views/places_amenities.py new file mode 100755 index 00000000000..08f61c6836a --- /dev/null +++ b/api/v1/views/places_amenities.py @@ -0,0 +1,98 @@ +#!/usr/bin/python3 +'''places amenities relationship blueprint''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage, storage_t +from models.place import Place +from models.user import User +from models.amenity import Amenity + + +@app_views.route('/places//amenities', + methods=['GET'], + strict_slashes=False) +def getAmenitiesInPlace(place_id=None): + '''get all amenities in a place''' + if place_id is None: + abort(404) + pl = storage.get(Place, place_id) + if pl is None: + abort(404) + + res = [] + if storage_t != 'db': + amens = storage.all(Amenity) + for amen in amens.values(): + if amen.id in pl.amenity_ids: + res.append(amen) + else: + res = pl.amenities + + return jsonify([amen.to_dict() for amen in res]) + + +@app_views.route('/places//amenities/', + methods=['DELETE'], + strict_slashes=False) +def deleteAmenityFromPlace(place_id=None, amenity_id=None): + '''deletes an amenity from a place''' + if place_id is None or amenity_id is None: + abort(404) + place = storage.get(Place, place_id) + amenity = storage.get(Amenity, amenity_id) + if place is None or amenity is None: + abort(404) + + if storage_t != 'db': + if amenity.id not in place.amenity_ids: + abort(404) + index = None + for idx, id in enumerate(place.amenity_ids): + if amenity.id == id: + index = idx + break + del place.amenity_ids[index] + place.save() + else: + index = None + for idx, amen in enumerate(place.amenities): + if amen.id == amenity.id: + index = idx + break + if index is None: + abort(404) + del place.amenities[index] + place.save() + + return make_response(jsonify({}), 200) + + +@app_views.route('/places//amenities/', + methods=['POST'], + strict_slashes=False) +def linkAmenityToPlace(place_id=None, amenity_id=None): + '''link an amenity to a place''' + if place_id is None or amenity_id is None: + abort(404) + + place = storage.get(Place, place_id) + amenity = storage.get(Amenity, amenity_id) + + if place is None or amenity is None: + abort(404) + + if storage_t != 'db': + if amenity_id in place.amenity_ids: + return make_response(jsonify(amenity.to_dict()), 200) + else: + place.amenity_ids.append(amenity_id) + place.save() + return make_response(jsonify(amenity.to_dict()), 201) + else: + if amenity in place.amenities: + return make_response(jsonify(amenity.to_dict()), 200) + else: + place.amenities.append(amenity) + place.save() + return make_response(jsonify(amenity.to_dict()), 201) diff --git a/api/v1/views/places_reviews.py b/api/v1/views/places_reviews.py new file mode 100755 index 00000000000..9b2d7cb3074 --- /dev/null +++ b/api/v1/views/places_reviews.py @@ -0,0 +1,108 @@ +#!/usr/bin/python3 +'''reviews blueprint''' + +import re +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage +from models.place import Place +from models.user import User +from models.review import Review + + +@app_views.route('/places//reviews', + methods=['GET'], + strict_slashes=False) +def getReviewsInPlace(place_id=None): + '''get all reviewss in a place''' + if place_id is None: + abort(404) + pl = storage.get(Place, place_id) + if pl is None: + abort(404) + + reviews = storage.all(Review) + res = [] + for rev in reviews.values(): + if rev.place_id == pl.id: + res.append(rev) + + return jsonify([rev.to_dict() for rev in res]) + + +@app_views.route('/reviews/', + methods=['GET'], + strict_slashes=False) +def getReviewById(review_id=None): + '''gets review by id''' + if review_id is None: + abort(404) + review = storage.get(Review, review_id) + if review is None: + abort(404) + return jsonify(review.to_dict()) + + +@app_views.route('/reviews/', + methods=['DELETE'], + strict_slashes=False) +def deleteReview(review_id=None): + '''deletes a review''' + if review_id is not None: + res = storage.get(Review, review_id) + if res is not None: + storage.delete(res) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/places//reviews', + methods=['POST'], + strict_slashes=False) +def postReview(place_id): + '''posts a new Review''' + if place_id is None: + abort(404) + pl = storage.get(Place, place_id) + if pl is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + if 'user_id' not in body.keys(): + abort(400, 'Missing user_id') + + user = storage.get(User, body['user_id']) + if user is None: + abort(404) + if 'text' not in body.keys(): + abort(400, 'Missing text') + + body['place_id'] = pl.id + rev = Review(**body) + rev.save() + return make_response(jsonify(rev.to_dict()), 201) + + +@app_views.route('/reviews/', + methods=['PUT'], + strict_slashes=False) +def updateReview(review_id=None): + '''updates a review''' + if review_id is None: + abort(404) + obj = storage.get(Review, review_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + for key in body.keys(): + if key not in ['id', 'created_at', + 'updated_at', 'place_id', 'user_id']: + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/api/v1/views/states.py b/api/v1/views/states.py new file mode 100755 index 00000000000..8c9a34b34e3 --- /dev/null +++ b/api/v1/views/states.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3 +'''states module''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage +from models.state import State + + +@app_views.route('/states', methods=['GET'], strict_slashes=False) +@app_views.route('/states/', methods=['GET'], strict_slashes=False) +def getStates(state_id=None): + '''gets all states or state with their id passed''' + if state_id is None: + result = [] + states = storage.all(State) + for state in states.values(): + result.append(state.to_dict()) + return jsonify(result) + + result = storage.get(State, state_id) + if res is None: + abort(404) + return jsonify(result.to_dict()) + + +@app_views.route('/states/', + methods=['DELETE'], + strict_slashes=False) +def deleteState(state_id=None): + '''deletes a state''' + if state_id is not None: + result = storage.get(State, state_id) + if result is not None: + storage.delete(result) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/states', methods=['POST'], strict_slashes=False) +def postState(): + '''posts a new state''' + body = request.get_json() + if body is None or type(body)is not dict: + abort(400, description='Not a JSON') + if 'name' not in body.keys(): + abort(400, description='Missing name') + obj = State(**body) + obj.save() + return make_response(jsonify(obj.to_dict()), 201) + + +@app_views.route('/states/', methods=['PUT'], strict_slashes=False) +def updateState(state_id=None): + '''updates a state''' + if state_id is None: + abort(404) + obj = storage.get(State, state_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, 'Not a JSON') + for key in body.keys(): + if key != 'id' and key != 'created_at' and key != 'updated_at': + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/api/v1/views/users.py b/api/v1/views/users.py new file mode 100755 index 00000000000..16a54474ef3 --- /dev/null +++ b/api/v1/views/users.py @@ -0,0 +1,76 @@ +#!/usr/bin/python3 +'''users blueprint''' + +from api.v1.views import app_views +from flask import jsonify, abort, request, make_response +from models import storage +from models.user import User + + +@app_views.route('/users', methods=['GET'], strict_slashes=False) +@app_views.route('/users/', + methods=['GET'], + strict_slashes=False) +def getUserById(user_id=None): + '''gets user by id''' + if user_id is None: + users = storage.all(User) + return jsonify([user.to_dict() for user in users.values()]) + + user = storage.get(User, user_id) + if user is None: + abort(404) + return jsonify(user.to_dict()) + + +@app_views.route('/users/', + methods=['DELETE'], + strict_slashes=False) +def deleteUser(user_id=None): + '''deletes an user''' + if user_id is not None: + res = storage.get(User, user_id) + if res is not None: + storage.delete(res) + storage.save() + return make_response(jsonify({}), 200) + abort(404) + + +@app_views.route('/users', + methods=['POST'], + strict_slashes=False) +def postUser(): + '''posts a new user''' + body = request.get_json() + if body is None or type(body)is not dict: + abort(400, description='Not a JSON') + if 'email' not in body.keys(): + abort(400, description='Missing email') + if 'password' not in body.keys(): + abort(400, description='Missing password') + + user = User(**body) + user.save() + return make_response(jsonify(user.to_dict()), 201) + + +@app_views.route('/users/', + methods=['PUT'], + strict_slashes=False) +def updateUser(user_id=None): + '''updates a user''' + if user_id is None: + abort(404) + obj = storage.get(User, user_id) + if obj is None: + abort(404) + + body = request.get_json() + if body is None: + abort(400, description='Not a JSON') + for key in body.keys(): + if key not in ['id', 'created_at', 'updated_at', 'email']: + setattr(obj, key, body[key]) + obj.save() + return make_response(jsonify(obj.to_dict()), 200) diff --git a/models/__pycache__/__init__.cpython-38.pyc b/models/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..8911f22cde8 Binary files /dev/null and b/models/__pycache__/__init__.cpython-38.pyc differ diff --git a/models/__pycache__/amenity.cpython-38.pyc b/models/__pycache__/amenity.cpython-38.pyc new file mode 100644 index 00000000000..029b74015f8 Binary files /dev/null and b/models/__pycache__/amenity.cpython-38.pyc differ diff --git a/models/__pycache__/base_model.cpython-38.pyc b/models/__pycache__/base_model.cpython-38.pyc new file mode 100644 index 00000000000..9fe6785cb21 Binary files /dev/null and b/models/__pycache__/base_model.cpython-38.pyc differ diff --git a/models/__pycache__/city.cpython-38.pyc b/models/__pycache__/city.cpython-38.pyc new file mode 100644 index 00000000000..ae5db6b2f24 Binary files /dev/null and b/models/__pycache__/city.cpython-38.pyc differ diff --git a/models/__pycache__/place.cpython-38.pyc b/models/__pycache__/place.cpython-38.pyc new file mode 100644 index 00000000000..fafd22dfc24 Binary files /dev/null and b/models/__pycache__/place.cpython-38.pyc differ diff --git a/models/__pycache__/review.cpython-38.pyc b/models/__pycache__/review.cpython-38.pyc new file mode 100644 index 00000000000..8bfd669f8b1 Binary files /dev/null and b/models/__pycache__/review.cpython-38.pyc differ diff --git a/models/__pycache__/state.cpython-38.pyc b/models/__pycache__/state.cpython-38.pyc new file mode 100644 index 00000000000..dbefe8fd6b2 Binary files /dev/null and b/models/__pycache__/state.cpython-38.pyc differ diff --git a/models/__pycache__/user.cpython-38.pyc b/models/__pycache__/user.cpython-38.pyc new file mode 100644 index 00000000000..87c2adb8f83 Binary files /dev/null and b/models/__pycache__/user.cpython-38.pyc differ diff --git a/models/engine/__pycache__/__init__.cpython-38.pyc b/models/engine/__pycache__/__init__.cpython-38.pyc new file mode 100644 index 00000000000..5730256fa17 Binary files /dev/null and b/models/engine/__pycache__/__init__.cpython-38.pyc differ diff --git a/models/engine/__pycache__/db_storage.cpython-38.pyc b/models/engine/__pycache__/db_storage.cpython-38.pyc new file mode 100644 index 00000000000..15c2fc31ce1 Binary files /dev/null and b/models/engine/__pycache__/db_storage.cpython-38.pyc differ