Skip to content

Commit

Permalink
Add fetch_by_name_extended query
Browse files Browse the repository at this point in the history
  • Loading branch information
Lekuruu committed Oct 9, 2023
1 parent d9ade57 commit 3fe0e7d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion database/repositories/users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from app.common.database.objects import DBUser, DBStats
from sqlalchemy import func, or_, and_
from typing import Optional, List
from sqlalchemy import func

import app

Expand Down Expand Up @@ -47,6 +47,16 @@ def fetch_by_name(username: str) -> Optional[DBUser]:
.filter(DBUser.name == username) \
.first()

def fetch_by_name_extended(query: str) -> Optional[DBUser]:
"""Used for searching users"""
return app.session.database.session.query(DBUser) \
.filter(or_(
DBUser.name.ilike(query),
DBUser.name.ilike(f'%{query}%')
)) \
.order_by(func.length(DBUser.name).asc()) \
.first()

def fetch_by_safe_name(username: str) -> Optional[DBUser]:
return app.session.database.session.query(DBUser) \
.filter(DBUser.safe_name == username) \
Expand Down

0 comments on commit 3fe0e7d

Please sign in to comment.