Command to add memes

This commit is contained in:
stijndcl 2022-08-25 02:07:02 +02:00
parent 86dd6cb27b
commit dbb570420b
9 changed files with 188 additions and 2 deletions

35
database/crud/memes.py Normal file
View 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()

View file

@ -30,6 +30,7 @@ __all__ = [
"DadJoke",
"Deadline",
"Link",
"MemeTemplate",
"NightlyData",
"Task",
"UforaAnnouncement",
@ -134,6 +135,17 @@ class Link(Base):
url: str = Column(Text, nullable=False)
class MemeTemplate(Base):
"""A meme template for the Imgflip API"""
__tablename__ = "meme"
meme_id: int = Column(Integer, primary_key=True)
name: str = Column(Text, nullable=False, unique=True)
template_id: int = Column(Integer, nullable=False, unique=True)
field_count: int = Column(Integer, nullable=False)
class NightlyData(Base):
"""Data for a user's Nightly stats"""

View file

@ -5,7 +5,7 @@ from discord import app_commands
from overrides import overrides
from sqlalchemy.ext.asyncio import AsyncSession
from database.crud import links, ufora_courses, wordle
from database.crud import links, memes, ufora_courses, wordle
from database.mongo_types import MongoDatabase
__all__ = ["CacheManager", "LinkCache", "UforaCourseCache"]
@ -61,6 +61,19 @@ class LinkCache(DatabaseCache[AsyncSession]):
self.data_transformed = list(map(str.lower, self.data))
class MemeCache(DatabaseCache[AsyncSession]):
"""Cache to store the names of meme templates"""
@overrides
async def invalidate(self, database_session: AsyncSession):
self.clear()
all_memes = await memes.get_all_memes(database_session)
self.data = list(map(lambda m: m.name, all_memes))
self.data.sort()
self.data_transformed = list(map(str.lower, self.data))
class UforaCourseCache(DatabaseCache[AsyncSession]):
"""Cache to store the names of Ufora courses"""
@ -119,16 +132,19 @@ class CacheManager:
"""Class that keeps track of all caches"""
links: LinkCache
memes: MemeCache
ufora_courses: UforaCourseCache
wordle_word: WordleCache
def __init__(self):
self.links = LinkCache()
self.memes = MemeCache()
self.ufora_courses = UforaCourseCache()
self.wordle_word = WordleCache()
async def initialize_caches(self, postgres_session: AsyncSession, mongo_db: MongoDatabase):
"""Initialize the contents of all caches"""
await self.links.invalidate(postgres_session)
await self.memes.invalidate(postgres_session)
await self.ufora_courses.invalidate(postgres_session)
await self.wordle_word.invalidate(mongo_db)