81 lines
2.9 KiB
Python
81 lines
2.9 KiB
Python
from uuid import UUID
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.auth.dependencies import get_current_active_user
|
|
from app.db import get_db_session
|
|
from app.models.project import Project
|
|
from app.models.user import User
|
|
from app.schemas.project import ProjectCreate, ProjectRead, ProjectUpdate
|
|
|
|
router = APIRouter(tags=["projects"])
|
|
|
|
|
|
@router.post("/projects", response_model=ProjectRead, status_code=status.HTTP_201_CREATED)
|
|
async def create_project(
|
|
project_in: ProjectCreate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
session: AsyncSession = Depends(get_db_session),
|
|
) -> Project:
|
|
project = Project(**project_in.model_dump(), owner_id=current_user.id)
|
|
session.add(project)
|
|
await session.commit()
|
|
await session.refresh(project)
|
|
return project
|
|
|
|
|
|
@router.get("/projects", response_model=list[ProjectRead])
|
|
async def list_projects(
|
|
current_user: User = Depends(get_current_active_user),
|
|
session: AsyncSession = Depends(get_db_session),
|
|
) -> list[Project]:
|
|
result = await session.execute(
|
|
select(Project).where(Project.owner_id == current_user.id)
|
|
)
|
|
return list(result.scalars().all())
|
|
|
|
|
|
@router.get("/projects/{project_id}", response_model=ProjectRead)
|
|
async def get_project(
|
|
project_id: UUID,
|
|
current_user: User = Depends(get_current_active_user),
|
|
session: AsyncSession = Depends(get_db_session),
|
|
) -> Project:
|
|
project = await session.get(Project, project_id)
|
|
if not project or project.owner_id != current_user.id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
|
return project
|
|
|
|
|
|
@router.put("/projects/{project_id}", response_model=ProjectRead)
|
|
async def update_project(
|
|
project_id: UUID,
|
|
project_in: ProjectUpdate,
|
|
current_user: User = Depends(get_current_active_user),
|
|
session: AsyncSession = Depends(get_db_session),
|
|
) -> Project:
|
|
project = await session.get(Project, project_id)
|
|
if not project or project.owner_id != current_user.id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
|
update_data = project_in.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(project, field, value)
|
|
await session.commit()
|
|
await session.refresh(project)
|
|
return project
|
|
|
|
|
|
@router.delete("/projects/{project_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_project(
|
|
project_id: UUID,
|
|
current_user: User = Depends(get_current_active_user),
|
|
session: AsyncSession = Depends(get_db_session),
|
|
) -> None:
|
|
project = await session.get(Project, project_id)
|
|
if not project or project.owner_id != current_user.id:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Project not found")
|
|
await session.delete(project)
|
|
await session.commit()
|