anagreg

add update task

Oct 10th, 2023 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ADD
  2. class AddTaskCommand extends Command {
  3.  
  4.   async execute(name, status) {
  5.     await wait(500);
  6.  
  7.     try {
  8.       const task = new Task(name, status);
  9.       this.taskRegistry.addTask(task);
  10.  
  11.       return `Task added successfully.`;
  12.     } catch (e) {
  13.       return `Problem with adding the task: ${e.message}`;
  14.     }
  15.   }
  16. }
  17.  
  18. //UPDATE
  19.  
  20. class UpdateTaskCommand extends Command {
  21.   async execute(name, status) {
  22.     await wait(500);
  23.  
  24.     try {
  25.       if (!this.taskRegistry.tasks.has(name)) {
  26.         return `Task with name ${name} doesn't exist!`;
  27.      }
  28.  
  29.      const task = this.taskRegistry.tasks.get(name);
  30.  
  31.      if (!taskStatus.hasOwnProperty(status)) {
  32.        throw new Error(`Invalid status`);
  33.      }
  34.  
  35.      task.status = status;
  36.  
  37.      return `Task updated successfully!`;
  38.    } catch (e) {
  39.      return `Problem with updating the task: ${e.message}`;
  40.    }
  41.  }
  42. }
  43.  
  44. // LIST
  45.  
  46. class ListTasksCommand extends Command {
  47.  async execute(name = '') {
  48.    await wait(500);
  49.  
  50.    try {
  51.      let matching;
  52.      if (name === '') {
  53.        matching = Array.from(this.taskRegistry.tasks.values);
  54.      } else {
  55.        matching = Array.from(this.taskRegistry.tasks.values()).filter((task) =>
  56.          task.name.includes(name));
  57.      }
  58.  
  59.      if (matching.length === 0) {
  60.        return 'No tasks matching search criteria';
  61.      }
  62.  
  63.      const taskList = matching.map((task) => `Task: ${task.name} (${task.status})`).join('\n');
  64.  
  65.      return taskList;
  66.    } catch (e) {
  67.      return `Problem listing the tasks!`;
  68.    }
  69.  }
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment