From 38ab123c483ae7c086c58ad96639c51c48b9471c Mon Sep 17 00:00:00 2001 From: Ihor Sydorenko Date: Tue, 19 Sep 2023 22:16:01 +0300 Subject: [PATCH] added AfterMapping in BookMapper, to set categories into Book --- .../dto/request/CreateBookRequestDto.java | 2 +- .../java/book/store/dto/response/BookDto.java | 2 +- .../java/book/store/mapper/BookMapper.java | 20 ++++++++++++++++--- .../store/repository/CategoryRepository.java | 2 -- .../store/service/impl/BookServiceImpl.java | 4 +--- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/main/java/book/store/dto/request/CreateBookRequestDto.java b/src/main/java/book/store/dto/request/CreateBookRequestDto.java index 074f50e..232d948 100644 --- a/src/main/java/book/store/dto/request/CreateBookRequestDto.java +++ b/src/main/java/book/store/dto/request/CreateBookRequestDto.java @@ -20,5 +20,5 @@ public class CreateBookRequestDto { private String description; private String coverImage; @NotNull - private Set categoriesIds; + private Set categoryIds; } diff --git a/src/main/java/book/store/dto/response/BookDto.java b/src/main/java/book/store/dto/response/BookDto.java index 13c5722..00eaebe 100644 --- a/src/main/java/book/store/dto/response/BookDto.java +++ b/src/main/java/book/store/dto/response/BookDto.java @@ -13,5 +13,5 @@ public class BookDto { private BigDecimal price; private String description; private String coverImage; - private Set categoriesIds; + private Set categoryIds; } diff --git a/src/main/java/book/store/mapper/BookMapper.java b/src/main/java/book/store/mapper/BookMapper.java index ddaf82b..91970a8 100644 --- a/src/main/java/book/store/mapper/BookMapper.java +++ b/src/main/java/book/store/mapper/BookMapper.java @@ -6,6 +6,8 @@ import book.store.dto.response.BookDtoWithoutCategoryIds; import book.store.model.Book; import book.store.model.Category; +import java.util.HashSet; +import java.util.Set; import java.util.stream.Collectors; import org.mapstruct.AfterMapping; import org.mapstruct.Mapper; @@ -14,18 +16,30 @@ @Mapper(config = MapperConfig.class) public interface BookMapper { - @Mapping(target = "categoriesIds", ignore = true) + @Mapping(target = "categoryIds", ignore = true) BookDto toDto(Book book); - @Mapping(target = "id", ignore = true) + @Mapping(target = "categories", ignore = true) Book toModel(CreateBookRequestDto createBookRequestDto); BookDtoWithoutCategoryIds toDtoWithoutCategories(Book book); @AfterMapping default void setCategoryIds(@MappingTarget BookDto bookDto, Book book) { - bookDto.setCategoriesIds(book.getCategories().stream() + bookDto.setCategoryIds(book.getCategories().stream() .map(Category::getId) .collect(Collectors.toSet())); } + + @AfterMapping + default void setCategoriesIntoBook(@MappingTarget Book book, + CreateBookRequestDto createBookRequestDto) { + Set categories = new HashSet<>(); + for (Long categoryId : createBookRequestDto.getCategoryIds()) { + Category category = new Category(); + category.setId(categoryId); + categories.add(category); + } + book.setCategories(categories); + } } diff --git a/src/main/java/book/store/repository/CategoryRepository.java b/src/main/java/book/store/repository/CategoryRepository.java index 4f44d15..00d333e 100644 --- a/src/main/java/book/store/repository/CategoryRepository.java +++ b/src/main/java/book/store/repository/CategoryRepository.java @@ -1,9 +1,7 @@ package book.store.repository; import book.store.model.Category; -import java.util.Set; import org.springframework.data.jpa.repository.JpaRepository; public interface CategoryRepository extends JpaRepository { - Set findByIdIn(Set categoriesIds); } diff --git a/src/main/java/book/store/service/impl/BookServiceImpl.java b/src/main/java/book/store/service/impl/BookServiceImpl.java index c030efd..abb2c99 100644 --- a/src/main/java/book/store/service/impl/BookServiceImpl.java +++ b/src/main/java/book/store/service/impl/BookServiceImpl.java @@ -23,9 +23,7 @@ public class BookServiceImpl implements BookService { @Override public BookDto save(CreateBookRequestDto requestDto) { - Book book = bookMapper.toModel(requestDto); - book.setCategories(categoryRepository.findByIdIn(requestDto.getCategoriesIds())); - return bookMapper.toDto(bookRepository.save(book)); + return bookMapper.toDto(bookRepository.save(bookMapper.toModel(requestDto))); } @Override