铁科院功能模块
This commit is contained in:
100
app/api/function_list.py
Normal file
100
app/api/function_list.py
Normal file
@@ -0,0 +1,100 @@
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from sqlalchemy.orm import Session
|
||||
from ..core.database import get_db
|
||||
from ..core.response_code import ResponseCode
|
||||
from ..schemas.function_list import (
|
||||
FunctionListCreate, FunctionListRequest, FunctionGetRequest,
|
||||
FunctionUpdateRequest, FunctionDeleteRequest,
|
||||
FunctionApiResponse, FunctionListApiResponse
|
||||
)
|
||||
from ..services.function_list import FunctionListService
|
||||
|
||||
router = APIRouter(prefix="/functions", tags=["铁科平台功能管理"])
|
||||
|
||||
|
||||
@router.post("/create", response_model=FunctionApiResponse, status_code=status.HTTP_201_CREATED)
|
||||
def create_function(func_data: FunctionListCreate, db: Session = Depends(get_db)):
|
||||
"""创建功能"""
|
||||
# 检查功能名称是否已存在
|
||||
existing = FunctionListService.get_function_by_name(db, func_data.function_name)
|
||||
if existing:
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.DATA_EXISTS,
|
||||
message="功能名称已存在",
|
||||
data=None
|
||||
)
|
||||
|
||||
func_response = FunctionListService.create_function(db, func_data)
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="功能创建成功",
|
||||
data=func_response.model_dump()
|
||||
)
|
||||
|
||||
|
||||
@router.post("/list", response_model=FunctionListApiResponse)
|
||||
def get_functions(request: FunctionListRequest, db: Session = Depends(get_db)):
|
||||
"""获取功能列表"""
|
||||
functions = FunctionListService.get_functions(db, skip=request.skip, limit=request.limit)
|
||||
return FunctionListApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="查询成功",
|
||||
total=len(functions),
|
||||
data=functions
|
||||
)
|
||||
|
||||
|
||||
@router.post("/get", response_model=FunctionListApiResponse)
|
||||
def get_function(request: FunctionGetRequest, db: Session = Depends(get_db)):
|
||||
"""根据条件查询功能"""
|
||||
functions = FunctionListService.search_functions(
|
||||
db,
|
||||
func_id=request.id,
|
||||
function_name=request.function_name
|
||||
)
|
||||
if not functions:
|
||||
return FunctionListApiResponse(
|
||||
code=ResponseCode.DATA_NOT_FOUND,
|
||||
message="功能不存在",
|
||||
total=0,
|
||||
data=[]
|
||||
)
|
||||
return FunctionListApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="查询成功",
|
||||
total=len(functions),
|
||||
data=functions
|
||||
)
|
||||
|
||||
|
||||
@router.post("/update", response_model=FunctionApiResponse)
|
||||
def update_function(request: FunctionUpdateRequest, db: Session = Depends(get_db)):
|
||||
"""更新功能"""
|
||||
func = FunctionListService.update_function(db, request.id, request.function_data)
|
||||
if not func:
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.DATA_NOT_FOUND,
|
||||
message="功能不存在",
|
||||
data=None
|
||||
)
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="功能更新成功",
|
||||
data=func.model_dump()
|
||||
)
|
||||
|
||||
|
||||
@router.post("/delete", response_model=FunctionApiResponse)
|
||||
def delete_function(request: FunctionDeleteRequest, db: Session = Depends(get_db)):
|
||||
"""删除功能"""
|
||||
if not FunctionListService.delete_function(db, request.id):
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.DATA_NOT_FOUND,
|
||||
message="功能不存在",
|
||||
data=None
|
||||
)
|
||||
return FunctionApiResponse(
|
||||
code=ResponseCode.SUCCESS,
|
||||
message="功能删除成功",
|
||||
data=None
|
||||
)
|
||||
Reference in New Issue
Block a user