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

updated readme #3719

Open
wants to merge 51 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
fe700b0
updated readme
sameduTM Jan 16, 2025
fe2b9a2
status of API
sameduTM Jan 20, 2025
b74f496
set content type to json
sameduTM Jan 20, 2025
37beb7a
pep8
sameduTM Jan 20, 2025
228b010
remove wildcard import from top oof module
sameduTM Jan 20, 2025
c448cca
added more characters to function docs of get and count methods
sameduTM Jan 20, 2025
4c6cdc9
some stats task 4 commit
sameduTM Jan 20, 2025
54a7cc3
error 4o4 code response
sameduTM Jan 21, 2025
5a3f68b
State view that handles all default RESTFul API actions
sameduTM Jan 22, 2025
788f22a
E713 test for membership should be 'not in'
sameduTM Jan 22, 2025
3f00528
changed the error response code
sameduTM Jan 22, 2025
5ea83cf
removed pycache
sameduTM Jan 22, 2025
b167ba1
use abort instead of make_response
sameduTM Jan 22, 2025
7f6bd0a
Correct output for get with FileStorage and Object existing
sameduTM Jan 23, 2025
1ee080a
changed return value of get method in state module to dict
sameduTM Jan 23, 2025
403e643
indentation error fixed in index.py file
sameduTM Jan 23, 2025
ad75076
rewrote the code by shrinking it
sameduTM Jan 23, 2025
3d57538
city view code updated
sameduTM Jan 24, 2025
b3e8eb5
state without cities listed
sameduTM Jan 24, 2025
4a845eb
not existing state for PUT
sameduTM Jan 24, 2025
0cb52a9
amenities view added
sameduTM Jan 24, 2025
ad114ba
returnn value for delAmn function changed to JSON
sameduTM Jan 24, 2025
7bb03bc
pep8 check
sameduTM Jan 24, 2025
29176f4
updateAmenity method for amenity view
sameduTM Jan 24, 2025
28621cc
get amenities view
sameduTM Jan 25, 2025
2db3944
user view coded
sameduTM Jan 25, 2025
5353fcb
new view for place
sameduTM Jan 25, 2025
8240be4
pep8 check
sameduTM Jan 25, 2025
0fd3d82
pep8 check
sameduTM Jan 25, 2025
29c5144
push to start
sameduTM Jan 25, 2025
05cbe49
updates place handles if place_id is not linked
sameduTM Jan 26, 2025
b726998
review views
sameduTM Jan 26, 2025
7f78f74
review views ammended
sameduTM Jan 26, 2025
fdf1fec
HTTP access control(CORS)
sameduTM Jan 29, 2025
6fe7b9b
jsonify list of all users
sameduTM Jan 29, 2025
23281db
returnn correct 404 response
sameduTM Jan 29, 2025
a9ead83
correct JSON response
sameduTM Jan 29, 2025
d7b1fb5
use loop for dictionary kwargs
sameduTM Jan 29, 2025
a3f0fe7
jsonify list of all amenities
sameduTM Jan 29, 2025
aa9066b
get everything within the loop
sameduTM Jan 29, 2025
f4826ed
pep8 fixes
sameduTM Jan 29, 2025
d2a2ae4
introduce loops
sameduTM Jan 29, 2025
361ae49
hash passwords
sameduTM Jan 30, 2025
ecb9f2f
pep8
sameduTM Jan 30, 2025
f3733f6
pep8
sameduTM Jan 30, 2025
01fb884
hash passwords
sameduTM Jan 30, 2025
78bf5ab
Place-Amenity relationship
sameduTM Jan 30, 2025
7efb057
renamed places_amenity file
sameduTM Jan 30, 2025
e003bba
documented module
sameduTM Jan 30, 2025
ce31071
add delete method
sameduTM Jan 30, 2025
d306acc
fix pep8
sameduTM Jan 30, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@

