fix: Simplify query that validates new username already exists

Instead of fetching all users and checking if the new username is present
in the list, we can directly query the database for the username.
This commit is contained in:
Michael Manganiello
2024-10-14 01:08:33 -03:00
parent 2a24f5f128
commit eba2971ffb
3 changed files with 22 additions and 3 deletions

View File

@@ -12,11 +12,13 @@ class DBUsersHandler(DBBaseHandler):
return session.merge(user)
@begin_session
def get_user_by_username(self, username: str, session: Session = None):
def get_user_by_username(
self, username: str, session: Session = None
) -> User | None:
return session.scalar(select(User).filter_by(username=username).limit(1))
@begin_session
def get_user(self, id: int, session: Session = None) -> User:
def get_user(self, id: int, session: Session = None) -> User | None:
return session.get(User, id)
@begin_session