diff --git a/src/main/java/com/scaler/springboot1/task/TaskController.java b/src/main/java/com/scaler/springboot1/task/TaskController.java index 2e6d209..ee81776 100644 --- a/src/main/java/com/scaler/springboot1/task/TaskController.java +++ b/src/main/java/com/scaler/springboot1/task/TaskController.java @@ -1,9 +1,13 @@ package com.scaler.springboot1.task; +import com.scaler.springboot1.task.dtos.CreateTaskDTO; +import com.scaler.springboot1.task.dtos.TaskDTO; +import com.scaler.springboot1.task.dtos.UpdateTaskDTO; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import java.util.Date; import java.util.List; @RestController @@ -15,12 +19,12 @@ public TaskController(TasksService tasksService) { } @GetMapping("") - ResponseEntity> getAllTasks() { + ResponseEntity> getAllTasks() { var tasks = tasksService.getAllTasks(); return ResponseEntity.ok(tasks); } @PostMapping("") - ResponseEntity createTask(@RequestBody Task task) { + ResponseEntity createTask(@RequestBody Task task) { var createdTask = tasksService.createTask(task); return ResponseEntity.ok(createdTask); } @@ -31,8 +35,22 @@ ResponseEntity getTaskById(@PathVariable("id") Integer id) { } //Todo 1: implement Update Task - PATCh + @PatchMapping("/update/{id}") + ResponseEntity updateById(@PathVariable("id") Integer id, @RequestBody Task task){ + UpdateTaskDTO currentTask = tasksService.updateTask(id, task); + return ResponseEntity.ok(currentTask); + } + // Todo 2: implement Delete Task - DELETE + @DeleteMapping("/delete/{id}") + ResponseEntity deleteById(@PathVariable("id") Integer id){ + String stringResp = tasksService.deleteTask(id); + + return ResponseEntity.ok(stringResp); + } + // Todo5: create a ResponseBodyDTO - only return name, dueDate, completed + // Done // Todo3 - handle expection for IllegalArgumentException ( due date, name) // Todo4 - in error responses, also send the error message in the response body diff --git a/src/main/java/com/scaler/springboot1/task/TasksService.java b/src/main/java/com/scaler/springboot1/task/TasksService.java index 2ccb91f..074dfcf 100644 --- a/src/main/java/com/scaler/springboot1/task/TasksService.java +++ b/src/main/java/com/scaler/springboot1/task/TasksService.java @@ -1,18 +1,23 @@ package com.scaler.springboot1.task; +import com.scaler.springboot1.task.dtos.CreateTaskDTO; +import com.scaler.springboot1.task.dtos.TaskDTO; +import com.scaler.springboot1.task.dtos.UpdateTaskDTO; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; @Service public class TasksService { List taskList = new ArrayList<>(); private int nextTaskId = 0; - public List getAllTasks() { - return taskList; + public List getAllTasks() { + List taskDTOs = this.taskList.stream().map(t->this.taskToDTO(t)).collect(Collectors.toList()); + return taskDTOs; } public Task getTaskById(Integer id) { @@ -29,29 +34,39 @@ public Task getTaskById(Integer id) { throw new TaskNotFoundException(id); } - public Task createTask(Task task) { + public CreateTaskDTO createTask(Task task) { task.setId(nextTaskId++); taskList.add(task); - return task; + CreateTaskDTO createTaskDTO = this.taskToCreateDTO(task); + return createTaskDTO; } - public Task updateTask(Integer id, Date dueDate, Boolean completed) { - Task task = getTaskById(id); - if (dueDate != null) { - task.setDueDate(dueDate); + public UpdateTaskDTO updateTask(Integer id, Task task) { + Task currentTask = getTaskById(id); + if(task.name != null){ + currentTask.name = task.name; } - - if (completed != null) { - task.setCompleted(completed); + if(task.dueDate != null){ + currentTask.dueDate = task.dueDate; } - - return task; + if(task.completed != null){ + currentTask.completed = task.completed; + } + UpdateTaskDTO updateTaskDTO = this.taskToUpdateDTO(task); + return updateTaskDTO; } - public void deleteTask(Integer id) { - Task task = getTaskById(id); - taskList.remove(task); + public String deleteTask(Integer id) { + for (Task task : taskList) { + if (task.getId().equals(id)) { + Task taskToDelete = getTaskById(id); + taskList.remove(taskToDelete); + return "Task is deleted"; + } + } + + throw new TaskNotFoundException(id); } /* @@ -64,4 +79,24 @@ public TaskNotFoundException(Integer id) { } } + public CreateTaskDTO taskToCreateDTO(Task task){ + CreateTaskDTO createTaskDTO = new CreateTaskDTO(); + createTaskDTO.setName(task.getName()); + createTaskDTO.setDueDate(task.getDueDate()) ; + return createTaskDTO; + } + + public TaskDTO taskToDTO(Task task){ + TaskDTO taskDTO = new TaskDTO(); + taskDTO.setName(task.getName()); + taskDTO.setDueDate(task.getDueDate()) ; + return taskDTO; + } + + public UpdateTaskDTO taskToUpdateDTO(Task task){ + UpdateTaskDTO updateTaskDTO = new UpdateTaskDTO(); + updateTaskDTO.setDueDate(task.getDueDate()); + updateTaskDTO.setCompleted(task.getCompleted()); + return updateTaskDTO; + } } \ No newline at end of file diff --git a/src/main/java/com/scaler/springboot1/task/dtos/CreateTaskDTO.java b/src/main/java/com/scaler/springboot1/task/dtos/CreateTaskDTO.java index 97ce6a0..6645879 100644 --- a/src/main/java/com/scaler/springboot1/task/dtos/CreateTaskDTO.java +++ b/src/main/java/com/scaler/springboot1/task/dtos/CreateTaskDTO.java @@ -2,9 +2,11 @@ import lombok.*; +import java.util.Date; + @Data public class CreateTaskDTO { String name; - String dueDate; + Date dueDate; } diff --git a/src/main/java/com/scaler/springboot1/task/dtos/TaskDTO.java b/src/main/java/com/scaler/springboot1/task/dtos/TaskDTO.java new file mode 100644 index 0000000..fa2c01f --- /dev/null +++ b/src/main/java/com/scaler/springboot1/task/dtos/TaskDTO.java @@ -0,0 +1,13 @@ +package com.scaler.springboot1.task.dtos; + +import lombok.Getter; +import lombok.Setter; + +import java.util.Date; + +@Setter +@Getter +public class TaskDTO { + String name; + Date dueDate; +} diff --git a/src/main/java/com/scaler/springboot1/task/dtos/UpdateTaskDTO.java b/src/main/java/com/scaler/springboot1/task/dtos/UpdateTaskDTO.java index f9b6e33..689a3b3 100644 --- a/src/main/java/com/scaler/springboot1/task/dtos/UpdateTaskDTO.java +++ b/src/main/java/com/scaler/springboot1/task/dtos/UpdateTaskDTO.java @@ -2,8 +2,12 @@ import lombok.*; +import java.util.Date; + @Data +@Getter +@Setter public class UpdateTaskDTO{ - String dueDate; + Date dueDate; Boolean completed; }