Added the list commands for both tasks and projects.

This commit is contained in:
Dan
2024-05-04 17:06:30 -04:00
parent 4599f49e84
commit e8ce8575e8
2 changed files with 55 additions and 2 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
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):

View File

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