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

implementation of update task, delete task, task DTO #4

Open
wants to merge 1 commit 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
22 changes: 20 additions & 2 deletions src/main/java/com/scaler/springboot1/task/TaskController.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,12 +19,12 @@ public TaskController(TasksService tasksService) {
}

@GetMapping("")
ResponseEntity<List<Task>> getAllTasks() {
ResponseEntity<List<TaskDTO>> getAllTasks() {
var tasks = tasksService.getAllTasks();
return ResponseEntity.ok(tasks);
}
@PostMapping("")
ResponseEntity<Task> createTask(@RequestBody Task task) {
ResponseEntity<CreateTaskDTO> createTask(@RequestBody Task task) {
var createdTask = tasksService.createTask(task);
return ResponseEntity.ok(createdTask);
}
Expand All @@ -31,8 +35,22 @@ ResponseEntity<Task> getTaskById(@PathVariable("id") Integer id) {
}

//Todo 1: implement Update Task - PATCh
@PatchMapping("/update/{id}")
ResponseEntity<UpdateTaskDTO> 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<String> 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
Expand Down
67 changes: 51 additions & 16 deletions src/main/java/com/scaler/springboot1/task/TasksService.java
Original file line number Diff line number Diff line change
@@ -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<Task> taskList = new ArrayList<>();
private int nextTaskId = 0;

public List<Task> getAllTasks() {
return taskList;
public List<TaskDTO> getAllTasks() {
List<TaskDTO> taskDTOs = this.taskList.stream().map(t->this.taskToDTO(t)).collect(Collectors.toList());
return taskDTOs;
}

public Task getTaskById(Integer id) {
Expand All @@ -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);
}

/*
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import lombok.*;

import java.util.Date;


@Data
public class CreateTaskDTO {
String name;
String dueDate;
Date dueDate;
}
13 changes: 13 additions & 0 deletions src/main/java/com/scaler/springboot1/task/dtos/TaskDTO.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import lombok.*;

import java.util.Date;

@Data
@Getter
@Setter
public class UpdateTaskDTO{
String dueDate;
Date dueDate;
Boolean completed;
}