Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add DELETE endpoint for tasks #703

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions api/routers/v1/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions api/services/timelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down