mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 23:55:46 +02:00
Command to add memes
This commit is contained in:
parent
86dd6cb27b
commit
dbb570420b
9 changed files with 188 additions and 2 deletions
35
database/crud/memes.py
Normal file
35
database/crud/memes.py
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.schemas.relational import MemeTemplate
|
||||
|
||||
__all__ = ["add_meme", "get_all_memes"]
|
||||
|
||||
|
||||
async def add_meme(session: AsyncSession, name: str, template_id: int, field_count: int) -> Optional[MemeTemplate]:
|
||||
"""Add a new meme into the database"""
|
||||
try:
|
||||
meme = MemeTemplate(name=name, template_id=template_id, field_count=field_count)
|
||||
session.add(meme)
|
||||
await session.commit()
|
||||
return meme
|
||||
except IntegrityError:
|
||||
return None
|
||||
|
||||
|
||||
async def get_meme_by_name(session: AsyncSession, query: str) -> Optional[MemeTemplate]:
|
||||
"""Try to find a meme by its name
|
||||
|
||||
Returns the first match found by PSQL
|
||||
"""
|
||||
statement = select(MemeTemplate).where(MemeTemplate.name.ilike(f"%{query.lower()}%"))
|
||||
return (await session.execute(statement)).scalar()
|
||||
|
||||
|
||||
async def get_all_memes(session: AsyncSession) -> list[MemeTemplate]:
|
||||
"""Get a list of all memes"""
|
||||
statement = select(MemeTemplate)
|
||||
return (await session.execute(statement)).scalars().all()
|
||||
Loading…
Add table
Add a link
Reference in a new issue