Ch 04. 리팩토링
package org.example.web;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.example.model.TodoEntity;
import org.example.model.TodoRequest;
import org.example.model.TodoResponse;
import org.example.service.TodoService;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("/")
public class TodoController {
private final TodoService todoService;
@PostMapping
public ResponseEntity<TodoResponse> create(@RequestBody TodoRequest request) {
log.info("CREATE");
if (ObjectUtils.isEmpty(request.getTitle()))
return ResponseEntity.badRequest().build();
if (ObjectUtils.isEmpty(request.getOrder()))
request.setOrder(0L);
if (ObjectUtils.isEmpty(request.getCompleted()))
request.setCompleted(false);
TodoEntity result = this.todoService.add(request);
return ResponseEntity.ok(new TodoResponse(result));
}
@GetMapping
public ResponseEntity<List<TodoResponse>> readAll() {
log.info("READ ALL");
List<TodoEntity> result = this.todoService.searchAll();
List<TodoResponse> response = result.stream().map(TodoResponse::new).collect(Collectors.toList());
return ResponseEntity.ok(response);
}
@GetMapping("{id}")
public ResponseEntity<TodoResponse> readOne(@PathVariable Long id) {
log.info("READ");
TodoEntity result = this.todoService.searchById(id);
return ResponseEntity.ok(new TodoResponse(result));
}
@PatchMapping("{id}")
public ResponseEntity<TodoResponse> update(@PathVariable Long id, @RequestBody TodoRequest request) {
log.info("UPDATE");
TodoEntity result = this.todoService.updateById(id, request);
return ResponseEntity.ok(new TodoResponse(result));
}
@DeleteMapping
public ResponseEntity<?> deleteAll() {
log.info("DELETE ALL");
this.todoService.deleteAll();
return ResponseEntity.ok().build();
}
@DeleteMapping("{id}")
public ResponseEntity<?> deleteOne(@PathVariable Long id) {
log.info("DELETE");
this.todoService.deleteById(id);
return ResponseEntity.ok().build();
}
}
<TodoController>
package org.example.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TodoResponse {
private Long id;
private String title;
private Long order;
private Boolean completed;
private String url;
public TodoResponse(TodoEntity todoEntity){
this.id = todoEntity.getId();
this.title = todoEntity.getTitle();
this.order = todoEntity.getOrder();
this.completed = todoEntity.getCompleted();
this.url = "http://localhost:8080/" + this.id;
}
}
<TodoResponse.java>
----------------------------------------------------------------------------------------------------------------------------------
https:/bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 7일차 (0) | 2022.01.30 |
---|---|
패스트캠퍼스 챌린지 6일차 (0) | 2022.01.29 |
패스트캠퍼스 챌린지 4일차 (0) | 2022.01.27 |
패스트캠퍼스 챌린지 3일차 (0) | 2022.01.26 |
패스트캠퍼스 챌린지 2일차 (0) | 2022.01.25 |
댓글