Jennifer Huang <[email protected]>
Alexa Orrico <[email protected]>
Joann Vuong <[email protected]>
Joann Vuong <[email protected]>
Ken Wekesa - [Github] (https://github.com/sameduTM)
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)
Ken Wekesa - [Github] (https://github.com/sameduTM)

Second part of Airbnb: Joann Vuong
## License
Expand Down
Binary file added __pycache__/test.cpython-312.pyc
Binary file not shown.
Empty file added api/__init__.py
Empty file.
Binary file added api/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Empty file added api/v1/__init__.py
Empty file.
Binary file added api/v1/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file added api/v1/__pycache__/app.cpython-312.pyc
Binary file not shown.
36 changes: 36 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/usr/bin/python3
"""main app of API"""
from flask import Flask
from flask import abort
from flask import make_response
from flask import jsonify
from models import storage
from api.v1.views import app_views
from flask_cors import CORS
import os
import json

app = Flask(__name__)
CORS(app, origins="0.0.0.0")
app.register_blueprint(app_views)

api_host = os.getenv("HBNB_API_HOST", default='0.0.0.0')
api_port = os.getenv("HBNB_API_PORT", default=5000)


@app.teardown_appcontext
def teardown_appcontext(exception):
"""close storage"""
storage.close()


@app.errorhandler(404)
def page_not_found(error):
"""a handler for 404 errors that returns a
JSON-formatted 404 status code response.
"""
return make_response(jsonify({"error": "Not found"}), 400)


if __name__ == "__main__":
app.run(host=api_host, port=api_port, threaded=True, debug=True)
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
"""init file for views"""
from flask import Blueprint


app_views = Blueprint('app_views', __name__,
url_prefix='/api/v1')
from api.v1.views.index import *
from api.v1.views import states
from api.v1.views import cities
from api.v1.views import amenities
from api.v1.views import users
from api.v1.views import places
from api.v1.views import places_reviews
from api.v1.views import places_amenities
Binary file added api/v1/views/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file added api/v1/views/__pycache__/cities.cpython-312.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/index.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file added api/v1/views/__pycache__/places.cpython-312.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added api/v1/views/__pycache__/states.cpython-312.pyc
Binary file not shown.
Binary file added api/v1/views/__pycache__/users.cpython-312.pyc
Binary file not shown.
81 changes: 81 additions & 0 deletions api/v1/views/amenities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/python3
"""view for Amenity objects that handles all default RESTFul API actions"""
from api.v1.views import app_views
from models import storage
from models.amenity import Amenity
from flask import make_response
from flask import request
from flask import jsonify


@app_views.route('/amenities', methods=['GET'], strict_slashes=False)
def getAmenities():
"""retrieve the list of all Amenity objects"""
all_amenities = storage.all(Amenity)

list_amenities = [val.to_dict() for val in all_amenities.values()]
return jsonify(list_amenities)


@app_views.route('/amenities/<amenity_id>', methods=['GET'],
strict_slashes=False)
def getAmenity(amenity_id):
"""retrieve Amenity object"""
all_amn = storage.all(Amenity)
for val in all_amn.values():
if val.id == amenity_id:
return jsonify(val.to_dict())
return make_response({"error": "Not found"}, 404)


@app_views.route('/amenities/<amenity_id>', methods=['DELETE'],
strict_slashes=False)
def deleteAmenity(amenity_id):
"""delete a Amenity object"""
all_amn = storage.all(Amenity)
for val in all_amn.values():
if val.id == amenity_id:
storage.delete(val)
storage.save()
return make_response(jsonify({}), 200)
return make_response({"error": "Not found"}, 404)


@app_views.route('/amenities', methods=['POST'], strict_slashes=False)
def createAmenity():
"""create a Amenity object"""
all_amn = storage.all(Amenity)
new_amn = Amenity()
try:
data = request.get_json()
if "name" not in data:
return make_response("Missing name", 400)
for key, val in data.items():
setattr(new_amn, key, val)
storage.new(new_amn)
storage.save()
return make_response(jsonify(new_amn.to_dict()), 201)
except Exception as e:
return make_response("Not a JSON", 400)


@app_views.route('/amenities/<amenity_id>', methods=['PUT'],
strict_slashes=False)
def updateAmenity(amenity_id):
"""update Amenity object"""
all_amn = storage.all(Amenity)
all_ids = []
for val in all_amn.values():
all_ids.append(val.id)
if amenity_id not in all_ids:
return make_response(jsonify({"error": "Not found"}), 404)
try:
data = request.get_json()
for v in all_amn.values():
if amenity_id == v.id:
for key, value in data.items():
setattr(v, key, value)
storage.save()
return make_response(jsonify(v.to_dict()), 200)
except Exception as e:
return make_response("Not a JSON", 400)
94 changes: 94 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#!/usr/bin/python3
"""view for City objects that handles all default RESTFul API actions"""
from models import storage
from models.city import City
from models.state import State
from api.v1.views import app_views
from flask import make_response
from flask import jsonify
from flask import request


@app_views.route('/states/<state_id>/cities', methods=['GET'],
strict_slashes=False)
def getCities(state_id):
"""retrieves the list of all City objects of a State"""
city_list = []
state_ids = []
all_states = storage.all(State)
all_cities = storage.all(City)
for val in all_states.values():
state_ids.append(val.id)
if state_id not in state_ids:
return make_response({"error": "Not found"}, 404)
for val in all_cities.values():
if state_id == val.state_id:
city_list.append(val.to_dict())
return jsonify(city_list)


@app_views.route('/cities/<city_id>')
def getCity(city_id):
"""retrieve a City object"""
all_cities = storage.all(City)
for key, val in all_cities.items():
if val.id == city_id:
return jsonify(val.to_dict())
return make_response(jsonify({"error": "Not found"}), 404)


@app_views.route('/cities/<city_id>', methods=['DELETE'],
strict_slashes=False)
def deleteCity(city_id):
"""delete a City object"""
all_cities = storage.all(City)
for key, val in all_cities.items():
if city_id == val.id:
storage.delete(val)
storage.save()
return make_response(jsonify({}), 200)
return make_response(jsonify({"error": "Not found"}), 404)


@app_views.route('/states/<state_id>/cities', methods=['POST'],
strict_slashes=False)
def createCity(state_id):
"""create a City object"""
from models.city import City
from models.state import State
try:
data = request.get_json()
if "name" not in data:
return make_response("Missing name", 400)
except Exception as e:
return make_response("Not a JSON", 400)
all_states = storage.all(State)
state_ids = []
for val in all_states.values():
state_ids.append(val.id)
if state_id not in state_ids:
return make_response({"error": "Not found"}, 404)
new_city = City()
data['state_id'] = state_id
for key, value in data.items():
setattr(new_city, key, value)
storage.new(new_city)
storage.save()
return make_response(jsonify(new_city.to_dict()), 201)


@app_views.route('/cities/<city_id>', methods=['PUT'], strict_slashes=False)
def updateCity(city_id):
"""update a City object"""
all_cities = storage.all(City)
try:
data = request.get_json()
for val in all_cities.values():
if city_id == val.id:
for key, value in data.items():
setattr(val, key, value)
storage.save()
return make_response(val.to_dict(), 200)
return make_response({"error": "Not found"}, 404)
except Exception as e:
return make_response("Not a JSON", 400)
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
"""index file for views"""
from api.v1.views import app_views
from flask import jsonify, json, current_app


@app_views.route('/status')
def status():
"""route `/status` on the object app_views that returns a JSON"""
o_dct = {"status": "OK"}
return jsonify(o_dct)


@app_views.route('/stats')
def stats():
"""endpoint that retrieves the number of each objects by type"""
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
stats_dict = {"amenities": storage.count(Amenity),
"cities": storage.count(City),
"places": storage.count(Place),
"reviews": storage.count(Review),
"states": storage.count(State),
"users": storage.count(User),
}
return jsonify(stats_dict)
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
"""view for Place objects that handles all default RESTFul API actions"""
from api.v1.views import app_views
from flask import jsonify
from flask import make_response
from models import storage
from models.place import Place
from models.city import City
from models.user import User
from flask import request


@app_views.route('/cities/<city_id>/places', methods=['GET'],
strict_slashes=False)
def getPlaces(city_id):
"""retrieve list of all Place objects of a City"""
all_places = storage.all(Place)
all_cities = storage.all(City)
places_list = []
city_ids = [val.id for val in all_cities.values()]
if city_id not in city_ids:
return make_response(jsonify({"error": "Not found"}), 404)
for val in all_places.values():
if val.city_id == city_id:
places_list.append(val.to_dict())
return jsonify(places_list)


@app_views.route('/places/<place_id>', methods=['GET'], strict_slashes=False)
def getPlace(place_id):
"""retrieve a Place object"""
all_places = storage.all(Place)
for val in all_places.values():
if val.id == place_id:
return jsonify(val.to_dict())
return make_response(jsonify({"error": "Not found"}), 404)


@app_views.route('/places/<place_id>', methods=['DELETE'],
strict_slashes=False)
def deletePlace(place_id):
"""delete a Place object"""
all_places = storage.all(Place)
for val in all_places.values():
if val.id == place_id:
storage.delete(val)
storage.save()
return make_response(jsonify({}), 200)
return make_response(jsonify({"error": "Not found"}), 404)


@app_views.route('/cities/<city_id>/places', methods=['POST'],
strict_slashes=False)
def createPlace(city_id):
"""create a Place"""
all_cities = storage.all(City)
all_users = storage.all(User)
places_list = []
city_ids = [val.id for val in all_cities.values()]
user_ids = [val.id for val in all_users.values()]
if city_id not in city_ids:
return make_response(jsonify({"error": "Not found"}), 404)
try:
data = request.get_json()
if "user_id" not in data:
return make_response("Missing user_id", 400)
if data['user_id'] not in user_ids:
return make_response(jsonify({"error": "Not found"}), 404)
if "name" not in data:
return make_response("Missing name", 400)
data['city_id'] = city_id
new_place = Place(**data)
for key, value in data.items():
setattr(new_place, key, value)
storage.new(new_place)
storage.save()
return make_response(jsonify(new_place.to_dict()), 201)
except Exception as e:
print(e)
return make_response("Not a JSON", 400)


@app_views.route('/places/<place_id>', methods=['PUT'], strict_slashes=False)
def updatePlace(place_id):
"""update a Place object"""
all_places = storage.all(Place)
try:
data = request.get_json()
for val in all_places.values():
if val.id == place_id:
for key, value in data.items():
setattr(val, key, value)
storage.save()
return make_response(jsonify(val.to_dict()), 200)
return make_response(jsonify({"error": "Not found"}), 404)
except Exception as e:
return make_response("Not a JSON", 400)
Loading