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

Storage get count #3686

Open
wants to merge 93 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
93 commits
Select commit Hold shift + click to select a range
37d7330
new authors updated
deantosh Aug 1, 2024
d8bbc0b
error fixed
deantosh Aug 1, 2024
d837a81
get() and count() methods added
deantosh Aug 1, 2024
c82e21d
get() and count() methods added
deantosh Aug 1, 2024
2d9d272
Merge pull request #1 from deantosh/storage_get_count
deantosh Aug 1, 2024
ca4f504
get() method - gets a specified object
deantosh Aug 1, 2024
8d29c6e
Merge pull request #2 from deantosh/storage_get_count
deantosh Aug 1, 2024
3264dec
count() method - get the number of storage objects
deantosh Aug 1, 2024
ff3ad2c
Merge pull request #3 from deantosh/storage_get_count
deantosh Aug 1, 2024
d5d4fd8
test case: get() and count()
deantosh Aug 1, 2024
3839bf2
various files added
luckys-lnz Aug 1, 2024
550b988
Merge pull request #4 from deantosh/storage_get_count
deantosh Aug 1, 2024
e80548e
test case: get() and count()
deantosh Aug 1, 2024
ff1d63a
deleted __pycache__
luckys-lnz Aug 1, 2024
fbeffbc
Merge pull request #5 from deantosh/storage_get_count
deantosh Aug 1, 2024
0f3297a
Merge pull request #6 from deantosh/status_of_api
deantosh Aug 1, 2024
206ce0d
custom 404 error handler
deantosh Aug 1, 2024
c206be9
get_status() -- documentation
deantosh Aug 1, 2024
bce1b67
Data not populated from DB
luckys-lnz Aug 1, 2024
5364020
import State objects view
deantosh Aug 2, 2024
8df0290
executable
deantosh Aug 2, 2024
3e5682d
view: State objects
deantosh Aug 2, 2024
55ad36d
added keys
luckys-lnz Aug 2, 2024
389ba9a
modified file storage
luckys-lnz Aug 2, 2024
f54d1a8
view: City objects
deantosh Aug 2, 2024
f54b35c
import City view
deantosh Aug 2, 2024
4f86e75
Merge pull request #7 from deantosh/status_of_api
deantosh Aug 2, 2024
9ffb32d
view: Amenity objects
deantosh Aug 2, 2024
6cbc270
import: Amenity view
deantosh Aug 2, 2024
5c6d98e
file upddated
deantosh Aug 2, 2024
633bfc3
file updated
deantosh Aug 2, 2024
7189e7f
count() method - error fixed
deantosh Aug 2, 2024
93c22a5
view: User objects
deantosh Aug 3, 2024
96c4c51
import User view
deantosh Aug 3, 2024
99a43a7
view: Place objects
deantosh Aug 3, 2024
95a7116
import Place view
deantosh Aug 3, 2024
6304589
places_reviews.py
luckys-lnz Aug 3, 2024
bdf582e
Merge branch 'master' of github.com:deantosh/AirBnB_clone_v3 into sta…
luckys-lnz Aug 3, 2024
c405cce
Merge pull request #8 from deantosh/status_of_api
deantosh Aug 3, 2024
5c3183c
get() method test: added
deantosh Aug 3, 2024
c7e3527
get() method test: added
deantosh Aug 3, 2024
e7573e2
to be deleted -- testing checker
deantosh Aug 3, 2024
68af43f
to be deleted -- testing checker
deantosh Aug 3, 2024
34fdf9d
tests: DStorage class
deantosh Aug 3, 2024
fc46f8e
tests: DStorage class
deantosh Aug 3, 2024
04e91c9
test: storage get()
deantosh Aug 3, 2024
c17307d
test: storage count()
deantosh Aug 3, 2024
0cc7d41
test: storage count() updated
deantosh Aug 3, 2024
ac61035
get() and count() method added
deantosh Aug 3, 2024
27185e7
route: /stats added
deantosh Aug 3, 2024
1a38097
get() and count() method added
deantosh Aug 3, 2024
b13ef56
route: /stats -- fix strict slashes url error
deantosh Aug 4, 2024
f6ec490
places reviews
luckys-lnz Aug 4, 2024
006ad67
updated package
luckys-lnz Aug 4, 2024
8e39c70
view: State objects
deantosh Aug 4, 2024
c90f183
Merge pull request #9 from deantosh/status_of_api
deantosh Aug 4, 2024
b980592
tests: get() and count()
deantosh Aug 4, 2024
861556f
tests: get() and count()
deantosh Aug 4, 2024
164db38
method: get(0 and count() added
deantosh Aug 4, 2024
7120d74
method: get() and count() added
deantosh Aug 4, 2024
d2d07f9
Merge branch 'master' into storage_get_count
luckys-lnz Aug 4, 2024
7791867
test: get() and count()
deantosh Aug 4, 2024
493ec90
Merge branch 'storage_get_count' of https://github.com/deantosh/AirBn…
deantosh Aug 4, 2024
bc95edc
file updated
deantosh Aug 4, 2024
599670d
file updated
deantosh Aug 4, 2024
53c2551
files deleted
deantosh Aug 4, 2024
faa7249
tests: FileStorage class
deantosh Aug 4, 2024
9bef908
tests: DBStorage class
deantosh Aug 4, 2024
0a3428d
tests: DBStorage class
deantosh Aug 4, 2024
1dcd48e
tests: DBStorage class
deantosh Aug 4, 2024
519141a
tests
deantosh Aug 5, 2024
add9c20
models
deantosh Aug 5, 2024
6405f4f
Advanced tasks
deantosh Aug 5, 2024
8ef8136
command line interpreter -- interact with models created
deantosh Aug 5, 2024
b5a9e64
web_static
deantosh Aug 5, 2024
3db35ba
test: FileStorage class
deantosh Aug 5, 2024
b09eef2
test: DBStorage class
deantosh Aug 5, 2024
d634e30
complaint to python3.4
deantosh Aug 5, 2024
9f91d83
test for get() and count() added
deantosh Aug 5, 2024
ce830da
get() and count() added
deantosh Aug 5, 2024
61ece77
get(0 and count() methods added
deantosh Aug 5, 2024
37ea222
get() and count() methods added
deantosh Aug 5, 2024
8070caa
get() and count() added
deantosh Aug 5, 2024
612550d
models
deantosh Aug 5, 2024
d774bec
tests
deantosh Aug 5, 2024
c04500a
new methods: get() and count()
deantosh Aug 5, 2024
5ccdeca
new methods: get() and count()
deantosh Aug 5, 2024
a5272cb
test: Amenity class
deantosh Aug 5, 2024
7f35a1c
test: get() and count()
deantosh Aug 5, 2024
52dfa9e
test: get() and count()
deantosh Aug 5, 2024
81b7015
get() method updated
deantosh Aug 5, 2024
795df80
view: State objects -- routes updated
deantosh Aug 6, 2024
3aa8459
view: Place objects
deantosh Aug 8, 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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,5 +158,10 @@ Alexa Orrico - [Github](https://github.com/alexaorrico) / [Twitter](https://twit
Jennifer Huang - [Github](https://github.com/jhuang10123) / [Twitter](https://twitter.com/earthtojhuang)

Second part of Airbnb: Joann Vuong

AirBnB -- RESTFUL API part
Deantosh Daiddoh - [Github] (https://github.com/deantosh) / [X] (https://x.com/daiddoh)
Lucky Archibong - [Github] (https://github.com/luckys-lnz) / [X] (https://x.com/)

## License
Public Domain. No copy write protection.
Binary file added __pycache__/console.cpython-312.pyc
Binary file not shown.
Empty file added api/__init__.py
Empty file.
Empty file added api/v1/__init__.py
Empty file.
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
"""
Module defines a Flask application
"""

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


app = Flask(__name__)
app.register_blueprint(app_views)


@app.errorhandler(404)
def not_found_error(error):
""" Handles error 404 """
return jsonify({"error": "Not found"}), 404


@app.teardown_appcontext
def teardown(exception=None):
""" Closes the storage """
storage.close()


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)
14 changes: 14 additions & 0 deletions api/v1/views/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/python3
"""python package"""

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 *
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
"""
Module defines view for Amenity objects that handles all default
RESTFUL API actions.
"""
from models import storage
from models.amenity import Amenity
from flask import jsonify, abort, request
from werkzeug.exceptions import BadRequest
from api.v1.views import app_views


@app_views.route('/amenities', methods=['GET'], strict_slashes=False)
def get_all_amenities():
""" Retrieves all Amenity objects """
amenity_objs = storage.all(Amenity).values()
amenity_list = [amenity.to_dict() for amenity in amenity_objs]

return jsonify(amenity_list)


@app_views.route('/amenities/<amenity_id>', methods=['GET'])
def get_amenity(amenity_id):
""" Retrieves Amenity object by amenity_id """
amenity_obj = storage.get(Amenity, amenity_id)
if amenity_obj is None:
abort(404)

return jsonify(amenity_obj.to_dict())


@app_views.route('/amenities/<amenity_id>', methods=['DELETE'])
def delete_amenity(amenity_id):
""" Deletes Amenity object by amenity_id """
amenity_obj = storage.get(Amenity, amenity_id)
if amenity_obj is None:
abort(404)

storage.delete(amenity_obj)
storage.save()

return jsonify({}), 200


@app_views.route('/amenities', methods=['POST'], strict_slashes=False)
def create_amenity():
""" Creates Amenity object """
try:
data = request.get_json()
if 'name' not in data:
abort(400, description='Missing name')
except BadRequest:
abort(400, description='Not a JSON')

amenity_obj = Amenity(**data)
storage.new(amenity_obj)
storage.save()

return jsonify(amenity_obj.to_dict()), 201


@app_views.route('/amenities/<amenity_id>', methods=['PUT'])
def update_amenity(amenity_id):
""" Updates Amenity object by amenity_id """
amenity_obj = storage.get(Amenity, amenity_id)
if amenity_obj is None:
abort(404)

try:
data = request.get_json()
except BadRequest:
abort(400, description='Not a JSON')

ignore_keys = ['id', 'state_id', 'created_at', 'updated_at']
for key, value in data.items():
if key not in ignore_keys:
setattr(amenity_obj, key, value)

storage.save()

return jsonify(amenity_obj.to_dict()), 200
90 changes: 90 additions & 0 deletions api/v1/views/cities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/python3
"""
Module defines 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 flask import jsonify, abort, request
from werkzeug.exceptions import BadRequest
from api.v1.views import app_views


@app_views.route('/states/<state_id>/cities',
methods=['GET'], strict_slashes=False)
def get_all_cities(state_id):
""" Retrieves all the cities of a state """
state_obj = storage.get(State, state_id)
if state_obj is None:
abort(404)
city_list = [city.to_dict() for city in state_obj.cities]
return jsonify(city_list)


@app_views.route('/cities/<city_id>', methods=['GET'])
def get_city(city_id):
""" Retrieves city object of a specified city_id """
city_obj = storage.get(City, city_id)
if city_obj is None:
abort(404)

return jsonify(city_obj.to_dict())


@app_views.route('/cities/<city_id>', methods=['DELETE'])
def delete_city(city_id):
""" Deletes city object """
city_obj = storage.get(City, city_id)
if city_obj is None:
abort(404)

storage.delete(city_obj)
storage.save()

return jsonify({}), 200


@app_views.route('/states/<state_id>/cities',
methods=['POST'], strict_slashes=False)
def create_city(state_id):
""" Creates city object"""
state_obj = storage.get(State, state_id)
if state_obj is None:
abort(404)

try:
data = request.get_json()
if 'name' not in data:
abort(400, description='Missing name')
except BadRequest:
abort(400, description='Not a JSON')

data['state_id'] = state_id
city_obj = City(**data)
storage.new(city_obj)
storage.save()

return jsonify(city_obj.to_dict()), 201


@app_views.route('/cities/<city_id>', methods=['PUT'])
def update_city(city_id):
""" Updates city object """
city_obj = storage.get(City, city_id)
if city_obj is None:
abort(404)

try:
data = request.get_json()
except BadRequest:
abort(400, description='Not a JSON')

ignore_keys = ['id', 'state_id', 'created_at', 'updated_at']
for key, value in data.items():
if key not in ignore_keys:
setattr(city_obj, key, value)

storage.save()

return jsonify(city_obj.to_dict()), 200
35 changes: 35 additions & 0 deletions api/v1/views/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/python3
"""
Create an endpoint that retrieves the number
of each objects by type:
"""

from flask import jsonify
from api.v1.views import app_views
from models import storage
from models.amenity import Amenity
from models.city import City
from models.state import State
from models.place import Place
from models.user import User
from models.review import Review


@app_views.route('/status', methods=['GET'])
def get_status():
"""Check status of file"""
return jsonify({"status": "OK"})


@app_views.route('/stats', methods=['GET'], strict_slashes=False)
def get_stats():
"""Create an endpoint"""
statistics = {
"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(statistics)
91 changes: 91 additions & 0 deletions api/v1/views/places.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/python3
"""
Module defines view for Place objects that handles all default
RESTFUL API actions.
"""
from models import storage
from models.city import City
from models.place import Place
from flask import jsonify, abort, request
from werkzeug.exceptions import BadRequest
from api.v1.views import app_views


@app_views.route('/cities/<city_id>/places',
methods=['GET'], strict_slashes=False)
def get_all_places(city_id):
""" Retrieves the list of all Place objects of a city by city_id"""
city = storage.get(City, city_id)
if city is None:
abort(404)
place_list = [place.to_dict() for place in city.places]
return jsonify(place_list)


@app_views.route('/places/<place_id>', methods=['GET'], strict_slashes=False)
def get_place(place_id):
""" Retrieves Place object by place_id """
place_obj = storage.get(Place, place_id)
if place_obj is None:
abort(404)

return jsonify(place_obj.to_dict())


@app_views.route('/places/<place_id>', methods=['DELETE'],
strict_slashes=False)
def delete_place(place_id):
""" Deletes Place object by place_id """
place_obj = storage.get(Place, place_id)
if place_obj is None:
abort(404)

storage.delete(place_obj)
storage.save()

return jsonify({}), 200


@app_views.route('/cities/<city_id>/places',
methods=['POST'], strict_slashes=False)
def create_place(city_id):
""" Creates Place object in a city by city_id"""
city_obj = storage.get(City, city_id)
if city_obj is None:
abort(404)

try:
data = request.get_json()
if 'name' not in data:
abort(400, description='Missing name')
except BadRequest:
abort(400, description='Not a JSON')

data['city_id'] = city_id
place_obj = Place(**data)
storage.new(place_obj)
storage.save()

return jsonify(place_obj.to_dict()), 201


@app_views.route('/places/<place_id>', methods=['PUT'], strict_slashes=False)
def update_place(place_id):
""" Updates Place object by place_id """
place_obj = storage.get(Place, place_id)
if place_obj is None:
abort(404)

try:
data = request.get_json()
except BadRequest:
abort(400, description='Not a JSON')

ignore_keys = ['id', 'state_id', 'created_at', 'updated_at']
for key, value in data.items():
if key not in ignore_keys:
setattr(place_obj, key, value)

storage.save()

return jsonify(place_obj.to_dict()), 200
Loading