From e1151f8423c343acf32823cc84669d771f126846 Mon Sep 17 00:00:00 2001 From: Dan Date: Sat, 4 May 2024 20:19:17 -0400 Subject: [PATCH] Added commands to remove projects and tasks --- dolly/commands.py | 22 +++++++++++++++++++++- dolly/database.py | 11 +++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dolly/commands.py b/dolly/commands.py index 8c94604..8cbf09a 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, update_task, list_projects, list_tasks_for_project +from .database import add_project, get_project_id, get_project_name, add_task_to_project, update_task, list_projects, list_tasks_for_project, remove_task, remove_project from datetime import datetime import logging @@ -31,6 +31,16 @@ class ProjectCommands(app_commands.Group): except Exception as e: await interaction.response.send_message("Failed to retrieve projects.", ephemeral=True) logger.error(f"Error in list_projects: {e}") + + @app_commands.command(name="remove", description="Remove a specific project and all its tasks.") + async def remove_project_command(self, interaction: discord.Interaction, project_id: int): + try: + await remove_project(project_id) + await interaction.response.send_message(f"Project ID {project_id} and all associated tasks have been successfully removed.") + logger.info(f"Project ID {project_id} and all associated tasks removed successfully.") + except Exception as e: + await interaction.response.send_message("Failed to remove the project.", ephemeral=True) + logger.error(f"Error in remove_project_command: {e}") class TaskCommands(app_commands.Group): def __init__(self): @@ -87,6 +97,16 @@ class TaskCommands(app_commands.Group): 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}") + + @app_commands.command(name="remove", description="Remove a specific task.") + async def remove_task_command(self, interaction: discord.Interaction, task_id: int): + try: + await remove_task(task_id) + await interaction.response.send_message(f"Task ID {task_id} has been successfully removed.") + logger.info(f"Task ID {task_id} removed successfully.") + except Exception as e: + await interaction.response.send_message("Failed to remove the task.", ephemeral=True) + logger.error(f"Error in remove_task_command: {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 8b647b3..8102045 100644 --- a/dolly/database.py +++ b/dolly/database.py @@ -69,3 +69,14 @@ async def list_tasks_for_project(project_id): cursor = await db.execute("SELECT id, description, assignee, deadline, status, priority FROM tasks WHERE project_id = ?", (project_id,)) tasks = await cursor.fetchall() return tasks + +async def remove_task(task_id): + async with aiosqlite.connect(DATABASE) as db: + await db.execute("DELETE FROM tasks WHERE id = ?", (task_id,)) + await db.commit() + +async def remove_project(project_id): + async with aiosqlite.connect(DATABASE) as db: + await db.execute("DELETE FROM tasks WHERE project_id = ?", (project_id,)) # Remove all tasks under the project + await db.execute("DELETE FROM projects WHERE id = ?", (project_id,)) + await db.commit() \ No newline at end of file