diff --git a/api/v1/views/__init__.py b/api/v1/views/__init__.py old mode 100644 new mode 100755 index 0b40d08407d..ad8ecf0c960 --- a/api/v1/views/__init__.py +++ b/api/v1/views/__init__.py @@ -9,3 +9,5 @@ # import views and register them with the Blueprint if (__name__ == 'api.v1.views'): from api.v1.views.index import * + from api.v1.views.states import * + from api.v1.views.users import * diff --git a/api/v1/views/__pycache__/__init__.cpython-38.pyc b/api/v1/views/__pycache__/__init__.cpython-38.pyc index 9ddd11b0e2c..0e58ecdcdd5 100644 Binary files a/api/v1/views/__pycache__/__init__.cpython-38.pyc and b/api/v1/views/__pycache__/__init__.cpython-38.pyc differ diff --git a/api/v1/views/__pycache__/index.cpython-38.pyc b/api/v1/views/__pycache__/index.cpython-38.pyc index 99adb4fd05b..264e95684f5 100644 Binary files a/api/v1/views/__pycache__/index.cpython-38.pyc and b/api/v1/views/__pycache__/index.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..aaa4fbf955d 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..2317ce7a643 Binary files /dev/null and b/api/v1/views/__pycache__/users.cpython-38.pyc differ 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 new file mode 100644 index 00000000000..036e3181941 --- /dev/null +++ b/api/v1/views/places.py @@ -0,0 +1,97 @@ +#!/usr/bin/python3 +"""Places module.Handles Places RESTFul API actions""" +from flask import jsonify, Blueprint, request, abort +from api.v1.views import app_views +from models import storage +from models.city import City +from models.place import Place + + +@app_views.route('/cities//places', methods=['GET'], + strict_slashes=False) +def get_city_places(city_id): + """Retrieves the list of all Place objects of a City""" + city = storage.get("City", city_id) + if city is None: + abort(404) + val = [] + for place in city.places: + val.append(place.to_dict()) + return jsonify(places) + + +@app_views.route('/places/', methods=['GET'], strict_slashes=False) +def get_place(place_id): + """Retrives a place object based on id""" + place = storage.get("PLace", place_id) + if place: + return jsonify(place.to_dict()) + else: + abort(404) + + +@app_views.route('/placess/', methods=['DELETE'], + strict_slashes=False) +def delete_place(place_id): + """Deletes a Place object""" + place = storage.get("Place", place_id) + if place: + storage.delete(place) + storage.save() + return jsonify({}), 200 + else: + abort(404) + + +@app_views.route('/cities//places', methods=['POST'], + strict_slashes=False) +def create_place(): + """Creates a Place Object""" + city = storage.get('City', city_id) + if city is None: + abort(404) + post_data = request.get_json() + if post_data is None or type(post_data) != dict: + abort(400, "Not a JSON") + user_id = post_data.get('user_id') + if user_id is None: + abort(400, "Missing user_id") + user = storage.get('User', user_id) + if user is None: + abort(404) + name = post_data.get('name') + if name is None: + abort(400, "Missing name") + place = Place(city_id=city_id, user_id=user_id, name=name) + storage.new(place) + storage.save() + + return jsonify(place.to_dict()), 201 + + +@app_views.route('/places/', methods=['PUT'], strict_slashes=False) +def update_place(place_id): + """Updates a Place object""" + place = storage.get("Place", place_id) + if place is None: + abort(404) + put_data = storage.get_json() + if put_data is None or type(put_data) != dict: + abort(400, "Not a JSON") + + ignored_keys = ['id', 'user_id', 'city_id', 'created_at', 'updated_at'] + for key, value in put_data.items(): + if key not in ignored_keys: + setattr(place, key, value) + storage.save() + return jsonify(place.to_dict()), 200 + if state: + if not request.json: + return jsonify({'error': 'Not a JSON'}), 400 + put_data = request.get_json() + ignore_keys = ['id', 'created_at', 'updated_at'] + for k, v in put_data.items(): + if k not in ignore_keys: + setattr(state, k, v) + state.save() + return jsonify({state.to_dict()}), 200 diff --git a/api/v1/views/states.py b/api/v1/views/states.py new file mode 100755 index 00000000000..4cc4bdb9392 --- /dev/null +++ b/api/v1/views/states.py @@ -0,0 +1,68 @@ +#!/usr/bin/python3 +"""states module.Handles States RESTFul API actions""" +from flask import jsonify, Blueprint, request, abort +from api.v1.views import app_views +from models import storage +from models.state import State + + +@app_views.route('/states', methods=['GET'], strict_slashes=False) +def get_states(): + """Retrieves the list of all State objects""" + states = storage.all('State').values() + states = [state.to_dict() for state in states] + return jsonify(states) + + +@app_views.route('/states/', methods=['GET'], strict_slashes=False) +def get_state(state_id): + """Retives a state object based on id""" + state = storage.get("State", state_id) + if state: + return jsonify(state.to_dict()) + else: + abort(404) + + +@app_views.route('/states/', methods=['DELETE'], + strict_slashes=False) +def delete_state(state_id): + """Deletes a State object""" + state = storage.get("State", state_id) + if state: + storage.delete(state) + storage.save() + return jsonify({}), 200 + else: + abort(404) + + +@app_views.route('/states', methods=['POST'], strict_slashes=False) +def create_state(): + """Creates a State Object""" + if not request.json: + return jsonify({'error': 'Not a JSON'}), 400 + if 'name' not in request.json: + return jsonify({'error': 'Missing name'}), 400 + post_data = request.get_json() + new_state = State(**post_data) + new_state.save() + return jsonify(new_state.to_dict()), 201 + + +@app_views.route('/states/', methods=['PUT'], strict_slashes=False) +def update_state(state_id): + """Updates a State object""" + state = storage.get("State", state_id) + if state: + if not request.json: + return jsonify({'error': 'Not a JSON'}), 400 + put_data = request.get_json() + ignore_keys = ['id', 'created_at', 'updated_at'] + for k, v in put_data.items(): + if k not in ignore_keys: + setattr(state, k, v) + storage.save() + return jsonify(state.to_dict()), 200 + else: + abort(404) diff --git a/api/v1/views/users.py b/api/v1/views/users.py new file mode 100755 index 00000000000..8c6c0a1c6b1 --- /dev/null +++ b/api/v1/views/users.py @@ -0,0 +1,70 @@ +#!/usr/bin/python3 +"""users module.Handles User RESTFul API actions""" +from flask import jsonify, Blueprint, request, abort +from api.v1.views import app_views +from models import storage +from models.user import User + + +@app_views.route('/users', methods=['GET'], strict_slashes=False) +def get_users(): + """Retrieves the list of all User objects""" + users = storage.all('User').values() + users = [user.to_dict() for user in users] + return jsonify(users) + + +@app_views.route('/users/', methods=['GET'], strict_slashes=False) +def get_user(user_id): + """Retrives a user object based on id""" + user = storage.get("User", user_id) + if user: + return jsonify(user.to_dict()) + else: + abort(404) + + +@app_views.route('/users/', methods=['DELETE'], + strict_slashes=False) +def delete_user(user_id): + """Deletes a User object""" + user = storage.get("User", user_id) + if user: + storage.delete(user) + storage.save() + return jsonify({}), 200 + else: + abort(404) + + +@app_views.route('/users', methods=['POST'], strict_slashes=False) +def create_user(): + """Creates a User Object""" + if not request.json: + return jsonify({'error': 'Not a JSON'}), 400 + if 'email' not in request.json: + return jsonify({'error': 'Missing email'}), 400 + if 'password' not in request.json: + return jsonify({'error': 'Missing password'}) + post_data = request.get_json() + new_user = User(**post_data) + new_user.save() + return jsonify(new_user.to_dict()), 201 + + +@app_views.route('/users/', methods=['PUT'], strict_slashes=False) +def update_user(user_id): + """Updates a User object""" + user = storage.get("User", user_id) + if user: + if not request.json: + return jsonify({'error': 'Not a JSON'}), 400 + put_data = request.get_json() + ignore_keys = ['id', 'created_at', 'updated_at'] + for k, v in put_data.items(): + if k not in ignore_keys: + setattr(user, k, v) + user.save() + return jsonify({user.to_dict()}), 200 + else: + abort(404)