Junit 15, 16, 17강

연어는결국강으로·2022년 10월 4일
0

Junit

목록 보기
6/9

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("해당 아이디를 찾을 수 없습니다.");
	}
	
	// 메서드 종료시에 더티체킹으로(flush) update 된다.
}

0개의 댓글