Skip to content

Update the uniform return method to success #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions backend/app/api/v1/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ async def user_login(obj: Auth):
token, user = await UserService.login(obj)
# TODO: token 存储
data = Token(access_token=token, user=user)
return response_base.response_200(data=data)
return response_base.success(data=data)


@router.post('/logout', summary='用户登出', dependencies=[DependsUser])
async def user_logout():
# TODO: 加入 token 黑名单
return response_base.response_200()
return response_base.success()
18 changes: 9 additions & 9 deletions backend/app/api/v1/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,64 +16,64 @@
@router.post('/register', summary='用户注册')
async def user_register(obj: CreateUser):
await UserService.register(obj)
return response_base.response_200()
return response_base.success()


@router.post('/password/reset', summary='密码重置')
async def password_reset(obj: ResetPassword):
await UserService.pwd_reset(obj)
return response_base.response_200()
return response_base.success()


@router.get('/{username}', summary='查看用户信息', dependencies=[DependsUser])
async def userinfo(username: str):
current_user = await UserService.get_userinfo(username)
data = GetUserInfo(**select_to_json(current_user))
return response_base.response_200(data=data, exclude={'password'})
return response_base.success(data=data, exclude={'password'})


@router.put('/{username}', summary='更新用户信息')
async def update_userinfo(username: str, obj: UpdateUser, current_user: CurrentUser):
count = await UserService.update(username=username, current_user=current_user, obj=obj)
if count > 0:
return response_base.response_200()
return response_base.success()
return response_base.fail()


@router.put('/{username}/avatar', summary='更新头像')
async def update_avatar(username: str, avatar: Avatar, current_user: CurrentUser):
count = await UserService.update_avatar(username=username, current_user=current_user, avatar=avatar)
if count > 0:
return response_base.response_200()
return response_base.success()
return response_base.fail()


@router.get('', summary='获取所有用户', dependencies=[DependsUser, PageDepends])
async def get_all_users(db: CurrentSession):
user_list = await UserService.get_user_list()
page_data = await paging_data(db, user_list, GetUserInfo)
return response_base.response_200(data=page_data)
return response_base.success(data=page_data)


@router.post('/{pk}/super', summary='修改用户超级权限', dependencies=[DependsSuperUser])
async def super_set(pk: int):
count = await UserService.update_permission(pk)
if count > 0:
return response_base.response_200()
return response_base.success()
return response_base.fail()


@router.post('/{pk}/action', summary='修改用户状态', dependencies=[DependsSuperUser])
async def active_set(pk: int):
count = await UserService.update_active(pk)
if count > 0:
return response_base.response_200()
return response_base.success()
return response_base.fail()


@router.delete('/{username}', summary='用户注销', description='用户注销 != 用户退出,注销之后用户将从数据库删除')
async def delete_user(username: str, current_user: CurrentUser):
count = await UserService.delete(username=username, current_user=current_user)
if count > 0:
return response_base.response_200()
return response_base.success()
return response_base.fail()
6 changes: 0 additions & 6 deletions backend/app/common/response/response_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,5 @@ def fail(*, code: int = 400, msg: str = 'Bad Request', data: Any = None, exclude
data = data if data is None else ResponseBase.__encode_json(data)
return ResponseModel(code=code, msg=msg, data=data).dict(exclude={'data': exclude})

@staticmethod
@validate_arguments
def response_200(*, msg: str = 'Success', data: Any | None = None, exclude: _JsonEncoder | None = None):
data = data if data is None else ResponseBase.__encode_json(data)
return ResponseModel(code=200, msg=msg, data=data).dict(exclude={'data': exclude})


response_base = ResponseBase()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cryptography==39.0.1
email-validator==1.1.3
Faker==9.7.1
fast-captcha==0.1.3
fastapi==0.95.0
fastapi==0.95.2
fastapi-pagination==0.12.1
gunicorn==20.1.0
httpx==0.23.0
Expand Down