mirror of
https://github.com/PaiGramTeam/PaiGram.git
synced 2025-01-19 01:43:55 +00:00
🔧 修复并完善用户数据库读写
This commit is contained in:
parent
5a3810c44a
commit
13abae5a19
@ -1,6 +1,8 @@
|
|||||||
from typing import Optional
|
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):
|
class User(SQLModel, table=True):
|
||||||
@ -8,4 +10,4 @@ class User(SQLModel, table=True):
|
|||||||
user_id: int = Field()
|
user_id: int = Field()
|
||||||
yuanshen_uid: int = Field()
|
yuanshen_uid: int = Field()
|
||||||
genshin_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()
|
user = results.first()
|
||||||
return user
|
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):
|
class UserNotFoundError(NotFoundError):
|
||||||
entity_name: str = "User"
|
entity_name: str = "User"
|
||||||
|
@ -12,5 +12,10 @@ class UserService:
|
|||||||
:param user_id:用户ID
|
:param user_id:用户ID
|
||||||
:return: User
|
:return: User
|
||||||
"""
|
"""
|
||||||
user = await self._repository.get_by_user_id(user_id)
|
return await self._repository.get_by_user_id(user_id)
|
||||||
return user
|
|
||||||
|
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