db867dcbe5
Deploy to Staging / deploy (push) Has been cancelled
Conflict detector / main (push) Has been cancelled
Lint Backend / lint-backend (push) Has been cancelled
Playwright Tests / changes (push) Has been cancelled
Test Backend / test-backend (push) Has been cancelled
Test Docker Compose / test-docker-compose (push) Has been cancelled
Playwright Tests / test-playwright (1, 4) (push) Has been cancelled
Playwright Tests / test-playwright (2, 4) (push) Has been cancelled
Playwright Tests / test-playwright (3, 4) (push) Has been cancelled
Playwright Tests / test-playwright (4, 4) (push) Has been cancelled
Playwright Tests / merge-playwright-reports (push) Has been cancelled
Playwright Tests / alls-green-playwright (push) Has been cancelled
Issue Manager / issue-manager (push) Has been cancelled
110 lines
3.2 KiB
Python
110 lines
3.2 KiB
Python
import uuid
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, HTTPException
|
|
from sqlmodel import func, select
|
|
|
|
from app.api.deps import CurrentUser, SessionDep
|
|
from app.models import Item, ItemCreate, ItemPublic, ItemsPublic, ItemUpdate, Message
|
|
|
|
router = APIRouter(prefix="/items", tags=["items"])
|
|
|
|
|
|
@router.get("/", response_model=ItemsPublic)
|
|
def read_items(
|
|
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
|
|
) -> Any:
|
|
"""
|
|
获取 Item 列表。
|
|
"""
|
|
|
|
if current_user.is_superuser:
|
|
count_statement = select(func.count()).select_from(Item)
|
|
count = session.exec(count_statement).one()
|
|
statement = select(Item).offset(skip).limit(limit)
|
|
items = session.exec(statement).all()
|
|
else:
|
|
count_statement = (
|
|
select(func.count())
|
|
.select_from(Item)
|
|
.where(Item.owner_id == current_user.id)
|
|
)
|
|
count = session.exec(count_statement).one()
|
|
statement = (
|
|
select(Item)
|
|
.where(Item.owner_id == current_user.id)
|
|
.offset(skip)
|
|
.limit(limit)
|
|
)
|
|
items = session.exec(statement).all()
|
|
|
|
return ItemsPublic(data=items, count=count)
|
|
|
|
|
|
@router.get("/{id}", response_model=ItemPublic)
|
|
def read_item(session: SessionDep, current_user: CurrentUser, id: uuid.UUID) -> Any:
|
|
"""
|
|
通过 ID 获取 Item。
|
|
"""
|
|
item = session.get(Item, id)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="项目未找到")
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
|
raise HTTPException(status_code=400, detail="权限不足")
|
|
return item
|
|
|
|
|
|
@router.post("/", response_model=ItemPublic)
|
|
def create_item(
|
|
*, session: SessionDep, current_user: CurrentUser, item_in: ItemCreate
|
|
) -> Any:
|
|
"""
|
|
创建新的 Item。
|
|
"""
|
|
item = Item.model_validate(item_in, update={"owner_id": current_user.id})
|
|
session.add(item)
|
|
session.commit()
|
|
session.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.put("/{id}", response_model=ItemPublic)
|
|
def update_item(
|
|
*,
|
|
session: SessionDep,
|
|
current_user: CurrentUser,
|
|
id: uuid.UUID,
|
|
item_in: ItemUpdate,
|
|
) -> Any:
|
|
"""
|
|
更新 Item。
|
|
"""
|
|
item = session.get(Item, id)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="项目未找到")
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
|
raise HTTPException(status_code=400, detail="权限不足")
|
|
update_dict = item_in.model_dump(exclude_unset=True)
|
|
item.sqlmodel_update(update_dict)
|
|
session.add(item)
|
|
session.commit()
|
|
session.refresh(item)
|
|
return item
|
|
|
|
|
|
@router.delete("/{id}")
|
|
def delete_item(
|
|
session: SessionDep, current_user: CurrentUser, id: uuid.UUID
|
|
) -> Message:
|
|
"""
|
|
删除 Item。
|
|
"""
|
|
item = session.get(Item, id)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="项目未找到")
|
|
if not current_user.is_superuser and (item.owner_id != current_user.id):
|
|
raise HTTPException(status_code=400, detail="权限不足")
|
|
session.delete(item)
|
|
session.commit()
|
|
return Message(message="项目删除成功")
|