2022-07-14 22:03:30 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
from database.models import UforaCourse, UforaCourseAlias
|
|
|
|
|
2022-07-14 22:44:22 +02:00
|
|
|
__all__ = ["get_all_courses", "get_course_by_name"]
|
|
|
|
|
2022-07-14 22:03:30 +02:00
|
|
|
|
|
|
|
async def get_all_courses(session: AsyncSession) -> list[UforaCourse]:
|
|
|
|
"""Get a list of all courses in the database"""
|
|
|
|
statement = select(UforaCourse)
|
2022-07-14 22:03:56 +02:00
|
|
|
return list((await session.execute(statement)).scalars().all())
|
2022-07-14 22:03:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def get_course_by_name(session: AsyncSession, query: str) -> Optional[UforaCourse]:
|
|
|
|
"""Try to find a course by its name
|
|
|
|
|
|
|
|
This checks for regular name first, and then aliases
|
|
|
|
"""
|
|
|
|
# Search case-insensitively
|
|
|
|
query = query.lower()
|
|
|
|
|
|
|
|
statement = select(UforaCourse).where(UforaCourse.name.ilike(f"%{query}%"))
|
|
|
|
result = (await session.execute(statement)).scalars().first()
|
|
|
|
if result:
|
|
|
|
return result
|
|
|
|
|
|
|
|
statement = select(UforaCourseAlias).where(UforaCourseAlias.alias.ilike(f"%{query}%"))
|
|
|
|
result = (await session.execute(statement)).scalars().first()
|
|
|
|
return result.course if result else None
|