|
| 1 | +"""API endpoints for Projects.""" |
| 2 | + |
| 3 | +import math |
| 4 | +from typing import Any |
| 5 | +from uuid import UUID |
| 6 | + |
| 7 | +from fastapi import APIRouter, Depends, HTTPException, Query, status |
| 8 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 9 | + |
| 10 | +from app.auth.dependencies import get_current_user, require_reviewer_or_admin |
| 11 | +from app.crud import project as crud |
| 12 | +from app.crud.audit_log import write_audit |
| 13 | +from app.db.session import get_db |
| 14 | +from app.models.user import User |
| 15 | +from app.schemas.pagination import PaginatedResponse |
| 16 | +from app.schemas.project import ProjectCreate, ProjectResponse, ProjectUpdate |
| 17 | + |
| 18 | +router = APIRouter() |
| 19 | + |
| 20 | + |
| 21 | +@router.post("/projects", response_model=ProjectResponse, status_code=status.HTTP_201_CREATED) |
| 22 | +async def create_project( |
| 23 | + payload: ProjectCreate, |
| 24 | + db: AsyncSession = Depends(get_db), |
| 25 | + current_user: User = Depends(require_reviewer_or_admin), |
| 26 | +) -> ProjectResponse: |
| 27 | + project = await crud.create_project(db, payload) |
| 28 | + await write_audit( |
| 29 | + db, |
| 30 | + actor_kind="user", |
| 31 | + actor_id=current_user.id, |
| 32 | + action="project.create", |
| 33 | + resource_type="project", |
| 34 | + resource_id=project.id, |
| 35 | + details=payload.model_dump(), |
| 36 | + ) |
| 37 | + return project |
| 38 | + |
| 39 | + |
| 40 | +@router.get("/projects", response_model=PaginatedResponse[ProjectResponse]) |
| 41 | +async def list_projects( |
| 42 | + page: int = Query(1, ge=1), |
| 43 | + page_size: int = Query(50, ge=1, le=200), |
| 44 | + db: AsyncSession = Depends(get_db), |
| 45 | + _current_user: User = Depends(get_current_user), |
| 46 | +) -> PaginatedResponse[ProjectResponse]: |
| 47 | + skip = (page - 1) * page_size |
| 48 | + items, total = await crud.list_projects(db, skip=skip, limit=page_size) |
| 49 | + return PaginatedResponse( |
| 50 | + items=items, |
| 51 | + total=total, |
| 52 | + page=page, |
| 53 | + page_size=page_size, |
| 54 | + pages=math.ceil(total / page_size) if total > 0 else 0, |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +@router.get("/projects/{project_id}", response_model=ProjectResponse) |
| 59 | +async def get_project( |
| 60 | + project_id: UUID, |
| 61 | + db: AsyncSession = Depends(get_db), |
| 62 | + _current_user: User = Depends(get_current_user), |
| 63 | +) -> ProjectResponse: |
| 64 | + project = await crud.get_project(db, project_id) |
| 65 | + if project is None: |
| 66 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Project {project_id} not found") |
| 67 | + return project |
| 68 | + |
| 69 | + |
| 70 | +@router.patch("/projects/{project_id}", response_model=ProjectResponse) |
| 71 | +async def update_project( |
| 72 | + project_id: UUID, |
| 73 | + payload: ProjectUpdate, |
| 74 | + db: AsyncSession = Depends(get_db), |
| 75 | + current_user: User = Depends(require_reviewer_or_admin), |
| 76 | +) -> ProjectResponse: |
| 77 | + existing = await crud.get_project(db, project_id) |
| 78 | + if existing is None: |
| 79 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Project {project_id} not found") |
| 80 | + |
| 81 | + changed_fields = payload.model_dump(exclude_unset=True) |
| 82 | + original_values = {field: getattr(existing, field) for field in changed_fields} |
| 83 | + updated = await crud.update_project(db, project_id, payload) |
| 84 | + if updated is None: |
| 85 | + raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"Project {project_id} not found") |
| 86 | + |
| 87 | + diff: dict[str, dict[str, Any]] = {} |
| 88 | + for field, new_value in changed_fields.items(): |
| 89 | + old_value = original_values[field] |
| 90 | + if old_value != new_value: |
| 91 | + diff[field] = {"from": old_value, "to": new_value} |
| 92 | + |
| 93 | + await write_audit( |
| 94 | + db, |
| 95 | + actor_kind="user", |
| 96 | + actor_id=current_user.id, |
| 97 | + action="project.update", |
| 98 | + resource_type="project", |
| 99 | + resource_id=updated.id, |
| 100 | + details=diff, |
| 101 | + ) |
| 102 | + return updated |
0 commit comments