From c2d287a656dc730d068f7255044ed7b38bef77e8 Mon Sep 17 00:00:00 2001 From: Jan Tymel <410388@mail.muni.cz> Date: Sat, 6 Feb 2021 10:56:15 +0100 Subject: [PATCH] Move task ID to URI path instead of body when task is updated Related to #3 --- .../example/demo/controller/TasksController.java | 6 ++++-- .../java/com/example/demo/dto/TaskUpdateDto.java | 16 +--------------- .../com/example/demo/service/TaskService.java | 3 ++- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/example/demo/controller/TasksController.java b/src/main/java/com/example/demo/controller/TasksController.java index dd556456..6700e5f4 100644 --- a/src/main/java/com/example/demo/controller/TasksController.java +++ b/src/main/java/com/example/demo/controller/TasksController.java @@ -122,16 +122,18 @@ public class TasksController { @ApiResponse(code = 200, message = "Task updated"), @ApiResponse(code = 500, message = "Unexpected application error") }) - @PutMapping + @PutMapping(path = "/{taskId}") public ResponseEntity<TaskDto> updateTask( @ApiParam(value = "Training definition ID", required = true) @PathVariable(name = "definitionId") Long definitionId, @ApiParam(value = "Game phase ID", required = true) @PathVariable(name = "phaseId") Long phaseId, + @ApiParam(value = "Task ID", required = true) + @PathVariable(name = "taskId") Long taskId, @ApiParam(value = "Task to be updated") @RequestBody @Valid TaskUpdateDto taskUpdateDto) { - TaskDto updatedTask = taskService.updateTask(definitionId, phaseId, taskUpdateDto); + TaskDto updatedTask = taskService.updateTask(definitionId, phaseId, taskId, taskUpdateDto); return new ResponseEntity<>(updatedTask, HttpStatus.OK); } diff --git a/src/main/java/com/example/demo/dto/TaskUpdateDto.java b/src/main/java/com/example/demo/dto/TaskUpdateDto.java index fb4c052d..94690d57 100644 --- a/src/main/java/com/example/demo/dto/TaskUpdateDto.java +++ b/src/main/java/com/example/demo/dto/TaskUpdateDto.java @@ -8,10 +8,6 @@ import javax.validation.constraints.PositiveOrZero; public class TaskUpdateDto { - @ApiModelProperty(value = "Task ID", required = true, example = "1") - @NotNull(message = "Task ID must be specified") - private Long id; - @ApiModelProperty(value = "Short description of task", required = true, example = "Task title") @NotEmpty(message = "Task title must not be blank") private String title; @@ -33,14 +29,6 @@ public class TaskUpdateDto { @PositiveOrZero(message = "Limit of the number of provided incorrect flags must not be a negative number") private Integer incorrectFlagLimit; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - public String getTitle() { return title; } @@ -49,7 +37,6 @@ public class TaskUpdateDto { this.title = title; } - public String getContent() { return content; } @@ -85,8 +72,7 @@ public class TaskUpdateDto { @Override public String toString() { return "TaskUpdateDto{" + - "id=" + id + - ", title='" + title + '\'' + + "title='" + title + '\'' + ", content='" + content + '\'' + ", flag='" + flag + '\'' + ", solution='" + solution + '\'' + diff --git a/src/main/java/com/example/demo/service/TaskService.java b/src/main/java/com/example/demo/service/TaskService.java index 63c34c5e..d43baffb 100644 --- a/src/main/java/com/example/demo/service/TaskService.java +++ b/src/main/java/com/example/demo/service/TaskService.java @@ -109,8 +109,9 @@ public class TaskService { return BeanMapper.INSTANCE.toDto(task); } - public TaskDto updateTask(Long trainingDefinitionId, Long phaseId, TaskUpdateDto taskUpdateDto) { + public TaskDto updateTask(Long trainingDefinitionId, Long phaseId, Long taskId, TaskUpdateDto taskUpdateDto) { Task taskUpdate = BeanMapper.INSTANCE.toEntity(taskUpdateDto); + taskUpdate.setId(taskId); Task persistedTask = taskRepository.findById(taskUpdate.getId()) .orElseThrow(() -> new RuntimeException("Task was not found")); -- GitLab