From 8ef005cd3a996296d6ba8338ebcf2bb176801a1a Mon Sep 17 00:00:00 2001 From: TropiiDev Date: Mon, 28 Apr 2025 21:23:35 -0400 Subject: [PATCH] Delete tasks & edit tasks fixes v0.0.8 --- api.py | 65 +++++++++++++++++++++++++++++++++++++++++++++---------- tables.py | 5 +++++ 2 files changed, 58 insertions(+), 12 deletions(-) diff --git a/api.py b/api.py index cf1c646..9d85452 100644 --- a/api.py +++ b/api.py @@ -42,25 +42,41 @@ def create_user(user: UserCreate, session: SessionDep): session.refresh(db_user) return db_user +# generated using Claude 3.5 Sonnet @app.patch("/user/update-tasks", response_model=UserPublic) async def update_user_task(task: UpdateTask, session: SessionDep, current_user: User = Depends(get_current_user)): user_db = session.get(User, current_user.id) user_data = task.model_dump(exclude_unset=True) - new_task = user_data + + print(new_task['task_id']); + if user_db.tasks is None: user_db.tasks = [] - - # Filter out the task with matching title and keep all others - updated_tasks = [ - task for task in user_db.tasks - if task['title'] != new_task['title'] - ] - - # Add the new task to the filtered list - updated_tasks.append(new_task) - - # Update user_db tasks with the new list + + # Create a copy of current tasks + updated_tasks = user_db.tasks.copy() + + # Check if task_id exists in user_db tasks + existing_task = next( + (task for task in updated_tasks if task.get('task_id') == new_task.get('task_id')), + None + ) + + if existing_task: + # Overwrite the existing task with the new task + for i, task in enumerate(updated_tasks): + if task['task_id'] == new_task['task_id']: + updated_tasks[i] = new_task + break + else: + # Generate new task_id if not provided + if new_task.get('task_id') is None: + new_task['task_id'] = len(updated_tasks) + # Append the new task + updated_tasks.append(new_task) + + # Update user_db tasks with the modified list user_db.tasks = updated_tasks # Update the session @@ -70,6 +86,31 @@ async def update_user_task(task: UpdateTask, session: SessionDep, current_user: return user_db +@app.delete("/user/delete-task", response_model=UserPublic) +async def delete_user_task(task: DeleteTask, session: SessionDep, current_user: User = Depends(get_current_user)): + user_db = session.get(User, current_user.id) + user_data = task.model_dump(exclude_unset=True) + task_id = user_data['task_id'] + + if user_db.tasks is None: + return {"detail": "No tasks to delete"} + + # Create a copy of current tasks + updated_tasks = user_db.tasks.copy() + + # Remove the task with the specified task_id + updated_tasks = [task for task in updated_tasks if task.get('task_id') != task_id] + + # Update user_db tasks with the modified list + user_db.tasks = updated_tasks + + # Update the session + session.add(user_db) + session.commit() + session.refresh(user_db) + + return user_db + @app.patch("/user/update", response_model=UserPublic) async def update_user(user: UserUpdate, session: SessionDep, current_user: User = Depends(get_current_user)): user_db = session.get(User, current_user.id) diff --git a/tables.py b/tables.py index 4e3e6e7..f7344e7 100644 --- a/tables.py +++ b/tables.py @@ -16,11 +16,16 @@ class Task(BaseModel): title: str description: str | None = None is_completed: bool | None = Field(default=False) + task_id: int | None = None class UpdateTask(BaseModel): title: str | None = None description: str | None = None is_completed: bool | None = Field(default=None) + task_id: int | None = None + +class DeleteTask(BaseModel): + task_id: int # User models class UserBase(SQLModel):