From 8b1ed7da107239d3acdc66e3c3e1feb098bd56e1 Mon Sep 17 00:00:00 2001 From: htylight Date: Fri, 6 Oct 2023 16:44:27 +0800 Subject: [PATCH] implement changing group chat avatar --- src/crud/group_chat_crud.py | 9 +++++++++ src/crud/user_crud.py | 9 +++++++++ src/routers/group_chat.py | 16 ++++++++++++++++ src/routers/user_profile.py | 17 ++++++++++------- 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/src/crud/group_chat_crud.py b/src/crud/group_chat_crud.py index dd99b63..ce1299d 100644 --- a/src/crud/group_chat_crud.py +++ b/src/crud/group_chat_crud.py @@ -116,6 +116,15 @@ async def select_full_profile(group_chat_id: str) -> Tuple[GroupChat, list[Tuple return group_chat, res.all() +async def update_group_avatar(id: str, avatar_name: str): + session = async_session() + await session.execute( + update(GroupChat).where(GroupChat.id == id).values(avatar=avatar_name) + ) + await session.commit() + await session.close() + + async def update_group_name(group_chat_id: str, new_name: str): session = async_session() try: diff --git a/src/crud/user_crud.py b/src/crud/user_crud.py index f7f9fdf..8db7fa0 100755 --- a/src/crud/user_crud.py +++ b/src/crud/user_crud.py @@ -115,6 +115,15 @@ async def update_profile_sign(id: str, sign: str): 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() + + async def update_profile_avatar(id: str, avatar_name: str): session = async_session() await session.execute( diff --git a/src/routers/group_chat.py b/src/routers/group_chat.py index ed8f193..d421ddf 100644 --- a/src/routers/group_chat.py +++ b/src/routers/group_chat.py @@ -1,6 +1,8 @@ +from anyio import open_file from fastapi import APIRouter from pydantic import BaseModel +from .user_profile import Uint8List from ..crud import group_chat_crud from ..response_models.group_chat_response import ( BaseResponseModel, @@ -8,6 +10,7 @@ from ..response_models.group_chat_response import ( MemberNameAvatarResponse, FullProfileResponse, ) +from ..utils import static_file router = APIRouter(prefix="/group_chat", tags=["group_chat"]) @@ -102,6 +105,19 @@ async def get_full_profile(group_chat_id: str): } +@router.post("/change_avatar") +async def change_avatar(group_chat_id: str, file: Uint8List): + avatar_dir_path = static_file.create_avatar_dir("group_chat", "avatars") + avatar_filename = static_file.create_avatar_filename() + + async with await open_file(avatar_dir_path / avatar_filename, "wb") as f: + await f.write(bytearray(file.file)) + + await group_chat_crud.update_group_avatar(group_chat_id, avatar_filename) + + return {"code": 10300, "msg": "Update Avatar Successfully", "data": avatar_filename} + + @router.post("/change_name", response_model=BaseResponseModel) async def change_name(group_chat_id: str, new_name: str): try: diff --git a/src/routers/user_profile.py b/src/routers/user_profile.py index 0a2836f..6abae78 100755 --- a/src/routers/user_profile.py +++ b/src/routers/user_profile.py @@ -1,5 +1,3 @@ -from typing import Optional - from fastapi import APIRouter from pydantic import BaseModel from fastapi.responses import FileResponse, JSONResponse @@ -25,11 +23,12 @@ class Uint8List(BaseModel): class ChangedProfile(BaseModel): id: str - nickname: Optional[str] = None - location: Optional[str] = None - birthday: Optional[str] = None - gender: Optional[str] = None - sign: Optional[str] = None + nickname: str | None = None + location: str | None = None + birthday: str | None = None + gender: str | None = None + sign: str | None = None + status: str | None = None @router.get("/my", response_model=UserProfileResponse) @@ -80,6 +79,10 @@ async def change_profile(aspect: str, changed_profile: ChangedProfile): 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"}