|
| 1 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 2 | +from sqlalchemy.orm import Session |
| 3 | +from typing import List |
| 4 | + |
| 5 | +from src.utils.security import get_current_user |
| 6 | +from src.models.user_model import User |
| 7 | +from src.models.project_model import Project |
| 8 | +from src.utils.utils import get_db |
| 9 | +from src.schemas.projects.projects_schema import ProjectCreate, ProjectResponse |
| 10 | + |
| 11 | +project_router = APIRouter(prefix="/projects", tags=["projects"]) |
| 12 | + |
| 13 | + |
| 14 | +@project_router.post( |
| 15 | + "/", response_model=ProjectResponse, status_code=status.HTTP_201_CREATED |
| 16 | +) |
| 17 | +def create_project( |
| 18 | + project: ProjectCreate, |
| 19 | + db: Session = Depends(get_db), |
| 20 | + current_user: User = Depends(get_current_user), |
| 21 | +): |
| 22 | + new_project = Project( |
| 23 | + user_id=current_user.id, title=project.title, description=project.description |
| 24 | + ) |
| 25 | + db.add(new_project) |
| 26 | + db.commit() |
| 27 | + db.refresh(new_project) |
| 28 | + return new_project |
| 29 | + |
| 30 | + |
| 31 | +@project_router.get("/", response_model=List[ProjectResponse]) |
| 32 | +def list_projects( |
| 33 | + db: Session = Depends(get_db), current_user: User = Depends(get_current_user) |
| 34 | +): |
| 35 | + return db.query(Project).filter(Project.user_id == current_user.id).all() |
| 36 | + |
| 37 | + |
| 38 | +@project_router.get("/{project_id}", response_model=ProjectResponse) |
| 39 | +def get_project( |
| 40 | + project_id: int, |
| 41 | + db: Session = Depends(get_db), |
| 42 | + current_user: User = Depends(get_current_user), |
| 43 | +): |
| 44 | + project = ( |
| 45 | + db.query(Project) |
| 46 | + .filter(Project.id == project_id, Project.user_id == current_user.id) |
| 47 | + .first() |
| 48 | + ) |
| 49 | + if not project: |
| 50 | + raise HTTPException(status_code=404, detail="Projeto não encontrado.") |
| 51 | + return project |
| 52 | + |
| 53 | + |
| 54 | +@project_router.put("/{project_id}", response_model=ProjectResponse) |
| 55 | +def update_project( |
| 56 | + project_id: int, |
| 57 | + updated: ProjectCreate, |
| 58 | + db: Session = Depends(get_db), |
| 59 | + current_user: User = Depends(get_current_user), |
| 60 | +): |
| 61 | + project = ( |
| 62 | + db.query(Project) |
| 63 | + .filter(Project.id == project_id, Project.user_id == current_user.id) |
| 64 | + .first() |
| 65 | + ) |
| 66 | + if not project: |
| 67 | + raise HTTPException(status_code=404, detail="Projeto não encontrado.") |
| 68 | + project.title = updated.title |
| 69 | + project.description = updated.description |
| 70 | + db.commit() |
| 71 | + db.refresh(project) |
| 72 | + return project |
| 73 | + |
| 74 | + |
| 75 | +@project_router.delete("/{project_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 76 | +def delete_project( |
| 77 | + project_id: int, |
| 78 | + db: Session = Depends(get_db), |
| 79 | + current_user: User = Depends(get_current_user), |
| 80 | +): |
| 81 | + project = ( |
| 82 | + db.query(Project) |
| 83 | + .filter(Project.id == project_id, Project.user_id == current_user.id) |
| 84 | + .first() |
| 85 | + ) |
| 86 | + if not project: |
| 87 | + raise HTTPException(status_code=404, detail="Projeto não encontrado.") |
| 88 | + db.delete(project) |
| 89 | + db.commit() |
0 commit comments