mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Add crud functions to get ufora courses
This commit is contained in:
parent
c8392342a6
commit
84bf1d7a26
4 changed files with 86 additions and 18 deletions
30
database/crud/ufora_courses.py
Normal file
30
database/crud/ufora_courses.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.models import UforaCourse, UforaCourseAlias
|
||||
|
||||
|
||||
async def get_all_courses(session: AsyncSession) -> list[UforaCourse]:
|
||||
"""Get a list of all courses in the database"""
|
||||
statement = select(UforaCourse)
|
||||
return (await session.execute(statement)).scalars().all()
|
||||
|
||||
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue