Quotes data and cog skeleton.
This commit is contained in:
41
quotes/quotes.py
Normal file
41
quotes/quotes.py
Normal file
@@ -0,0 +1,41 @@
|
||||
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
|
||||
Reference in New Issue
Block a user