Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ADD
- class AddTaskCommand extends Command {
- async execute(name, status) {
- await wait(500);
- try {
- const task = new Task(name, status);
- this.taskRegistry.addTask(task);
- return `Task added successfully.`;
- } catch (e) {
- return `Problem with adding the task: ${e.message}`;
- }
- }
- }
- //UPDATE
- class UpdateTaskCommand extends Command {
- async execute(name, status) {
- await wait(500);
- try {
- if (!this.taskRegistry.tasks.has(name)) {
- return `Task with name ${name} doesn't exist!`;
- }
- const task = this.taskRegistry.tasks.get(name);
- if (!taskStatus.hasOwnProperty(status)) {
- throw new Error(`Invalid status`);
- }
- task.status = status;
- return `Task updated successfully!`;
- } catch (e) {
- return `Problem with updating the task: ${e.message}`;
- }
- }
- }
- // LIST
- class ListTasksCommand extends Command {
- async execute(name = '') {
- await wait(500);
- try {
- let matching;
- if (name === '') {
- matching = Array.from(this.taskRegistry.tasks.values);
- } else {
- matching = Array.from(this.taskRegistry.tasks.values()).filter((task) =>
- task.name.includes(name));
- }
- if (matching.length === 0) {
- return 'No tasks matching search criteria';
- }
- const taskList = matching.map((task) => `Task: ${task.name} (${task.status})`).join('\n');
- return taskList;
- } catch (e) {
- return `Problem listing the tasks!`;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment