🔧 修复并完善用户数据库读写

This commit is contained in:
洛水居室 2022-08-05 20:03:57 +08:00
parent 5a3810c44a
commit 13abae5a19
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
3 changed files with 24 additions and 4 deletions

View File

@ -1,6 +1,8 @@
from typing import Optional
from sqlmodel import SQLModel, Field
from sqlmodel import SQLModel, Field, Enum, Column
from models.base import RegionEnum
class User(SQLModel, table=True):
@ -8,4 +10,4 @@ class User(SQLModel, table=True):
user_id: int = Field()
yuanshen_uid: int = Field()
genshin_uid: int = Field()
region: int = Field()
region: Optional[RegionEnum] = Field(sa_column=Column(Enum(RegionEnum)))

View File

@ -20,6 +20,19 @@ class UserRepository:
user = results.first()
return user
async def update_user(self, user: User):
async with self.mysql.Session() as session:
session = cast(AsyncSession, session)
await session.add(user)
await session.commit()
await session.refresh(user)
async def add_user(self, user: User):
async with self.mysql.Session() as session:
session = cast(AsyncSession, session)
await session.add(user)
await session.commit()
class UserNotFoundError(NotFoundError):
entity_name: str = "User"

View File

@ -12,5 +12,10 @@ class UserService:
:param user_id:用户ID
:return: User
"""
user = await self._repository.get_by_user_id(user_id)
return user
return await self._repository.get_by_user_id(user_id)
async def update_user(self, user: User) -> User:
return await self._repository.update_user(user)
async def add_user(self, user: User) -> User:
return await self._repository.update_user(user)