restructure profile apis

main
htylight 2023-10-21 21:26:02 +08:00
parent 8b1ed7da10
commit ce795b02e6
3 changed files with 32 additions and 55 deletions

View File

@ -86,16 +86,21 @@ async def select_profile(id: str) -> UserProfile:
async def update_profile_basic( async def update_profile_basic(
id: str, id: str,
sign: str,
status: str,
nickname: str, nickname: str,
location: str,
birthday: str, birthday: str,
location: str,
gender: str, gender: str,
): ):
session = async_session() session = async_session()
try:
await session.execute( await session.execute(
update(UserProfile) update(UserProfile)
.where(UserProfile.user_id == id) .where(UserProfile.user_id == id)
.values( .values(
sign=sign,
status=status,
nickname=nickname, nickname=nickname,
location=location, location=location,
birthday=date.fromisoformat(birthday), birthday=date.fromisoformat(birthday),
@ -103,24 +108,10 @@ async def update_profile_basic(
) )
) )
await session.commit() await session.commit()
await session.close() except Exception as e:
print(f"Updating basic profile fail with error: {e}")
raise Exception(e)
async def update_profile_sign(id: str, sign: str): finally:
session = async_session()
await session.execute(
update(UserProfile).where(UserProfile.user_id == id).values(sign=sign)
)
await session.commit()
await session.close()
async def update_profile_status(id: str, status: str):
session = async_session()
await session.execute(
update(UserProfile).where(UserProfile.user_id == id).values(status=status)
)
await session.commit()
await session.close() await session.close()
@ -159,6 +150,7 @@ async def select_account_profile(
try: try:
return True, res.one() return True, res.one()
except NoResultFound: except NoResultFound:
print(f"账号 <{value}> 不存在")
return False, None return False, None

View File

@ -54,7 +54,7 @@ class UserProfile(Base):
location: Mapped[str] = mapped_column(String, nullable=True) location: Mapped[str] = mapped_column(String, nullable=True)
status: Mapped[str] = mapped_column(String, nullable=True) status: Mapped[str] = mapped_column(String, nullable=True)
sign: Mapped[str] = mapped_column(String, nullable=True) sign: Mapped[str] = mapped_column(String, nullable=True)
avatar: Mapped[str] = mapped_column(String, nullable=True) avatar: Mapped[str] = mapped_column(String, nullable=True, default="user.png")
user_id: Mapped[str] = mapped_column( user_id: Mapped[str] = mapped_column(
ForeignKey("user_account.id", ondelete="CASCADE") ForeignKey("user_account.id", ondelete="CASCADE")
) )
@ -146,7 +146,7 @@ class GroupChat(Base):
introduction: Mapped[str] = mapped_column(String(100), default="") introduction: Mapped[str] = mapped_column(String(100), default="")
tags: Mapped[list[str]] = mapped_column(ARRAY(String), default=[]) tags: Mapped[list[str]] = mapped_column(ARRAY(String), default=[])
noticeboard: Mapped[list[dict]] = mapped_column(JSONB, default=[]) noticeboard: Mapped[list[dict]] = mapped_column(JSONB, default=[])
avatar: Mapped[str] = mapped_column(String, default="") avatar: Mapped[str] = mapped_column(String, default="Logo_light.png")
created_at: Mapped[datetime] = mapped_column( created_at: Mapped[datetime] = mapped_column(
default=datetime.now, onupdate=datetime.now default=datetime.now, onupdate=datetime.now
) )

View File

@ -21,7 +21,7 @@ class Uint8List(BaseModel):
file: list file: list
class ChangedProfile(BaseModel): class BasicProfile(BaseModel):
id: str id: str
nickname: str | None = None nickname: str | None = None
location: str | None = None location: str | None = None
@ -64,25 +64,10 @@ async def change_avatar(id: str, file: Uint8List):
return {"code": 10300, "msg": "Update Avatar Successfully", "data": avatar_filename} return {"code": 10300, "msg": "Update Avatar Successfully", "data": avatar_filename}
@router.post("/change/{aspect}", response_model=BaseResponseModel) @router.post("/change/basic", response_model=BaseResponseModel)
async def change_profile(aspect: str, changed_profile: ChangedProfile): async def change_profile_basic(basic_profile: BasicProfile):
match aspect: try:
case "basic": await user_crud.update_profile_basic(**basic_profile.model_dump())
await user_crud.update_profile_basic( except Exception as e:
changed_profile.id, return {"code": 10301, "msg": f"Updating Basic Profile Fail: {e}"}
changed_profile.nickname, return {"code": 10300, "msg": f"Update basic Profile Successfully"}
changed_profile.location,
changed_profile.birthday,
changed_profile.gender,
)
case "sign":
await user_crud.update_profile_sign(
changed_profile.id, changed_profile.sign
)
case "status":
await user_crud.update_profile_status(
changed_profile.id, changed_profile.status
)
case _:
return {"code": 10301, "msg": f"No /change/{aspect} Path"}
return {"code": 10300, "msg": f"Update {aspect} Profile Successfully"}