mirror of
https://github.com/stijndcl/didier.git
synced 2026-04-07 23:55:46 +02:00
Rework links
This commit is contained in:
parent
94de47082b
commit
a614e9a9f1
12 changed files with 246 additions and 30 deletions
45
database/crud/links.py
Normal file
45
database/crud/links.py
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
from typing import Optional
|
||||
|
||||
from sqlalchemy import func, select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.exceptions import NoResultFoundException
|
||||
from database.schemas.relational import Link
|
||||
|
||||
__all__ = ["add_link", "edit_link", "get_all_links", "get_link_by_name"]
|
||||
|
||||
|
||||
async def get_all_links(session: AsyncSession) -> list[Link]:
|
||||
"""Get a list of all links"""
|
||||
statement = select(Link)
|
||||
return (await session.execute(statement)).scalars().all()
|
||||
|
||||
|
||||
async def add_link(session: AsyncSession, name: str, url: str) -> Link:
|
||||
"""Add a new link into the database"""
|
||||
if name.islower():
|
||||
name = name.capitalize()
|
||||
|
||||
instance = Link(name=name, url=url)
|
||||
session.add(instance)
|
||||
await session.commit()
|
||||
|
||||
return instance
|
||||
|
||||
|
||||
async def get_link_by_name(session: AsyncSession, name: str) -> Optional[Link]:
|
||||
"""Get a link by its name"""
|
||||
statement = select(Link).where(func.lower(Link.name) == name.lower())
|
||||
return (await session.execute(statement)).scalar_one_or_none()
|
||||
|
||||
|
||||
async def edit_link(session: AsyncSession, name: str, new_url: str):
|
||||
"""Edit an existing link"""
|
||||
link: Optional[Link] = await get_link_by_name(session, name)
|
||||
|
||||
if link is None:
|
||||
raise NoResultFoundException
|
||||
|
||||
link.url = new_url
|
||||
session.add(link)
|
||||
await session.commit()
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
from .constraints import DuplicateInsertException
|
||||
from .currency import DoubleNightly, NotEnoughDinks
|
||||
from .not_found import NoResultFoundException
|
||||
|
||||
__all__ = ["DuplicateInsertException", "DoubleNightly", "NotEnoughDinks", "NoResultFoundException"]
|
||||
|
|
@ -28,6 +28,7 @@ __all__ = [
|
|||
"CustomCommand",
|
||||
"CustomCommandAlias",
|
||||
"DadJoke",
|
||||
"Link",
|
||||
"NightlyData",
|
||||
"Task",
|
||||
"UforaAnnouncement",
|
||||
|
|
@ -109,6 +110,16 @@ class DadJoke(Base):
|
|||
joke: str = Column(Text, nullable=False)
|
||||
|
||||
|
||||
class Link(Base):
|
||||
"""Useful links that go useful places"""
|
||||
|
||||
__tablename__ = "links"
|
||||
|
||||
link_id: int = Column(Integer, primary_key=True)
|
||||
name: str = Column(Text, nullable=False, unique=True)
|
||||
url: str = Column(Text, nullable=False)
|
||||
|
||||
|
||||
class NightlyData(Base):
|
||||
"""Data for a user's Nightly stats"""
|
||||
|
||||
|
|
|
|||
|
|
@ -4,10 +4,10 @@ from typing import Generic, TypeVar
|
|||
from overrides import overrides
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from database.crud import ufora_courses, wordle
|
||||
from database.crud import links, ufora_courses, wordle
|
||||
from database.mongo_types import MongoDatabase
|
||||
|
||||
__all__ = ["CacheManager", "UforaCourseCache"]
|
||||
__all__ = ["CacheManager", "LinkCache", "UforaCourseCache"]
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
|
@ -35,12 +35,8 @@ class DatabaseCache(ABC, Generic[T]):
|
|||
self.data.clear()
|
||||
|
||||
@abstractmethod
|
||||
async def refresh(self, database_session: T):
|
||||
"""Refresh the data stored in this cache"""
|
||||
|
||||
async def invalidate(self, database_session: T):
|
||||
"""Invalidate the data stored in this cache"""
|
||||
await self.refresh(database_session)
|
||||
|
||||
def get_autocomplete_suggestions(self, query: str):
|
||||
"""Filter the cache to find everything that matches the search query"""
|
||||
|
|
@ -49,6 +45,19 @@ class DatabaseCache(ABC, Generic[T]):
|
|||
return [self.data[index] for index, value in enumerate(self.data_transformed) if query in value]
|
||||
|
||||
|
||||
class LinkCache(DatabaseCache[AsyncSession]):
|
||||
"""Cache to store the names of links"""
|
||||
|
||||
@overrides
|
||||
async def invalidate(self, database_session: AsyncSession):
|
||||
self.clear()
|
||||
|
||||
all_links = await links.get_all_links(database_session)
|
||||
self.data = list(map(lambda l: l.name, all_links))
|
||||
self.data.sort()
|
||||
self.data_transformed = list(map(str.lower, self.data))
|
||||
|
||||
|
||||
class UforaCourseCache(DatabaseCache[AsyncSession]):
|
||||
"""Cache to store the names of Ufora courses"""
|
||||
|
||||
|
|
@ -61,11 +70,10 @@ class UforaCourseCache(DatabaseCache[AsyncSession]):
|
|||
super().clear()
|
||||
|
||||
@overrides
|
||||
async def refresh(self, database_session: AsyncSession):
|
||||
async def invalidate(self, database_session: AsyncSession):
|
||||
self.clear()
|
||||
|
||||
courses = await ufora_courses.get_all_courses(database_session)
|
||||
|
||||
self.data = list(map(lambda c: c.name, courses))
|
||||
|
||||
# Load the aliases
|
||||
|
|
@ -97,7 +105,7 @@ class UforaCourseCache(DatabaseCache[AsyncSession]):
|
|||
class WordleCache(DatabaseCache[MongoDatabase]):
|
||||
"""Cache to store the current daily Wordle word"""
|
||||
|
||||
async def refresh(self, database_session: MongoDatabase):
|
||||
async def invalidate(self, database_session: MongoDatabase):
|
||||
word = await wordle.get_daily_word(database_session)
|
||||
if word is not None:
|
||||
self.data = [word]
|
||||
|
|
@ -106,14 +114,17 @@ class WordleCache(DatabaseCache[MongoDatabase]):
|
|||
class CacheManager:
|
||||
"""Class that keeps track of all caches"""
|
||||
|
||||
links: LinkCache
|
||||
ufora_courses: UforaCourseCache
|
||||
wordle_word: WordleCache
|
||||
|
||||
def __init__(self):
|
||||
self.links = LinkCache()
|
||||
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.ufora_courses.refresh(postgres_session)
|
||||
await self.wordle_word.refresh(mongo_db)
|
||||
await self.links.invalidate(postgres_session)
|
||||
await self.ufora_courses.invalidate(postgres_session)
|
||||
await self.wordle_word.invalidate(mongo_db)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue