feat: 实现起卦、设置与积分系统

This commit is contained in:
qzl
2026-04-03 16:56:47 +08:00
parent 31594558eb
commit f245eec5f6
170 changed files with 20728 additions and 328 deletions
+1
View File
@@ -0,0 +1 @@
from __future__ import annotations
+52
View File
@@ -0,0 +1,52 @@
from __future__ import annotations
from dataclasses import dataclass
from uuid import UUID, uuid4
from schemas.enums import MemoryType
@dataclass
class MemoryRecord:
id: UUID
owner_id: UUID
memory_type: MemoryType
content: dict
class SQLAlchemyMemoriesRepository:
def __init__(self, session: object) -> None:
self._session = session
async def get_user_memory_for_owner(self, *, owner_id: UUID) -> MemoryRecord | None:
_ = self._session
_ = owner_id
return None
async def get_work_memory_for_owner(self, *, owner_id: UUID) -> MemoryRecord | None:
_ = self._session
_ = owner_id
return None
async def create(
self,
*,
owner_id: UUID,
memory_type: MemoryType,
content: dict,
) -> MemoryRecord:
return MemoryRecord(
id=uuid4(),
owner_id=owner_id,
memory_type=memory_type,
content=content,
)
async def update_content(
self,
memory: MemoryRecord,
content: dict | None = None,
) -> MemoryRecord:
if content is not None:
memory.content = content
return memory
+76
View File
@@ -0,0 +1,76 @@
from __future__ import annotations
from core.auth.models import CurrentUser
from schemas.enums import MemoryType
from schemas.domain.memory_content import UserMemoryContent, WorkProfileContent
from v1.memories.repository import MemoryRecord, SQLAlchemyMemoriesRepository
class MemoriesService:
def __init__(
self,
repository: SQLAlchemyMemoriesRepository,
session: object,
current_user: CurrentUser | None,
) -> None:
self._repository = repository
self._session = session
self._current_user = current_user
def _require_user_id(self):
if self._current_user is None:
raise ValueError("current user is required")
return self._current_user.id
async def get_all_memories(
self,
) -> dict[str, UserMemoryContent | WorkProfileContent | None]:
owner_id = self._require_user_id()
user_memory = await self._repository.get_user_memory_for_owner(
owner_id=owner_id
)
work_memory = await self._repository.get_work_memory_for_owner(
owner_id=owner_id
)
return {
"user_memory": UserMemoryContent.model_validate(user_memory.content)
if user_memory is not None
else None,
"work_memory": WorkProfileContent.model_validate(work_memory.content)
if work_memory is not None
else None,
}
async def get_memory_model(self, *, memory_type: MemoryType) -> MemoryRecord | None:
owner_id = self._require_user_id()
if memory_type == MemoryType.USER:
return await self._repository.get_user_memory_for_owner(owner_id=owner_id)
return await self._repository.get_work_memory_for_owner(owner_id=owner_id)
async def update_user_memory(self, *, content: UserMemoryContent) -> MemoryRecord:
owner_id = self._require_user_id()
existing = await self._repository.get_user_memory_for_owner(owner_id=owner_id)
if existing is not None:
return await self._repository.update_content(
existing,
content.model_dump(mode="json", exclude_none=True),
)
return await self._repository.create(
owner_id=owner_id,
memory_type=MemoryType.USER,
content=content.model_dump(mode="json", exclude_none=True),
)
async def update_work_memory(self, *, content: WorkProfileContent) -> MemoryRecord:
owner_id = self._require_user_id()
existing = await self._repository.get_work_memory_for_owner(owner_id=owner_id)
if existing is not None:
return await self._repository.update_content(
existing,
content.model_dump(mode="json", exclude_none=True),
)
return await self._repository.create(
owner_id=owner_id,
memory_type=MemoryType.WORK,
content=content.model_dump(mode="json", exclude_none=True),
)