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

merging storage_get_count bracnh #3677

Open
wants to merge 8 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)
Kyakuwa Peninnah - [Github] (https://github.com/KyakuwaPeninnah)

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.
33 changes: 33 additions & 0 deletions api/v1/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python3
'''Flask app'''


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


app = Flask(__name__)
app.register_blueprint(app_views)
cors = CORS(app, resources={r'/*': {'origins': '0.0.0.0'}})


@app.teardown_appcontext
def teardown(exception):
'''something should happen when the app is getting teared down'''
if storage is not None:
storage.close()


@app.errorhandler(404)
def errorhandler(error):
'''handling 404 error'''
return make_response(jsonify({'error': 'Not found'}), 404)


if __name__ == '__main__':
host = os.getenv('HBNB_API_HOST', '0.0.0.0')
port = os.getenv('HBNB_API_PORT', '5000')
app.run(host=host, port=port, threaded=True)
16 changes: 16 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/python3
'''init in 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.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 *
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 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.
77 changes: 77 additions & 0 deletions api/v1/views/amenities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/python3
'''amenities module'''

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


@app_views.route('/amenities', methods=['GET'], strict_slashes=False)
def getamenities():
'''get all amenities'''
amens = storage.all(Amenity)
return jsonify([amen.to_dict() for amen in amens.values()])


@app_views.route('/amenities/<amenity_id>',
methods=['GET'],
strict_slashes=False)
def getAmenityById(amenity_id=None):
'''gets amenity by id'''
if amenity_id is None:
abort(404)
amen = storage.get(Amenity, amenity_id)
if amen is None:
abort(404)
return jsonify(amen.to_dict())


@app_views.route('/amenities/<amenity_id>',
methods=['DELETE'],
strict_slashes=False)
def deleteAmenity(amenity_id=None):
'''deletes an amenity'''
if amenity_id is not None:
result = storage.get(Amenity, amenity_id)
if res is not None:
storage.delete(result)
storage.save()
return make_response(jsonify({}), 200)
abort(404)


@app_views.route('/amenities',
methods=['POST'],
strict_slashes=False)
def postAmenity():
'''posts a new amenity'''
body = request.get_json()
if body is None:
abort(400, 'Not a JSON')
if 'name' not in body.keys():
abort(400, 'Missing name')
amen = Amenity(**body)
amen.save()
return make_response(jsonify(amen.to_dict()), 201)


@app_views.route('/amenities/<amenity_id>',
methods=['PUT'],
strict_slashes=False)
def updateAmenity(amenity_id=None):
'''updates amenity'''
if amenity_id is None:
abort(404)
obj = storage.get(Amenity, amenity_id)
if obj is None:
abort(404)

body = request.get_json()
if body is None:
abort(400, 'Not a JSON')
for key in body.keys():
if key not in ['id', 'created_at', 'updated_at']:
setattr(obj, key, body[key])
obj.save()
return make_response(jsonify(obj.to_dict()), 200)
88 changes: 88 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/python3
'''cities blueprint'''

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


@app_views.route('/cities/<city_id>', methods=['GET'], strict_slashes=False)
def getCity(city_id=None):
'''get a city with the id'''
if city_id is None:
abort(404)
ct = storage.get(City, city_id)
if ct is None:
abort(404)
return jsonify(ct.to_dict())


@app_views.route('/states/<state_id>/cities',
methods=['GET'],
strict_slashes=False)
def getCitiesInState(state_id=None):
'''gets all cities in state with the id passed'''
if state_id is None:
abort(404)
st = storage.get(State, state_id)
if st is None:
abort(404)
cts = st.cities
return jsonify([ct.to_dict() for ct in cts])


@app_views.route('/cities/<city_id>',
methods=['DELETE'],
strict_slashes=False)
def deleteCity(city_id=None):
'''deletes a city'''
if city_id is not None:
res = storage.get(City, city_id)
if res is not None:
storage.delete(res)
storage.save()
return make_response(jsonify({}), 200)
abort(404)


@app_views.route('/states/<state_id>/cities',
methods=['POST'],
strict_slashes=False)
def postCity(state_id=None):
'''posts a new city to a specific state'''
if state_id is None:
abort(404)
st = storage.get(State, state_id)
if st is None:
abort(404)

body = request.get_json()
if body is None:
abort(400, 'Not a JSON')
if 'name' not in body.keys():
abort(400, 'Missing name')
body['state_id'] = st.id
obj = City(**body)
obj.save()
return make_response(jsonify(obj.to_dict()), 201)


@app_views.route('/cities/<city_id>', methods=['PUT'], strict_slashes=False)
def updateCity(city_id=None):
'''updates a city'''
if city_id is None:
abort(404)
obj = storage.get(City, city_id)
if obj is None:
abort(404)

body = request.get_json()
if body is None:
abort(400, 'Not a JSON')
for key in body.keys():
if key not in ['id', 'created_at', 'updated_at', 'state_id']:
setattr(obj, key, body[key])
obj.save()
return make_response(jsonify(obj.to_dict()), 200)
33 changes: 33 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/python3
'''index module'''

from api.v1.views import app_views
from flask import jsonify
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


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


@app_views.route('/status')
def status():
'''returns json status OK'''
return jsonify({"status": "OK"})


@app_views.route('/stats')
def stats():
'''retrieves the number of each objects by type'''
result = {}
for cls in classes:
counter = storage.count(classes[cls])
result[cls] = counter
return jsonify(result)
Loading