Added commands to remove projects and tasks

This commit is contained in:
Dan
2024-05-04 20:19:17 -04:00
parent 80becf1961
commit e1151f8423
2 changed files with 32 additions and 1 deletions

View File

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

View File

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