93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
|
|
"""
|
||
|
|
Item 路由
|
||
|
|
"""
|
||
|
|
from typing import List
|
||
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||
|
|
from tortoise.exceptions import DoesNotExist
|
||
|
|
from app.models.item import Item
|
||
|
|
from app.schemas.item import ItemCreate, ItemUpdate, ItemResponse
|
||
|
|
from app.api.dependencies import get_db
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
@router.get("/", response_model=List[ItemResponse], summary="获取物品列表")
|
||
|
|
async def read_items(
|
||
|
|
skip: int = 0,
|
||
|
|
limit: int = 100,
|
||
|
|
db=Depends(get_db)
|
||
|
|
):
|
||
|
|
"""获取物品列表"""
|
||
|
|
items = await Item.all().offset(skip).limit(limit)
|
||
|
|
return items
|
||
|
|
|
||
|
|
@router.get("/{item_id}", response_model=ItemResponse, summary="获取物品详情")
|
||
|
|
async def read_item(item_id: int, db=Depends(get_db)):
|
||
|
|
"""根据ID获取物品"""
|
||
|
|
try:
|
||
|
|
item = await Item.get(id=item_id)
|
||
|
|
return item
|
||
|
|
except DoesNotExist:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail=f"物品 ID {item_id} 不存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.post("/", response_model=ItemResponse, status_code=status.HTTP_201_CREATED, summary="创建物品")
|
||
|
|
async def create_item(item: ItemCreate, db=Depends(get_db)):
|
||
|
|
"""创建新物品"""
|
||
|
|
# 检查名称是否已存在
|
||
|
|
existing_item = await Item.filter(name=item.name).first()
|
||
|
|
if existing_item:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||
|
|
detail=f"物品名称 '{item.name}' 已存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
# 创建物品
|
||
|
|
item_dict = item.model_dump()
|
||
|
|
new_item = await Item.create(**item_dict)
|
||
|
|
return new_item
|
||
|
|
|
||
|
|
@router.put("/{item_id}", response_model=ItemResponse, summary="更新物品")
|
||
|
|
async def update_item(item_id: int, item_update: ItemUpdate, db=Depends(get_db)):
|
||
|
|
"""更新物品信息"""
|
||
|
|
try:
|
||
|
|
item = await Item.get(id=item_id)
|
||
|
|
|
||
|
|
# 更新字段
|
||
|
|
update_data = item_update.model_dump(exclude_unset=True)
|
||
|
|
|
||
|
|
# 如果更新名称,检查是否与其他物品重名
|
||
|
|
if "name" in update_data:
|
||
|
|
existing_item = await Item.filter(
|
||
|
|
name=update_data["name"]
|
||
|
|
).exclude(id=item_id).first()
|
||
|
|
if existing_item:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||
|
|
detail=f"物品名称 '{update_data['name']}' 已存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
# 应用更新
|
||
|
|
await item.update_from_dict(update_data)
|
||
|
|
await item.save()
|
||
|
|
|
||
|
|
return item
|
||
|
|
except DoesNotExist:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail=f"物品 ID {item_id} 不存在"
|
||
|
|
)
|
||
|
|
|
||
|
|
@router.delete("/{item_id}", status_code=status.HTTP_204_NO_CONTENT, summary="删除物品")
|
||
|
|
async def delete_item(item_id: int, db=Depends(get_db)):
|
||
|
|
"""删除物品"""
|
||
|
|
try:
|
||
|
|
item = await Item.get(id=item_id)
|
||
|
|
await item.delete()
|
||
|
|
return None
|
||
|
|
except DoesNotExist:
|
||
|
|
raise HTTPException(
|
||
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
||
|
|
detail=f"物品 ID {item_id} 不存在"
|
||
|
|
)
|