Skip to content

Commit

Permalink
added AfterMapping in BookMapper, to set categories into Book
Browse files Browse the repository at this point in the history
  • Loading branch information
ihor-sydorenko committed Sep 19, 2023
1 parent e3dedd2 commit 38ab123
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ public class CreateBookRequestDto {
private String description;
private String coverImage;
@NotNull
private Set<Long> categoriesIds;
private Set<Long> categoryIds;
}
2 changes: 1 addition & 1 deletion src/main/java/book/store/dto/response/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ public class BookDto {
private BigDecimal price;
private String description;
private String coverImage;
private Set<Long> categoriesIds;
private Set<Long> categoryIds;
}
20 changes: 17 additions & 3 deletions src/main/java/book/store/mapper/BookMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Category> categories = new HashSet<>();
for (Long categoryId : createBookRequestDto.getCategoryIds()) {
Category category = new Category();
category.setId(categoryId);
categories.add(category);
}
book.setCategories(categories);
}
}
2 changes: 0 additions & 2 deletions src/main/java/book/store/repository/CategoryRepository.java
Original file line number Diff line number Diff line change
@@ -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<Category, Long> {
Set<Category> findByIdIn(Set<Long> categoriesIds);
}
4 changes: 1 addition & 3 deletions src/main/java/book/store/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 38ab123

Please sign in to comment.