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 #3694

Open
wants to merge 4 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
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 <133@holbertonschool.com>
Alexa Orrico <210@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Joann Vuong <130@holbertonschool.com>
Mark Manani <mikonani413@gmail.com>
Binary file added __pycache__/console.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/amenity.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/base_model.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/city.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/place.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/review.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/state.cpython-310.pyc
Binary file not shown.
Binary file added models/__pycache__/user.cpython-310.pyc
Binary file not shown.
Binary file added models/engine/__pycache__/__init__.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
18 changes: 18 additions & 0 deletions models/engine/db_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.exc import SQLAlchemyError

classes = {"Amenity": Amenity, "City": City,
"Place": Place, "Review": Review, "State": State, "User": User}
Expand Down Expand Up @@ -74,3 +75,20 @@ def reload(self):
def close(self):
"""call remove() method on the private session attribute"""
self.__session.remove()

def get(self, cls, id):
"""retrieves one object"""
try:
return self.__session.query(cls).get(id)
except SQLAlchemyError:
return None

def count(self, cls=None):
"""Count the number of objects in storage for the given class."""
try:
if cls:
return self.__session.query(cls).count()
else:
return sum([self.__session.query(c).count() for c in self.__classes.values()])
except SQLAlchemyError:
return 0
11 changes: 11 additions & 0 deletions models/engine/file_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,14 @@ def delete(self, obj=None):
def close(self):
"""call reload() method for deserializing the JSON file to objects"""
self.reload()

def get(self, cls, id):
"""Retrieve one object based on the class and its ID."""
objects = self.all(cls)
return objects.get(id, None)

def count(self, cls=None):
"""Count the number of objects in storage for the given class."""
if cls:
return len([obj for obj in self.__objects.values() if isinstance(obj, cls)])
return len(self.__objects)
Binary file added tests/__pycache__/test_console.cpython-310.pyc
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/test_models/test_amenity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import models
from models import amenity
from models.base_model import BaseModel
import pep8
import pep8 as pycodestyle
import unittest
Amenity = amenity.Amenity

Expand Down
2 changes: 1 addition & 1 deletion tests/test_models/test_city.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import models
from models import city
from models.base_model import BaseModel
import pep8
import pep8 as pycodestyle
import unittest
City = city.City

Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.