diff --git a/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java b/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java index a39e36c..49886a7 100644 --- a/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java +++ b/todolist/src/main/java/com/scaler/todolist/controllers/TasksController.java @@ -1,6 +1,7 @@ package com.scaler.todolist.controllers; import com.scaler.todolist.models.Task; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @@ -38,6 +39,44 @@ ResponseEntity addNewTask(@RequestBody Task task) { * delete task no 5 (response with correct HTTP code) * if task 5 does not exist, send 404 */ + /* + *Assignment + 1. GET -> /tasks/3 + get task no 3 + 2.Patch -> task/3 + update due date or done status for task no 3 + send 404 error to client if task no 3 does not exist + 3. Delete -> tasks/5 + delete task no 5 (response code with correct http code) + if task 5 does not exist + */ + @GetMapping("/{id}") + ResponseEntity getTaskNumber(@PathVariable("id") int id){ + if(taskList.size() == 0 || id > taskList.size() ){//Add a length check + return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); + } + return ResponseEntity.ok(taskList.get(id)); + } + + @PatchMapping("/{id}") + ResponseEntity updateTask(@PathVariable("id") int id, @RequestBody Task task){ + if(taskList.size() == 0 || id > taskList.size() ){ + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + Task taskToUpdate = taskList.get(id); + taskToUpdate.setDone(true); + return ResponseEntity.status(200).body(taskToUpdate); + } + + @DeleteMapping("/{id}") + ResponseEntity deleteTask(@PathVariable("id") int id, @RequestBody Task task){ + if(taskList.size() == 0 || id > taskList.size() ){ + return new ResponseEntity("Task to delete not found",HttpStatus.NOT_FOUND); + } + Task taskToDelete = taskList.get(id); + taskList.remove(id); + return new ResponseEntity("Task deleted successful", HttpStatus.OK); + } }