响应格式修改,原始数据查询修改
This commit is contained in:
@@ -2,33 +2,47 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from ..core.database import get_db
|
||||
from ..core.response_code import ResponseCode, ResponseMessage
|
||||
from ..schemas.account import (
|
||||
AccountCreate, AccountUpdate, AccountResponse,
|
||||
AccountListRequest, AccountGetRequest, AccountUpdateRequest, AccountDeleteRequest
|
||||
AccountListRequest, AccountGetRequest, AccountUpdateRequest, AccountDeleteRequest,
|
||||
AccountApiResponse, AccountListResponse
|
||||
)
|
||||
from ..services.account import AccountService
|
||||
|
||||
router = APIRouter(prefix="/accounts", tags=["账号管理"])
|
||||
|
||||
@router.post("/create", response_model=AccountResponse, status_code=status.HTTP_201_CREATED)
|
||||
@router.post("/create", response_model=AccountApiResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_account(account: AccountCreate, db: Session = Depends(get_db)):
|
||||
"""创建账号"""
|
||||
# 检查账号是否已存在
|
||||
existing_account = AccountService.get_account_by_username(db, account.username)
|
||||
if existing_account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="用户名已存在"
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.ACCOUNT_EXISTS,
|
||||
message=ResponseMessage.ACCOUNT_EXISTS,
|
||||
data=None
|
||||
)
|
||||
|
||||
return AccountService.create_account(db, account)
|
||||
account_response = AccountService.create_account(db, account)
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="账号创建成功",
|
||||
data=account_response.dict()
|
||||
)
|
||||
|
||||
@router.post("/list", response_model=List[AccountResponse])
|
||||
@router.post("/list", response_model=AccountListResponse)
|
||||
def get_accounts(request: AccountListRequest, db: Session = Depends(get_db)):
|
||||
"""获取账号列表"""
|
||||
return AccountService.get_accounts(db, skip=request.skip, limit=request.limit)
|
||||
accounts = AccountService.get_accounts(db, skip=request.skip, limit=request.limit)
|
||||
return AccountListResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="查询成功",
|
||||
total=len(accounts),
|
||||
data=accounts
|
||||
)
|
||||
|
||||
@router.post("/get", response_model=List[AccountResponse])
|
||||
@router.post("/get", response_model=AccountListResponse)
|
||||
def get_account(request: AccountGetRequest, db: Session = Depends(get_db)):
|
||||
"""根据多种条件查询账号"""
|
||||
accounts = AccountService.search_accounts(
|
||||
@@ -40,28 +54,46 @@ def get_account(request: AccountGetRequest, db: Session = Depends(get_db)):
|
||||
today_updated=request.today_updated
|
||||
)
|
||||
if not accounts:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="未找到符合条件的账号"
|
||||
return AccountListResponse(
|
||||
code=ResponseCode.ACCOUNT_NOT_FOUND,
|
||||
message=ResponseMessage.ACCOUNT_NOT_FOUND,
|
||||
total=0,
|
||||
data=[]
|
||||
)
|
||||
return accounts
|
||||
return AccountListResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="查询成功",
|
||||
total=len(accounts),
|
||||
data=accounts
|
||||
)
|
||||
|
||||
@router.post("/update", response_model=AccountResponse)
|
||||
@router.post("/update", response_model=AccountApiResponse)
|
||||
def update_account(request: AccountUpdateRequest, db: Session = Depends(get_db)):
|
||||
"""更新账号"""
|
||||
account = AccountService.update_account(db, request.account_id, request.account_data)
|
||||
if not account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.ACCOUNT_NOT_FOUND,
|
||||
message=ResponseMessage.ACCOUNT_NOT_FOUND,
|
||||
data=None
|
||||
)
|
||||
return account
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="账号更新成功",
|
||||
data=account.dict()
|
||||
)
|
||||
|
||||
@router.post("/delete", status_code=status.HTTP_204_NO_CONTENT)
|
||||
@router.post("/delete", response_model=AccountApiResponse)
|
||||
def delete_account(request: AccountDeleteRequest, db: Session = Depends(get_db)):
|
||||
"""删除账号"""
|
||||
if not AccountService.delete_account(db, request.account_id):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
)
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.ACCOUNT_NOT_FOUND,
|
||||
message=ResponseMessage.ACCOUNT_NOT_FOUND,
|
||||
data=None
|
||||
)
|
||||
return AccountApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="账号删除成功",
|
||||
data=None
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user