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

airbnb_v3 done #3687

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
e88e213
fixed tests
EgyAbdo10 Aug 4, 2024
6fe9de8
added or modified a file
EgyAbdo10 Aug 4, 2024
f65eb7a
added or modified a file
EgyAbdo10 Aug 4, 2024
d3a20e4
added or modified a file
EgyAbdo10 Aug 4, 2024
b59e0ad
added or modified a file
EgyAbdo10 Aug 4, 2024
5943957
added or modified a file
EgyAbdo10 Aug 4, 2024
b093552
added or modified a file
EgyAbdo10 Aug 4, 2024
6d4006d
added or modified a file
EgyAbdo10 Aug 4, 2024
3fa0305
added or modified a file
EgyAbdo10 Aug 4, 2024
07c988b
added or modified a file
EgyAbdo10 Aug 4, 2024
8f19596
added or modified a file
EgyAbdo10 Aug 5, 2024
7bc90e2
added or modified a file
EgyAbdo10 Aug 5, 2024
6353d4c
added or modified a file
EgyAbdo10 Aug 5, 2024
00bab2f
added or modified a file
EgyAbdo10 Aug 5, 2024
a5d3e26
added or modified a file
EgyAbdo10 Aug 5, 2024
90d6b34
added or modified a file
EgyAbdo10 Aug 5, 2024
f2b92fe
added or modified a file
EgyAbdo10 Aug 5, 2024
a00c11b
added or modified a file
EgyAbdo10 Aug 6, 2024
ccb7c6d
added or modified a file
EgyAbdo10 Aug 6, 2024
b60a4ca
added or modified a file
EgyAbdo10 Aug 6, 2024
abbaac6
added or modified a file
EgyAbdo10 Aug 6, 2024
4be1b4f
added or modified a file
EgyAbdo10 Aug 6, 2024
903f22d
added or modified a file
EgyAbdo10 Aug 6, 2024
4827a10
added or modified a file
EgyAbdo10 Aug 6, 2024
2e4eec5
added or modified a file
EgyAbdo10 Aug 6, 2024
bc7b50c
added or modified a file
EgyAbdo10 Aug 6, 2024
b52280b
added or modified a file
EgyAbdo10 Aug 6, 2024
1497335
added or modified a file
EgyAbdo10 Aug 6, 2024
5bac269
added or modified a file
EgyAbdo10 Aug 6, 2024
80bdc54
added or modified a file
EgyAbdo10 Aug 6, 2024
822ceec
added or modified a file
EgyAbdo10 Aug 6, 2024
60ded63
added or modified a file
EgyAbdo10 Aug 6, 2024
0156dad
added or modified a file
EgyAbdo10 Aug 6, 2024
5a11d25
added or modified a file
EgyAbdo10 Aug 6, 2024
e98be48
added or modified a file
EgyAbdo10 Aug 7, 2024
88c1e9e
added or modified a file
EgyAbdo10 Aug 7, 2024
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 AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
Jennifer Huang <[email protected]>
Alexa Orrico <[email protected]>
Joann Vuong <[email protected]>
Abdelmoneim Maher <[email protected]>
Binary file added __pycache__/console.cpython-38.pyc
Binary file not shown.
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.
40 changes: 40 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/usr/bin/python3
"""Flask app main module"""


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


app = Flask(__name__)
CORS(app, resources={"/*": {"origins": "0.0.0.0"}})

app.register_blueprint(app_views)


@app.teardown_appcontext
def shutdown(exception=None):
"""close storage after every request"""
storage.close()


@app.errorhandler(404)
def handle_not_found(error):
"""return a not found response"""
return make_response(jsonify({"error": "Not found"}), 404)


if __name__ == "__main__":
HBNB_API_HOST = "0.0.0.0"
HBNB_API_PORT = 5000

if getenv("HBNB_API_HOST"):
HBNB_API_HOST = getenv("HBNB_API_HOST")

if getenv("HBNB_API_PORT"):
HBNB_API_PORT = getenv("HBNB_API_PORT")

