Refactor with registry.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
from typing import Optional
|
||||
from .data import Koan, KoanData
|
||||
|
||||
|
||||
class KoanRegistry:
|
||||
def __init__(self, data: KoanData):
|
||||
self.data = data
|
||||
|
||||
async def init(self):
|
||||
await self.data.init()
|
||||
|
||||
async def get_community_koans(self, communityid: int) -> list[Koan]:
|
||||
return await Koan.fetch_where(communityid=communityid)
|
||||
|
||||
async def get_koan(self, koanid: int) -> Optional[Koan]:
|
||||
return await Koan.fetch(koanid)
|
||||
|
||||
async def get_koan_named(self, communityid: int, name: str) -> Optional[Koan]:
|
||||
name = name.lower()
|
||||
koans = await Koan.fetch_where(communityid=communityid, name=name)
|
||||
if koans:
|
||||
return koans[0]
|
||||
else:
|
||||
return None
|
||||
|
||||
async def create_koan(self, communityid: int, name: str, message: str) -> Koan:
|
||||
name = name.lower()
|
||||
await Koan.table.delete_where(communityid=communityid, name=name)
|
||||
koan = await Koan.create(
|
||||
communityid=communityid,
|
||||
name=name,
|
||||
message=message
|
||||
)
|
||||
return koan
|
||||
|
||||
Reference in New Issue
Block a user