初始化
This commit is contained in:
60
app/api/account.py
Normal file
60
app/api/account.py
Normal file
@@ -0,0 +1,60 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import List
|
||||
from ..core.database import get_db
|
||||
from ..schemas.account import (
|
||||
AccountCreate, AccountUpdate, AccountResponse,
|
||||
AccountListRequest, AccountGetRequest, AccountUpdateRequest, AccountDeleteRequest
|
||||
)
|
||||
from ..services.account import AccountService
|
||||
|
||||
router = APIRouter(prefix="/accounts", tags=["账号管理"])
|
||||
|
||||
@router.post("/create", response_model=AccountResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_account(account: AccountCreate, db: Session = Depends(get_db)):
|
||||
"""创建账号"""
|
||||
# 检查账号是否已存在
|
||||
existing_account = AccountService.get_account_by_account(db, account.account)
|
||||
if existing_account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="账号已存在"
|
||||
)
|
||||
|
||||
return AccountService.create_account(db, account)
|
||||
|
||||
@router.post("/list", response_model=List[AccountResponse])
|
||||
def get_accounts(request: AccountListRequest, db: Session = Depends(get_db)):
|
||||
"""获取账号列表"""
|
||||
return AccountService.get_accounts(db, skip=request.skip, limit=request.limit)
|
||||
|
||||
@router.post("/get", response_model=AccountResponse)
|
||||
def get_account(request: AccountGetRequest, db: Session = Depends(get_db)):
|
||||
"""根据ID获取账号"""
|
||||
account = AccountService.get_account(db, request.account_id)
|
||||
if not account:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="账号不存在"
|
||||
)
|
||||
return account
|
||||
|
||||
@router.post("/update", response_model=AccountResponse)
|
||||
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 account
|
||||
|
||||
@router.post("/delete", status_code=status.HTTP_204_NO_CONTENT)
|
||||
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="账号不存在"
|
||||
)
|
||||
Reference in New Issue
Block a user