게시글
질문&답변
2021.11.25
service와 controller의 역할에 대한 질문이 있습니다.
//서비스를 주입받는 서비스 @Service @Transactional(readOnly = true) @RequiredArgsConstructor public class BookComplexService { private final BookInfoService bookInfoService; private final BookService bookService; private final ClubBookUserService cbuService; private final UserService userService; private final PostService postService; @Transactional public Long createBookAndRelated(CreateBookDto createBookDto, Long userId) { BookInfo bookInfo = BookInfo.builder() .name(createBookDto.getName()) .isbn(createBookDto.getIsbn()) .build(); bookInfo = bookInfoService.createOrFindBookInfo(bookInfo); Book newBook = Book.builder() .category(createBookDto.getCategory()) .bookInfo(bookInfo) .build(); Long bookId = bookService.createBookWithValidation(newBook, bookInfo.getIsbn(), userId); User user = userService.findById(userId); ClubBookUser cbu = ClubBookUser.builder() .book(newBook) .user(user) .build(); cbuService.createClubBookUser(cbu); return bookId; }
- 2
- 5
- 2.3K
질문&답변
2021.11.25
service와 controller의 역할에 대한 질문이 있습니다.
//수정한코드 @PostMapping public ResponseEntity createBook(@SessionAttribute(name = SessionConst.LOGIN_USER, required = false) User loginUser, @RequestBody CreateBookDto createBookDto, UriComponentsBuilder b) { Long bookId = bookComplexService.createBookAndRelated(createBookDto, loginUser.getId()); UriComponents uriComponents = b.path("/books/{bookId}").buildAndExpand(bookId); return ResponseEntity.created(uriComponents.toUri()).body(createBookDto); }
- 2
- 5
- 2.3K
질문&답변
2021.11.25
service와 controller의 역할에 대한 질문이 있습니다.
//기존 코드 @PostMapping public ResponseEntity createBook(@SessionAttribute(name = SessionConst.LOGIN_USER, required = false) User loginUser, @RequestBody CreateBookDto createBookDto, UriComponentsBuilder b){ BookInfo bookInfo = BookInfo.builder() .name(createBookDto.getName()) .isbn(createBookDto.getIsbn()) .build(); try { bookInfoService.createBookInfo(bookInfo); }catch (IllegalStateException e){ log.info("bookInfoService = {}",e.toString()); BookInfo byIsbn = bookInfoService.findByIsbn(bookInfo.getIsbn()); bookInfo = byIsbn; } Book newBook = Book.builder() .category(createBookDto.getCategory()) .bookInfo(bookInfo) .build(); Long bookId = bookService.createBook(newBook); ClubBookUser cbu = ClubBookUser.builder() .book(newBook) .user(loginUser) .build(); cbuService.createClubBookUser(cbu); UriComponents uriComponents = b.path("/books/{bookId}").buildAndExpand(bookId); // return ResponseEntity.noContent().build().created(uriComponents.toUri()).build(); return ResponseEntity.created(uriComponents.toUri()).body(createBookDto); }
- 2
- 5
- 2.3K