15강. 서비스레이어 책 한건보기
public BookRespDto 책한건보기(Long id) {
Optional<Book> bookOP = bookRepository.findById(id);
if(bookOP.isPresent()) {
return new BookRespDto().toDto(bookOP.get());
} else {
throw new RuntimeException("해당 아이디를 찾을 수 없습니다.");
}
}
16강. 서비스레이어 책 삭제하기
@Transactional(rollbackFor = RuntimeException.class)
public void 책삭제하기(Long id) {
bookRepository.deleteById(id);
}
- null이 들어오면 컨트롤러에서 막음
- 없는 id가 들어오면 롤백할 필요가 없다.
17강. 서비스레이어 책 수정하기
@Transactional(rollbackFor = RuntimeException.class)
public void 책수정하기(Long id, BookSaveReqDto dto) {
Optional<Book> bookOP = bookRepository.findById(id);
if (bookOP.isPresent()) {
Book bookPS = bookOP.get();
bookPS.update(dto.getTitle(), dto.getAuthor());
} else {
throw new RuntimeException("해당 아이디를 찾을 수 없습니다.");
}
}