CH 03. 인텔리제이 실습 - To do List 구현하기
05. 서비스 코드 구현
- Service layer : 작성한 repository가 실제로 작성하는 코드를 구현
//TodoService
package org.example.servcie;
import lombok.AllArgsConstructor;
import org.example.model.TodoEntity;
import org.example.model.TodoRequest;
import org.example.repository.TodoRepository;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.List;
@Service
@AllArgsConstructor
public class TodoService {
private final TodoRepository todoRepository;
public TodoEntity add(TodoRequest request){ //1 todo 리스트 목록에 아이템을 추가
TodoEntity todoEntity = new TodoEntity();
todoEntity.setTitle(request.getTitle());
todoEntity.setOrder(request.getOrder());
todoEntity.setCompleted(request.getCompleted());
return this.todoRepository.save(todoEntity);
}
public TodoEntity searchById(Long id){ //2 todo 리스트 목록 중 특정 아이템을 조회
return this.todoRepository.findById(id)
.orElseThrow(()-> new ResponseStatusException(HttpStatus.NOT_FOUND));
}
public List<TodoEntity> serachAll() { //3 todo 리스트 전체 목록을 조회
return this.todoRepository.findAll();
}
public TodoEntity updateById(Long id, TodoRequest request){
//4 todo 리스트 목록 중 특정 아이템을 수정
TodoEntity todoEntity = this.searchById(id);
if (request.getTitle() != null){
todoEntity.setTitle(request.getTitle());
}
if (request.getOrder() != null) {
todoEntity.setOrder(request.getOrder());
}
if (request.getCompleted() != null){
todoEntity.setCompleted(request.getCompleted());
}
return this.todoRepository.save(todoEntity);
}
public void deleteById(Long id){ //5 todo 리스트 목록 중 특정 아이템을 삭제
this.todoRepository.deleteById(id);
}
public void deleteAll(){ //6 todo 리스트 전체 목록을 삭제
this.todoRepository.deleteAll();
}
}
06. 컨트롤러 구현
- Control : Server에 들어오는 Request를 처리 및 Response
package org.example.controller;
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();
}
}
----------------------------------------------------------------------------------------------------------------------------------
https:/bit.ly/37BpXic
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 6일차 (0) | 2022.01.29 |
---|---|
패스트캠퍼스 챌린지 5일차 (0) | 2022.01.28 |
패스트캠퍼스 챌린지 4일차 (0) | 2022.01.27 |
패스트캠퍼스 챌린지 2일차 (0) | 2022.01.25 |
패스트캠퍼스 챌린지 1일차 - Intellij 가이드 (0) | 2022.01.24 |
댓글