diff --git a/api/routers/v1/timelog.py b/api/routers/v1/timelog.py index 8ec17ea76..99325c6d0 100644 --- a/api/routers/v1/timelog.py +++ b/api/routers/v1/timelog.py @@ -200,6 +200,17 @@ async def update_task( return result +@router.delete("/tasks/{task_id}", status_code=204) +async def delete_task(task_id: int, current_user=Depends(get_current_user), db: Session = Depends(get_db)): + task = TaskService(db).get_task(task_id) + if not task: + raise HTTPException(status_code=404, detail=f"Task with id {task_id} not found") + if current_user.id != task.user_id: + raise HTTPException(status_code=403, detail="You are not authorized to delete tasks for this user") + TaskService(db).delete_task(task_id) + return + + def validate_task(task_to_validate: TaskSchema, db: Session): validated = ValidatedObject(is_valid=False, message="") user_can_create_tasks = ConfigService(db).can_user_edit_task(task_to_validate.date) diff --git a/api/services/timelog.py b/api/services/timelog.py index a4ca32f26..a7223e147 100644 --- a/api/services/timelog.py +++ b/api/services/timelog.py @@ -106,6 +106,11 @@ def update_task(self, existing_task: Task, task_updates: TaskUpdate) -> Task: self.db.refresh(existing_task) return existing_task + def delete_task(self, task_id: int): + task = self.get_task(task_id) + self.db.delete(task) + self.db.commit() + def check_task_for_overlap(self, task: Task) -> ValidatedObject: validated_task = ValidatedObject(is_valid=True, message="") user_tasks_for_day = self.db.query(Task).where(Task.user_id == task.user_id, Task.date == task.date)