app.run(host=HBNB_API_HOST, port=HBNB_API_PORT, threaded=True)
19 changes: 19 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/python3
"""initialize a blueprint"""
from flask import Blueprint
# from flask_restful import Api

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 *

# api = Api(app_views)

# api.add_resource(State_view_all, "/states/")
# api.add_resource(State_view, "/states/<state_id>")
Binary file added api/v1/views/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/amenities.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/cities.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/index.cpython-38.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/places.cpython-38.pyc
Binary file not shown.
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.
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
"""modify Amenity objects via APIs"""
from models.amenity import Amenity
from flask import jsonify, make_response, abort, request
from models import storage
from api.v1.views import app_views


def abortNotExists(cls, obj_id):
"""exit with a 404 status if obj not found"""
obj = storage.get(cls, obj_id)
if obj is None:
abort(404)
return obj


@app_views.route("/amenities", strict_slashes=False, methods=["GET"])
@app_views.route("/amenities/<amenity_id>",
strict_slashes=False, methods=["GET"])
def get_amenities(amenity_id=None):
"""retrieve Amenity objects"""
if not amenity_id:
objs_dict = storage.all(Amenity)
obj_list = []
for obj in objs_dict.values():
obj_list.append(obj.to_dict())
return make_response(jsonify(obj_list))

else:
obj = abortNotExists(Amenity, amenity_id)
return make_response(jsonify(obj.to_dict()))


@app_views.route("/amenities", strict_slashes=False, methods=["POST"])
def post_amenities():
"""create a new amenity object"""
try:
data = request.get_json()
except Exception:
abort(400, description="Not a JSON")
if "name" not in data.keys():
abort(400, description="Missing name")

new_obj = Amenity(**data)
storage.new(new_obj)
storage.save()
return make_response(new_obj.to_dict(), 201)


@app_views.route("/amenities/<amenity_id>", strict_slashes=False,
methods=["DELETE"])
def delete_amenity(amenity_id):
"""DELETE /api/v1/amenities/<amenity_id>"""
obj = abortNotExists(Amenity, amenity_id)
storage.delete(obj)
storage.save()
return make_response({}, 200)


@app_views.route("/amenities/<amenity_id>",
strict_slashes=False, methods=["PUT"])
def put_amenity(amenity_id):
"""Updates a amenity_id object: PUT /api/v1/amenities/<amenity_id>"""
obj = abortNotExists(Amenity, amenity_id)
try:
data = request.get_json()
except Exception:
abort(400, description="Not a JSON")

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

storage.save()
return make_response(obj.to_dict(), 200)
89 changes: 89 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/python3
"""modify state objects via APIs"""
from models.state import State
from models.city import City
from flask import jsonify, make_response, abort, request
from models import storage
from api.v1.views import app_views


def abortNotExists(cls, obj_id):
"""exit with a 404 status if obj not found"""
obj = storage.get(cls, obj_id)
if obj is None:
abort(404)
return obj


@app_views.route("/states/<state_id>/cities",
strict_slashes=False, methods=["GET"])
def get_state_cities(state_id=None):
"""etrieves the list of all City objects of a State:
GET /api/v1/states/<state_id>/cities
"""
state = abortNotExists(State, state_id)
cities_dicts_list = []
for city in state.cities:
cities_dicts_list.append(city.to_dict())
return jsonify(cities_dicts_list)


@app_views.route("/cities/<city_id>", strict_slashes=False, methods=["GET"])
def get_city(city_id=None):
"""retrieve a city object"""
# if not city_id:
# objs_dict = storage.all(City)
# obj_list = []
# for obj in objs_dict.values():
# obj_list.append(obj.to_dict())
# return make_response(jsonify(obj_list))

# else:
obj = abortNotExists(City, city_id)
return make_response(jsonify(obj.to_dict()))


@app_views.route("/states/<state_id>/cities",
strict_slashes=False, methods=["POST"])
def post_city(state_id):
"""Creates a City: POST /api/v1/states/<state_id>/cities"""
abortNotExists(State, state_id)
try:
data = request.get_json()
data["state_id"] = state_id
except Exception:
abort(400, description="Not a JSON")
if "name" not in data.keys():
abort(400, description="Missing name")

