42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from typing import Optional
|
|
from .data import Quote, QuoteInfo, QuotesData
|
|
|
|
|
|
class QuoteRegistry:
|
|
def __init__(self, data: QuotesData):
|
|
self.data = data
|
|
# TODO: For efficiency we could have a cache here
|
|
# Caching quotesinfo by community.
|
|
# Particularly efficient for autocomplete.
|
|
|
|
async def init(self):
|
|
await self.data.init()
|
|
|
|
async def get_community_quotes(self, communityid: int) -> list[QuoteInfo]:
|
|
return await QuoteInfo.fetch_where(communityid=communityid)
|
|
|
|
async def get_quoteinfo(self, quoteid: int) -> Optional[QuoteInfo]:
|
|
return await QuoteInfo.fetch(quoteid)
|
|
|
|
async def get_quote(self, quoteid: int) -> Optional[Quote]:
|
|
return await Quote.fetch(quoteid)
|
|
|
|
async def get_quote_label(self, communityid: int, label: int) -> Optional[QuoteInfo]:
|
|
results = await QuoteInfo.fetch_where(communityid=communityid, quotelabel=label)
|
|
return results[0] if results else None
|
|
|
|
async def create_quote(
|
|
self,
|
|
communityid: int,
|
|
content: str,
|
|
created_by: Optional[int] = None
|
|
) -> QuoteInfo:
|
|
quote = await Quote.create(
|
|
content=content,
|
|
communityid=communityid,
|
|
created_by=created_by,
|
|
)
|
|
info = await QuoteInfo.fetch(quote.quoteid)
|
|
assert info is not None
|
|
return info
|