cc-dashboard/src/routers/projects.py

41 lines
1.4 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from src.auth import CurrentUser
from src.database import get_db
from src.models import Project
from src.schemas import ProjectOut
router = APIRouter(prefix="/api/projects", tags=["projects"])
@router.get("", response_model=list[ProjectOut])
async def list_projects(user: CurrentUser, db: AsyncSession = Depends(get_db)):
result = await db.execute(
select(Project).where(Project.user_id == user.id).order_by(Project.display_name)
)
return result.scalars().all()
@router.patch("/{project_id}", response_model=ProjectOut)
async def update_project(
project_id: str,
body: dict,
user: CurrentUser,
db: AsyncSession = Depends(get_db),
):
project = await db.get(Project, project_id)
if not project or project.user_id != user.id:
raise HTTPException(status_code=404, detail="Project not found")
if "display_name" in body:
project.display_name = str(body["display_name"])[:255]
if "client" in body:
project.client = str(body["client"])[:255]
if "job_number" in body:
project.job_number = str(body["job_number"])[:100]
if "repo_url" in body:
project.repo_url = str(body["repo_url"])[:500]
await db.commit()
await db.refresh(project)
return project