new_obj = City(**data)
storage.new(new_obj)
storage.save()
return make_response(new_obj.to_dict(), 201)


@app_views.route("/cities/<city_id>", strict_slashes=False,
methods=["DELETE"])
def delete_city(city_id):
"""Deletes a City object:: DELETE /api/v1/cities/<city_id>"""
obj = abortNotExists(City, city_id)
storage.delete(obj)
storage.save()
return make_response({}, 200)


@app_views.route("/cities/<city_id>", strict_slashes=False, methods=["PUT"])
def put_city(city_id):
"""Updates a City object: PUT /api/v1/cities/<city_id>"""
obj = abortNotExists(City, city_id)
try:
data = request.get_json()
except Exception:
abort(400, description="Not a JSON")

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

storage.save()
return make_response(obj.to_dict(), 200)
38 changes: 38 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/python3
"""routes of the app_view blueprint"""
# from api.v1.views import app_views avoid circular import
from flask import jsonify, make_response # , make_response
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
from api.v1.views import app_views


classes = {
"amenities": Amenity,
"cities": City,
"places": Place,
"reviews": Review,
"states": State,
"users": User
}


@app_views.route("/status", strict_slashes=False, methods=["GET"])
def get_status():
"""return status OK"""
return jsonify({"status": "OK"})


@app_views.route("/stats", strict_slashes=False, methods=["GET"])
def get_stats():
"""get objects count"""
stats = {}
for cls_name, cls in classes.items():
stats[cls_name] = storage.count(cls)

return make_response(jsonify(stats))
87 changes: 87 additions & 0 deletions api/v1/views/places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/usr/bin/python3
"""modify place objects via APIs"""
from models.place import Place
from models.city import City
from models.user import User
from flask import jsonify, make_response, abort, request
from models import storage
from api.v1.views import app_views


def abortNotExists(cls, obj_id):
"""exit with a 404 status if obj not found"""
obj = storage.get(cls, obj_id)
if obj is None:
abort(404)
return obj


@app_views.route("/cities/<city_id>/places",
strict_slashes=False, methods=["GET"])
def get_city_places(city_id=None):
"""Retrieves the list of all Place objects of a City:
GET /api/v1/cities/<city_id>/places
"""
city = abortNotExists(City, city_id)
places_dicts_list = []
for place in city.places:
places_dicts_list.append(place.to_dict())
return jsonify(places_dicts_list)


@app_views.route("/places/<place_id>", strict_slashes=False,
methods=["GET"])
def get_places(place_id):
"""Retrieves a Place object. : GET /api/v1/places/<place_id>"""
obj = abortNotExists(Place, place_id)
return jsonify(obj.to_dict())


@app_views.route("/cities/<city_id>/places",
strict_slashes=False, methods=["POST"])
def post_places(city_id):
"""create a new place object"""
abortNotExists(City, city_id)
try:
data = request.get_json()
data["city_id"] = city_id
except Exception:
abort(400, description="Not a JSON")
if "user_id" not in data.keys():
abort(400, description="Missing user_id")

abortNotExists(User, data["user_id"]) # check for user id if it exists
if "name" not in data.keys():
abort(400, description="Missing name")
new_obj = Place(**data)
storage.new(new_obj)
storage.save()
return make_response(new_obj.to_dict(), 201)


@app_views.route("/places/<place_id>", strict_slashes=False,
methods=["DELETE"])
def delete_place(place_id):
"""Deletes a Place object: DELETE /api/v1/places/<place_id>"""
obj = abortNotExists(Place, place_id)
storage.delete(obj)
storage.save()
return make_response({}, 200)


@app_views.route("/places/<place_id>", strict_slashes=False, methods=["PUT"])
def put_place(place_id):
"""Updates a place object: PUT /api/v1/places/<place_id>"""
obj = abortNotExists(Place, place_id)
try:
data = request.get_json()
except Exception:
abort(400, description="Not a JSON")

for key, val in data.items():
if key not in ["id", "user_id", "city_id",
"created_at", "updated_at"]:
setattr(obj, key, val)

storage.save()
return make_response(obj.to_dict(), 200)
Loading