v2 refactor to label-based koans, and add discord

This commit is contained in:
2025-09-04 02:29:48 +10:00
parent fda6847671
commit 89173f1676
10 changed files with 679 additions and 76 deletions

View File

@@ -1,34 +1,42 @@
from typing import Optional
from .data import Koan, KoanData
from .data import Koan, KoanInfo, KoansData
from .lib import utc_now
class KoanRegistry:
def __init__(self, data: KoanData):
self.data = data
def __init__(self, data: KoansData):
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_community_koans(self, communityid: int) -> list[KoanInfo]:
return await KoanInfo.fetch_where(communityid=communityid, is_deleted=False)
async def get_koaninfo(self, koanid: int) -> Optional[KoanInfo]:
return await KoanInfo.fetch(koanid)
async def get_koan(self, koanid: int) -> Optional[Koan]:
return await Koan.fetch(koanid)
async def get_koan_label(self, communityid: int, label: int) -> Optional[KoanInfo]:
results = await KoanInfo.fetch_where(communityid=communityid, koanlabel=label, is_deleted=False)
return results[0] if results else None
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)
async def create_koan(
self,
communityid: int,
content: str,
created_by: Optional[int] = None
) -> KoanInfo:
koan = await Koan.create(
content=content,
communityid=communityid,
name=name,
message=message
created_by=created_by,
)
return koan
info = await KoanInfo.fetch(koan.koanid)
assert info is not None
return info
async def delete_koan(self, koanid: int):
await self.data.koans.update_where(koanid=koanid).set(deleted_at=utc_now())