From 662d16c2a66d8f0eaa834d7afe69feeb9bbd2da4 Mon Sep 17 00:00:00 2001 From: Hari_Aivar Date: Thu, 13 Aug 2026 19:12:25 +0530 Subject: [PATCH] Fix task bugs and add search filter --- app/service.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/app/service.py b/app/service.py index 8022b93..f036f41 100644 --- a/app/service.py +++ b/app/service.py @@ -12,13 +12,18 @@ def list_tasks(status: str | None = None, q: str | None = None) -> list[dict[str filtered: list[dict[str, Any]] = [] for task in tasks: - # Instructor note: intentional bug for the lab. - # This uses the literal string "status" instead of the query parameter value. - if status and task["status"] != "status": + # Filter by status when provided + if status and task["status"] != status: continue - # Instructor note: partial feature for the lab. - # The route already accepts `q`, but search is not implemented yet. + # Filter by search query when provided (case-insensitive, matches title or description) + if q: + query_lower = q.lower() + title_match = query_lower in task.get("title", "").lower() + description_match = query_lower in task.get("description", "").lower() + if not (title_match or description_match): + continue + filtered.append(task) return filtered @@ -51,14 +56,11 @@ def complete_task(task_id: int) -> dict[str, Any] | None: """Mark a task as completed.""" tasks = load_tasks() - for task in tasks: + for i, task in enumerate(tasks): if task["id"] == task_id: - updated_task = dict(task) - updated_task["status"] = "done" - updated_task["completed_at"] = datetime.now(timezone.utc).isoformat() - - # Instructor note: intentional bug for the lab. - # The updated task is returned, but the stored list is never updated or saved. - return updated_task + task["status"] = "done" + task["completed_at"] = datetime.now(timezone.utc).isoformat() + save_tasks(tasks) + return task return None