mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 15:48:29 +02:00
Add birthday commands
This commit is contained in:
parent
016d87bcea
commit
f49f32d2e9
7 changed files with 141 additions and 1 deletions
22
database/crud/birthdays.py
Normal file
22
database/crud/birthdays.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
from datetime import date
|
||||
from typing import Optional
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.models import Birthday
|
||||
|
||||
__all__ = ["add_birthday", "get_birthday_for_user"]
|
||||
|
||||
|
||||
async def add_birthday(session: AsyncSession, user_id: int, birthday: date):
|
||||
"""Add a user's birthday into the database"""
|
||||
bd = Birthday(user_id=user_id, birthday=birthday)
|
||||
session.add(bd)
|
||||
await session.commit()
|
||||
|
||||
|
||||
async def get_birthday_for_user(session: AsyncSession, user_id: int) -> Optional[Birthday]:
|
||||
"""Find a user's birthday"""
|
||||
statement = select(Birthday).where(Birthday.user_id == user_id)
|
||||
return (await session.execute(statement)).scalar_one_or_none()
|
||||
|
|
@ -12,6 +12,7 @@ Base = declarative_base()
|
|||
__all__ = [
|
||||
"Base",
|
||||
"Bank",
|
||||
"Birthday",
|
||||
"CustomCommand",
|
||||
"CustomCommandAlias",
|
||||
"DadJoke",
|
||||
|
|
@ -46,6 +47,18 @@ class Bank(Base):
|
|||
user: User = relationship("User", uselist=False, back_populates="bank", lazy="selectin")
|
||||
|
||||
|
||||
class Birthday(Base):
|
||||
"""A user's birthday"""
|
||||
|
||||
__tablename__ = "birthdays"
|
||||
|
||||
birthday_id: int = Column(Integer, primary_key=True)
|
||||
user_id: int = Column(BigInteger, ForeignKey("users.user_id"))
|
||||
birthday: datetime = Column(DateTime, nullable=False)
|
||||
|
||||
user: User = relationship("User", uselist=False, back_populates="birthday", lazy="selectin")
|
||||
|
||||
|
||||
class CustomCommand(Base):
|
||||
"""Custom commands to fill the hole Dyno couldn't"""
|
||||
|
||||
|
|
@ -149,6 +162,9 @@ class User(Base):
|
|||
bank: Bank = relationship(
|
||||
"Bank", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
birthday: Birthday = relationship(
|
||||
"Birthday", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
nightly_data: NightlyData = relationship(
|
||||
"NightlyData", back_populates="user", uselist=False, lazy="selectin", cascade="all, delete-orphan"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue