Skip to content

Commit a9eca16

Browse files
authored
Merge pull request #81 from igorbenav/crud_base_count_fix
CRUDBase count method bug fix
2 parents d4e8775 + 9845178 commit a9eca16

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

src/app/core/security.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@
1919
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/login")
2020

2121
async def verify_password(plain_password: str, hashed_password: str) -> bool:
22-
return bcrypt.checkpw(plain_password.encode(), hashed_password.encode())
22+
correct_password: bool = bcrypt.checkpw(plain_password.encode(), hashed_password.encode())
23+
return correct_password
2324

2425
def get_password_hash(password: str) -> str:
25-
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
26+
hashed_password: str = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
27+
return hashed_password
2628

2729
async def authenticate_user(username_or_email: str, password: str, db: AsyncSession) -> Union[Dict[str, Any], Literal[False]]:
2830
if "@" in username_or_email:

src/app/crud/crud_base.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,13 @@ async def count(
146146
----
147147
This method provides a quick way to get the count of records without retrieving the actual data.
148148
"""
149-
conditions = [getattr(self._model, key) == value for key, value in kwargs.items()]
150-
combined_conditions = and_(*conditions)
149+
if kwargs:
150+
conditions = [getattr(self._model, key) == value for key, value in kwargs.items()]
151+
combined_conditions = and_(*conditions)
152+
count_query = select(func.count()).select_from(self._model).filter(combined_conditions)
153+
else:
154+
count_query = select(func.count()).select_from(self._model)
151155

152-
count_query = select(func.count()).filter(combined_conditions)
153156
total_count: int = await db.scalar(count_query)
154157

155158
return total_count

0 commit comments

Comments
 (0)