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

View File

@ -54,7 +54,7 @@ class UserProfile(Base):
location: Mapped[str] = mapped_column(String, nullable=True)
status: 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(
ForeignKey("user_account.id", ondelete="CASCADE")
)
@ -146,7 +146,7 @@ class GroupChat(Base):
introduction: Mapped[str] = mapped_column(String(100), default="")
tags: Mapped[list[str]] = mapped_column(ARRAY(String), 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(
default=datetime.now, onupdate=datetime.now
)

View File

@ -21,7 +21,7 @@ class Uint8List(BaseModel):
file: list
class ChangedProfile(BaseModel):
class BasicProfile(BaseModel):
id: str
nickname: 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}
@router.post("/change/{aspect}", response_model=BaseResponseModel)
async def change_profile(aspect: str, changed_profile: ChangedProfile):
match aspect:
case "basic":
await user_crud.update_profile_basic(
changed_profile.id,
changed_profile.nickname,
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"}
@router.post("/change/basic", response_model=BaseResponseModel)
async def change_profile_basic(basic_profile: BasicProfile):
try:
await user_crud.update_profile_basic(**basic_profile.model_dump())
except Exception as e:
return {"code": 10301, "msg": f"Updating Basic Profile Fail: {e}"}
return {"code": 10300, "msg": f"Update basic Profile Successfully"}