|
| 1 | +from typing import List |
| 2 | +from sqlalchemy.orm import Session |
| 3 | +from src.utils.utils import get_db |
| 4 | +from src.models.user_model import User |
| 5 | +from src.models.task_model import Task |
| 6 | +from src.utils.security import get_current_user |
| 7 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 8 | +from src.schemas.tasks.tasks_schema import TaskCreate, TaskResponse |
| 9 | + |
| 10 | +task_router = APIRouter(prefix="/tasks", tags=["tasks"]) |
| 11 | + |
| 12 | + |
| 13 | +@task_router.post("/", response_model=TaskResponse, status_code=status.HTTP_201_CREATED) |
| 14 | +def create_task( |
| 15 | + task: TaskCreate, |
| 16 | + db: Session = Depends(get_db), |
| 17 | + current_user: User = Depends(get_current_user), |
| 18 | +): |
| 19 | + """ |
| 20 | + Create a new task for the authenticated user. |
| 21 | + """ |
| 22 | + new_task = Task(**task.model_dump(), user_id=current_user.id) |
| 23 | + db.add(new_task) |
| 24 | + db.commit() |
| 25 | + db.refresh(new_task) |
| 26 | + return new_task |
| 27 | + |
| 28 | + |
| 29 | +@task_router.get("/", response_model=List[TaskResponse]) |
| 30 | +def list_tasks( |
| 31 | + db: Session = Depends(get_db), current_user: User = Depends(get_current_user) |
| 32 | +): |
| 33 | + """ |
| 34 | + List all tasks for the authenticated user. |
| 35 | + """ |
| 36 | + return db.query(Task).filter(Task.user_id == current_user.id).all() |
| 37 | + |
| 38 | + |
| 39 | +@task_router.get("/{task_id}", response_model=TaskResponse) |
| 40 | +def get_task( |
| 41 | + task_id: int, |
| 42 | + db: Session = Depends(get_db), |
| 43 | + current_user: User = Depends(get_current_user), |
| 44 | +): |
| 45 | + """ |
| 46 | + Get a specific task by its ID for the authenticated user. |
| 47 | + """ |
| 48 | + task = ( |
| 49 | + db.query(Task) |
| 50 | + .filter(Task.id == task_id, Task.user_id == current_user.id) |
| 51 | + .first() |
| 52 | + ) |
| 53 | + if not task: |
| 54 | + raise HTTPException( |
| 55 | + status_code=status.HTTP_404_NOT_FOUND, |
| 56 | + detail=f"Tarefa com id {task_id} não encontrada.", |
| 57 | + ) |
| 58 | + return task |
| 59 | + |
| 60 | + |
| 61 | +@task_router.put("/{task_id}", response_model=TaskResponse) |
| 62 | +def update_task( |
| 63 | + task_id: int, |
| 64 | + task_update: TaskCreate, |
| 65 | + db: Session = Depends(get_db), |
| 66 | + current_user: User = Depends(get_current_user), |
| 67 | +): |
| 68 | + """ |
| 69 | + Update a specific task for the authenticated user. |
| 70 | + """ |
| 71 | + db_task = ( |
| 72 | + db.query(Task) |
| 73 | + .filter(Task.id == task_id, Task.user_id == current_user.id) |
| 74 | + .first() |
| 75 | + ) |
| 76 | + if not db_task: |
| 77 | + raise HTTPException( |
| 78 | + status_code=status.HTTP_404_NOT_FOUND, |
| 79 | + detail=f"Tarefa com id {task_id} não encontrada.", |
| 80 | + ) |
| 81 | + |
| 82 | + for key, value in task_update.model_dump().items(): |
| 83 | + setattr(db_task, key, value) |
| 84 | + |
| 85 | + db.commit() |
| 86 | + db.refresh(db_task) |
| 87 | + return db_task |
| 88 | + |
| 89 | + |
| 90 | +@task_router.delete("/{task_id}", status_code=status.HTTP_204_NO_CONTENT) |
| 91 | +def delete_task( |
| 92 | + task_id: int, |
| 93 | + db: Session = Depends(get_db), |
| 94 | + current_user: User = Depends(get_current_user), |
| 95 | +): |
| 96 | + """ |
| 97 | + Delete a specific task for the authenticated user. |
| 98 | + """ |
| 99 | + task = ( |
| 100 | + db.query(Task) |
| 101 | + .filter(Task.id == task_id, Task.user_id == current_user.id) |
| 102 | + .first() |
| 103 | + ) |
| 104 | + if not task: |
| 105 | + raise HTTPException( |
| 106 | + status_code=status.HTTP_404_NOT_FOUND, |
| 107 | + detail=f"Tarefa com id {task_id} não encontrada.", |
| 108 | + ) |
| 109 | + db.delete(task) |
| 110 | + db.commit() |
0 commit comments