From e8ce8575e84391ecab068ad794926fff9194dcd9 Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 4 May 2024 17:06:30 -0400 Subject: [PATCH] Added the list commands for both tasks and projects. --- dolly/commands.py | 43 ++++++++++++++++++++++++++++++++++++++++++- dolly/database.py | 14 +++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/dolly/commands.py b/dolly/commands.py index 501a08e..8d7d7fd 100644 --- a/dolly/commands.py +++ b/dolly/commands.py @@ -1,6 +1,6 @@ import discord from discord import app_commands -from .database import add_project, get_project_id, add_task_to_project +from .database import add_project, get_project_id, add_task_to_project, update_task, list_projects, list_tasks_for_project from datetime import datetime import logging @@ -18,6 +18,19 @@ class ProjectCommands(app_commands.Group): except Exception as e: await interaction.response.send_message("An error occurred while creating the project.", ephemeral=True) logger.error(f"Error in create_project: {e}") + + @app_commands.command(name="list", description="List all projects.") + async def list_projects(self, interaction: discord.Interaction): + try: + projects = await list_projects() + if projects: + message = "\n".join([f"ID: {id}, Name: {name}, Description: {description}" for id, name, description in projects]) + await interaction.response.send_message(f"Projects:\n{message}") + else: + await interaction.response.send_message("No projects found.") + except Exception as e: + await interaction.response.send_message("Failed to retrieve projects.", ephemeral=True) + logger.error(f"Error in list_projects: {e}") class TaskCommands(app_commands.Group): def __init__(self): @@ -41,6 +54,34 @@ class TaskCommands(app_commands.Group): except Exception as e: await interaction.response.send_message("An error occurred while adding the task.", ephemeral=True) logger.error(f"Unexpected error in add_task: {e}") + + @app_commands.command(name="update", description="Update an existing task.") + async def update_task_command(self, interaction: discord.Interaction, task_id: int, description: str, assignee: str, deadline: str, status: str, priority: str): + try: + datetime.strptime(deadline, "%m/%d/%Y") # Validate date format + await update_task(task_id, description, assignee, deadline, status, priority) + await interaction.response.send_message(f"Task ID {task_id} updated successfully.") + logger.info(f"Task ID {task_id} updated: {description}") + except ValueError: + await interaction.response.send_message("Invalid date format. Please use MM/DD/YYYY format.", ephemeral=True) + logger.error(f"Invalid date format provided by user for task update: {deadline}") + except Exception as e: + await interaction.response.send_message("An error occurred while updating the task.", ephemeral=True) + logger.error(f"Unexpected error in update_task_command: {e}") + + @app_commands.command(name="list", description="List tasks for a project.") + async def list_tasks(self, interaction: discord.Interaction, project_id: int): + try: + tasks = await list_tasks_for_project(project_id) + if tasks: + message = "\n".join([f"ID: {id}, Description: {description}, Assignee: {assignee}, Deadline: {deadline}, Status: {status}, Priority: {priority}" + for id, description, assignee, deadline, status, priority in tasks]) + await interaction.response.send_message(f"Tasks for Project ID {project_id}:\n{message}") + else: + await interaction.response.send_message(f"No tasks found for project ID {project_id}.") + except Exception as e: + await interaction.response.send_message(f"Failed to retrieve tasks for project ID {project_id}.", ephemeral=True) + logger.error(f"Error in list_tasks: {e}") class DollyTracker(app_commands.Group, name="dolly", description="Dolly the Project Tracker."): def __init__(self): diff --git a/dolly/database.py b/dolly/database.py index 10dbb24..ff4609c 100644 --- a/dolly/database.py +++ b/dolly/database.py @@ -50,4 +50,16 @@ async def update_task(task_id, description, assignee, deadline, status, priority deadline_date = datetime.strptime(deadline, "%m/%d/%Y").date() await db.execute("UPDATE tasks SET description=?, assignee=?, deadline=?, status=?, priority=? WHERE id=?", (description, assignee, deadline_date, status, priority, task_id)) - await db.commit() \ No newline at end of file + await db.commit() + +async def list_projects(): + async with aiosqlite.connect(DATABASE) as db: + cursor = await db.execute("SELECT id, name, description FROM projects") + projects = await cursor.fetchall() + return projects + +async def list_tasks_for_project(project_id): + async with aiosqlite.connect(DATABASE) as db: + cursor = await db.execute("SELECT id, description, assignee, deadline, status, priority FROM tasks WHERE project_id = ?", (project_id,)) + tasks = await cursor.fetchall() + return tasks