mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-22 06:17:54 +00:00
🔧 修复并完善用户数据库读写
This commit is contained in:
parent
5a3810c44a
commit
13abae5a19
@ -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)))
|
||||
|
@ -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"
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user