Skip to content
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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__/
2 changes: 1 addition & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

Jennifer Huang <133@holbertonschool.com>
Alexa Orrico <210@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ 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)
Otetumo Oluwaseun Ayodele - [Github](https://https://github.com/annyauthe4) / [Twitter/X](https://x.com/annyauthe4)
Oluwafikunayomi Salu - [Github](https://github.com/fikayosalu) / [Twitter/X](https://x.com/Salufreeman)

Second part of Airbnb: Joann Vuong
## License
Expand Down
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Empty file added api/v1/__init__.py
Empty file.
Binary file added api/v1/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/__pycache__/app.cpython-38.pyc
Binary file not shown.
31 changes: 31 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python3
"""Create a JSON 404 error page."""

from api.v1.views import app_views
from models import storage
from flask import Flask, jsonify, make_response
import os


app = Flask(__name__)
app.url_map.strict_slashes = False
app.register_blueprint(app_views)


@app.teardown_appcontext
def tear_down(exc):
"""Cleans up after the response"""
storage.close()


@app.errorhandler(404)
def not_found(error):
"""Returns a JSON-formatted 404 status code"""
return make_response(jsonify({'error': 'Not found'}), 404)


if __name__ == "__main__":
host = os.getenv("HBNB_API_HOST", "0.0.0.0")
port = int(os.getenv("HBNB_API_PORT", 5000))

app.run(host=host, port=port, threaded=True, debug=True)
12 changes: 12 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/python3
""" Create an Instance of Blueprint """
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.amenities import *
from api.v1.views.users import *
from api.v1.views.places import *
from api.v1.views.cities import *
Binary file added api/v1/views/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/index.cpython-38.pyc
Binary file not shown.
76 changes: 76 additions & 0 deletions api/v1/views/amenities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#!/usr/bin/python3
""" Create a view for the Amenity Objects """


from models import storage
from models.amenity import Amenity
from api.v1.views import app_views
from flask import jsonify, request, abort


@app_views.route("/amenities", methods=["GET"])
def all_amenities():
""" Retrieve all Amenity objects """
amenities = storage.all(Amenity)
amenity_list = [amenity.to_dict() for amenity in amenities.values()]
return jsonify(amenity_list)


@app_views.route("/amenities/<amenity_id>", methods=["GET"])
def one_amenity(amenity_id):
""" Retrieves a Amenity Object """
amenities = storage.get(Amenity, amenity_id)
if not amenities:
abort(404)
return jsonify(amenities.to_dict())


@app_views.route("/amenities/<amenity_id>", methods=["DELETE"])
def delete_amenity(amenity_id):
""" Retrieves a Amenity Object """
one_amenity = storage.get(Amenity, amenity_id)
if not one_amenity:
abort(404)
storage.delete(one_amenity)
storage.save()
return {}, 200


@app_views.route("/amenities", methods=["POST"])
def create_amenity():
""" Create a new Amenity object """
try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400

if "name" not in data:
return jsonify({"error": "Missing name"}), 400

new_amenity = Amenity(name=data["name"])

storage.new(new_amenity)
storage.save()

return jsonify(new_amenity.to_dict()), 201


@app_views.route("/amenities/<amenity_id>", methods=["PUT"])
def update_amenity(amenity_id):
""" Update a Amenity object """
one_amenity = storage.get(Amenity, amenity_id)

if not one_amenity:
return jsonify({"error": "Not found"}), 404

try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400

for key, value in data.items():
if key not in ["id", "created_at", "updated_at"]:
setattr(one_amenity, key, value)

storage.save()
return jsonify(one_amenity.to_dict()), 200
82 changes: 82 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python3
"""Create a new view for City Objects."""


from api.v1.views import app_views
from flask import jsonify, request, abort
from models import storage
from models.city import City
from models.state import State


@app_views.route('/states/<state_id>/cities', methods=['GET'],
strict_slashes=False)
def get_all_cities(state_id):
"""Returns all cities by state id"""
state = storage.get(State, state_id)
if state is None:
abort(404)
return jsonify([city.to_json() for city in state.cities])


@app_views.route('/cities/<city_id>', methods['GET'],
strict_slashes=False)
def get_city(city_id):
"""Retrieves a city object by city id"""
city = storage.get("City", str(city_id))
if city is None:
abort(404)
return jsonify(city.to_json())


@app_views.route('/cities/<city_id>', methods=['DELETE'],
strict_slashes=False)
def delete_city(city_id):
"""Deletes a city object by id"""
city = storage.get("City", str(city_id))
if city is None:
abort(404)
storage.delete(city)
storage.save()
return jsonify({}), 200


@app_views.route('/states/<state_id>/cities', methods=['POST'],
strict_slashes=False)
def create_city(state_id):
"""Creates a new city object by state ID"""
state = storage.get("State", str(state_id))
if not state:
abort(404)
try:
json_data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400
if "name" not in json_data:
return jsonify({"error": "Missing name"}), 400
new_city = City(name=json_data["name"], state_id=state_id)
storage.new(new_city)
storage.save()

return jsonify(new_city.to_json()), 201


@app_views.route('/cities/<city_id>', methods=['PUT'], strict_slashes=False)
def update_city(city_id):
"""Updates an existing City object"""
city = storage.get(City, city_id)
if not city:
abort(404)
try:
json_data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400

ignored_keys = {"id", "state_id", "created_at", "updated_at"}
for key, value in json_data.items():
if key not in ignored_keys:
setattr(city, key, value)

storage.save()

return jsonify(city.to_dict()), 200
31 changes: 31 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/python3
""" Set up response for status code """
from api.v1.views import app_views
from flask import jsonify
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 import storage
from models.user import User


@app_views.route("/status")
def response_status():
""" Return status of the API """
return jsonify({"status": "OK"})

@app_views.route("/api/v1/stats", method=['GET'])
def get_object_stats():
"""Retrieves the number of each object in storage by type"""
classes = {
"amenities": Amenity,
"cities": City,
"places": Place,
"reviews": Review,
"states": State,
"users": User
}
counts = {key: storage.count(value) for key, value in classes.items()}
return jsonify(counts)
81 changes: 81 additions & 0 deletions api/v1/views/places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/python3
""" Create a view for the Place Objects """


from models import storage
from models.place import Place
from api.v1.views import app_views
from flask import jsonify, request, abort


@app_views.route("/places", methods=["GET"])
def all_places():
""" Retrieve all Place objects """
places = storage.all(Place)
place_list = [place.to_dict() for place in places.values()]
return jsonify(place_list)


@app_views.route("/places/<place_id>", methods=["GET"])
def one_place(place_id):
""" Retrieves a Place Object """
place = storage.get(Place, place_id)
if not place:
abort(404)
return jsonify(place.to_dict())


@app_views.route("/places/<place_id>", methods=["DELETE"])
def delete_place(place_id):
""" Retrieves a Place Object """
place = storage.get(Place, place_id)
if not place:
abort(404)
storage.delete(place)
storage.save()
return {}, 200


@app_views.route("/cities/<city_id>", methods=["POST"])
def create_place(city_id):
""" Create a new Place object """
city = storage.get(City, city_id)
if not city:
abort(404)
try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400

if "user_id" not in data:
return jsonify({"error": "Missing user_id"}), 400
if "name" not in data:
return jsonify({"error": "Missing name"}), 400

new_place = Place(name=data["name"], user_id=data["user_id"])

storage.new(new_place)
storage.save()

return jsonify(new_place.to_dict()), 201


@app_views.route("/places/<place_id>", methods=["PUT"])
def update_place(place_id):
""" Update a Place object """
one_place = storage.get(Place, place_id)

if not one_place:
return jsonify({"error": "Not found"}), 404

try:
data = request.get_json()
except Exception:
return jsonify({"error": "Not a JSON"}), 400

for key, value in data.items():
if key not in ["id", "created_at", "updated_at"]:
setattr(one_place, key, value)

storage.save()
return jsonify(one_place.to_dict()), 200
Loading