Add dad jokes

This commit is contained in:
stijndcl 2022-07-16 00:14:02 +02:00
parent 3d0f771f94
commit 3debd18d82
11 changed files with 165 additions and 4 deletions

View file

@ -33,6 +33,7 @@ async def create_command(session: AsyncSession, name: str, response: str) -> Cus
command = CustomCommand(name=name, indexed_name=clean_name(name), response=response)
session.add(command)
await session.commit()
return command

View file

@ -0,0 +1,38 @@
from typing import Optional
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from database.exceptions.not_found import NoResultFoundException
from database.models import DadJoke
__all__ = ["add_dad_joke", "edit_dad_joke", "get_random_dad_joke"]
async def add_dad_joke(session: AsyncSession, joke: str) -> DadJoke:
"""Add a new dad joke to the database"""
dad_joke = DadJoke(joke=joke)
session.add(dad_joke)
await session.commit()
return dad_joke
async def edit_dad_joke(session: AsyncSession, joke_id: int, new_joke: str) -> DadJoke:
"""Edit an existing dad joke"""
statement = select(DadJoke).where(DadJoke.dad_joke_id == joke_id)
dad_joke: Optional[DadJoke] = (await session.execute(statement)).scalar_one_or_none()
if dad_joke is None:
raise NoResultFoundException
dad_joke.joke = new_joke
session.add(dad_joke)
await session.commit()
return dad_joke
async def get_random_dad_joke(session: AsyncSession) -> DadJoke:
"""Return a random database entry"""
statement = select(DadJoke).order_by(func.random())
return (await session.execute(statement)).first()

View file

@ -14,6 +14,7 @@ __all__ = [
"Bank",
"CustomCommand",
"CustomCommandAlias",
"DadJoke",
"NightlyData",
"UforaAnnouncement",
"UforaCourse",
@ -73,6 +74,15 @@ class CustomCommandAlias(Base):
command: CustomCommand = relationship("CustomCommand", back_populates="aliases", uselist=False, lazy="selectin")
class DadJoke(Base):
"""When I finally understood asymptotic notation, it was a big "oh" moment"""
__tablename__ = "dad_jokes"
dad_joke_id: int = Column(Integer, primary_key=True)
joke: str = Column(Text, nullable=False)
class NightlyData(Base):
"""Data for a user's Nightly stats"""