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

air bnb v4 #253

Open
wants to merge 12 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Empty file added api/__init__.py
Empty file.
Empty file added api/v1/__init__.py
Empty file.
45 changes: 45 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -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"))
15 changes: 15 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
75 changes: 75 additions & 0 deletions api/v1/views/amenities.py
Original file line number Diff line number Diff line change
@@ -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/<amenity_id>', 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/<amenity_id>', 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/<amenity_id>', 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
84 changes: 84 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -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/<state_id>/cities', methods=['GET'])
@app_views.route('/states/<state_id>/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/<state_id>/cities', methods=['POST'])
@app_views.route('/states/<state_id>/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/<city_id>', 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/<city_id>', 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/<city_id>', 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
46 changes: 46 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -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
Loading