Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rungene #5

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/v1/views/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Binary file modified api/v1/views/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file modified api/v1/views/__pycache__/index.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/states.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/users.cpython-38.pyc
Binary file not shown.
Empty file modified api/v1/views/index.py
100644 → 100755
Empty file.
97 changes: 97 additions & 0 deletions api/v1/views/places.py
Original file line number Diff line number Diff line change
@@ -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/<city_id>/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/<place_id>', 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/<place_id>', 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/<city_id>/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/<place_id>', 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
68 changes: 68 additions & 0 deletions api/v1/views/states.py
Original file line number Diff line number Diff line change
@@ -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/<state_id>', 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/<state_id>', 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/<state_id>', 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)
70 changes: 70 additions & 0 deletions api/v1/views/users.py
Original file line number Diff line number Diff line change
@@ -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/<user_id>', 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/<user_id>', 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/<user_id>', 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)