You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.8 KiB
120 lines
3.8 KiB
"""
|
|
{{ table.comment }} Router - 对齐JAVA Controller
|
|
|
|
@author: {{ author }}
|
|
@date: {{ date }}
|
|
"""
|
|
from datetime import datetime
|
|
from fastapi import APIRouter, Depends, HTTPException, Header
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
|
|
from app.database import get_db
|
|
from app.schemas.{{ table.name_lower }} import {{ table.entity }}
|
|
from app.services.{{ table.name_lower }}_service import {{ table.entity }}Service
|
|
from app.utils.result import Result
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/page", summary="{{ table.comment }}分页列表查询")
|
|
async def page(
|
|
param: {{ table.entity }},
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}分页列表查询 - 对齐JAVA: @PostMapping("/page")"""
|
|
if not token:
|
|
return Result.error("token不能为空")
|
|
|
|
page_num = param.pageNum if param.pageNum else 1
|
|
page_size = param.pageSize if param.pageSize else 10
|
|
|
|
items, total = {{ table.entity }}Service.get_list(
|
|
db=db,
|
|
param=param,
|
|
page=page_num,
|
|
page_size=page_size
|
|
)
|
|
|
|
page_data = {
|
|
"records": [item.__dict__ for item in items],
|
|
"total": total,
|
|
"size": page_size,
|
|
"current": page_num,
|
|
"pages": (total + page_size - 1) // page_size
|
|
}
|
|
return Result.OK(page_data)
|
|
|
|
|
|
@router.post("/info", summary="{{ table.comment }}根据条件查询")
|
|
async def info(
|
|
param: {{ table.entity }},
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}根据条件查询 - 对齐JAVA: @PostMapping("/info")"""
|
|
if not token:
|
|
return Result.error("token不能为空")
|
|
|
|
data = {{ table.entity }}Service.info(db=db, param=param)
|
|
return Result.OK(data)
|
|
|
|
|
|
@router.post("/add", summary="{{ table.comment }}新增")
|
|
async def add(
|
|
param: {{ table.entity }},
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}新增 - 对齐JAVA: @PostMapping("/add")"""
|
|
if not token:
|
|
return Result.error("token不能为空")
|
|
|
|
{{ table.entity }}Service.add(db=db, param=param)
|
|
return Result.OK()
|
|
|
|
|
|
@router.post("/modify", summary="{{ table.comment }}修改")
|
|
async def modify(
|
|
param: {{ table.entity }},
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}修改 - 对齐JAVA: @PostMapping("/modify")"""
|
|
if not token:
|
|
return Result.error("token不能为空")
|
|
|
|
info = {{ table.entity }}Service.get_by_id(db=db, id=param.id)
|
|
if info is None:
|
|
return Result.error(f"[{param.id}]记录不存在")
|
|
|
|
{{ table.entity }}Service.modify(db=db, param=param)
|
|
return Result.OK()
|
|
|
|
|
|
@router.get("/remove/{id}", summary="{{ table.comment }}删除(单个条目)")
|
|
async def remove(
|
|
id: int,
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}删除(单个条目) - 对齐JAVA: @GetMapping("/remove/{id}")"""
|
|
{{ table.entity }}Service.remove(db=db, id=id)
|
|
return Result.OK()
|
|
|
|
|
|
@router.post("/removes", summary="{{ table.comment }}删除(多个条目)")
|
|
async def removes(
|
|
ids: List[int],
|
|
token: str = Header(..., description="认证token"),
|
|
version: str = Header("1.0", description="版本号"),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""{{ table.comment }}删除(多个条目) - 对齐JAVA: @PostMapping("/removes")"""
|
|
{{ table.entity }}Service.removes(db=db, ids=ids)
|
|
return Result.OK()
|
|
|