Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update task, delete task #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 11 additions & 1 deletion src/main/java/com/scaler/springboot1/task/TaskController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scaler.springboot1.task;


import com.scaler.springboot1.task.dtos.TaskResponseDTO;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,7 +21,7 @@ ResponseEntity<List<Task>> getAllTasks() {
return ResponseEntity.ok(tasks);
}
@PostMapping("")
ResponseEntity<Task> createTask(@RequestBody Task task) {
ResponseEntity<TaskResponseDTO> createTask(@RequestBody Task task) {
var createdTask = tasksService.createTask(task);
return ResponseEntity.ok(createdTask);
}
Expand All @@ -31,7 +32,16 @@ ResponseEntity<Task> getTaskById(@PathVariable("id") Integer id) {
}

//Todo 1: implement Update Task - PATCh
@PatchMapping("/update/{id}")
ResponseEntity<TaskResponseDTO> updateTask(@PathVariable Integer id, @RequestBody Task task){
var task1 = tasksService.updateTask(id, task.getDueDate(), task.getCompleted());
return ResponseEntity.ok(task1);
}
// Todo 2: implement Delete Task - DELETE
@DeleteMapping("/{id}")
ResponseEntity<Task> deleteTaskById(@PathVariable("id") Integer id){
return ResponseEntity.ok(tasksService.deleteTask(id));
}
// Todo5: create a ResponseBodyDTO - only return name, dueDate, completed

// Todo3 - handle expection for IllegalArgumentException ( due date, name)
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/scaler/springboot1/task/TasksService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.scaler.springboot1.task;

import com.scaler.springboot1.task.dtos.TaskResponseDTO;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand Down Expand Up @@ -29,14 +30,14 @@ public Task getTaskById(Integer id) {
throw new TaskNotFoundException(id);
}

public Task createTask(Task task) {
public TaskResponseDTO createTask(Task task) {
task.setId(nextTaskId++);
taskList.add(task);
return task;
return new TaskResponseDTO(task.getName(), task.getDueDate(), task.getCompleted());
}


public Task updateTask(Integer id, Date dueDate, Boolean completed) {
public TaskResponseDTO updateTask(Integer id, Date dueDate, Boolean completed) {
Task task = getTaskById(id);
if (dueDate != null) {
task.setDueDate(dueDate);
Expand All @@ -46,12 +47,13 @@ public Task updateTask(Integer id, Date dueDate, Boolean completed) {
task.setCompleted(completed);
}

return task;
return new TaskResponseDTO(task.getName(), dueDate, completed);
}

public void deleteTask(Integer id) {
public Task deleteTask(Integer id) {
Task task = getTaskById(id);
taskList.remove(task);
return task;
}

/*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.scaler.springboot1.task.dtos;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.util.Date;

@Getter
@Setter
@AllArgsConstructor
public class TaskResponseDTO {
String name;
Date dueDate;
Boolean completed;
}