Delete tasks & edit tasks fixes v0.0.8

This commit is contained in:
TropiiDev 2025-04-28 21:23:35 -04:00
parent 8b787309d8
commit 8ef005cd3a
2 changed files with 58 additions and 12 deletions

65
api.py
View File

@ -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)

View File

@ -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):