Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.*;

Expand Down Expand Up @@ -38,6 +39,44 @@ ResponseEntity<Task> 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<Task> 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<Task> 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);
}


}