Advertisement
Azamatik

Untitled

Apr 15th, 2023 (edited)
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. enum Status {
  2.     DONE = "Done", IN_PROGRESS = "In progress"
  3. }
  4.  
  5. enum Priority {
  6.     HIGH = "high", LOW = "low"
  7. }
  8.  
  9. interface Task {
  10.     name: string,
  11.     status: Status,
  12.     priority: Priority
  13. }
  14.  
  15. class TaskRepository {
  16.     private tasks: Array<Task> = [];
  17.  
  18.     public getAllTasks(): Array<Task> {
  19.         return this.tasks.sort((t1, t2) => {
  20.             if (t1.status === Status.DONE && t2.status === Status.DONE) {
  21.                 return 0;
  22.             }
  23.             if (t1.status === Status.DONE && t2.status !== Status.DONE) {
  24.                 return 1;
  25.             }
  26.             return -1;
  27.         });
  28.     }
  29.  
  30.     public addTask(name: string): void {
  31.         this.tasks.push({name, status: Status.IN_PROGRESS, priority: Priority.HIGH});
  32.     }
  33.  
  34.     public changeTask(name: string, status: Status, priority: Priority): void {
  35.         const taskToChange: Array<Task> = this.tasks.filter(t => t.name === name);
  36.        
  37.         if (taskToChange.length < 1) {
  38.             return;
  39.         }
  40.  
  41.         taskToChange[0].status = status;
  42.         taskToChange[0].priority = priority;
  43.     }
  44.  
  45.     public deleteTaskByName(name: string): void {
  46.         const newTasks = this.tasks.filter(t => t.name !== name);
  47.         this.tasks = newTasks;
  48.     }
  49. }
  50.  
  51.  
  52. const taskRepo = new TaskRepository();
  53. taskRepo.addTask("create a post");
  54. console.log("Add create a post task:")
  55. console.log(taskRepo.getAllTasks());
  56.  
  57.  
  58. taskRepo.changeTask("create a post", Status.DONE, Priority.LOW);
  59. console.log("Modify create a post task:")
  60. console.log(taskRepo.getAllTasks());
  61.  
  62. taskRepo.addTask("have a walk");
  63. console.log("Add have a walk task:")
  64. console.log(taskRepo.getAllTasks());
  65.  
  66. taskRepo.deleteTaskByName("create a post")
  67. console.log("Delete create a post task:")
  68. console.log(taskRepo.getAllTasks());
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement