Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added some mappers #295

Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
2f250e6
added some mappers
ZiborovaVictoria May 9, 2024
bdb5527
fixed npe
ZiborovaVictoria May 9, 2024
a082e3f
issues/294 (incorrect data: rating and reviews count) was fixed
Sunagatov May 10, 2024
eb5e523
ProductReviewsStatisticsProvider was fixed
Sunagatov May 10, 2024
2b4eda1
ProductApi and ProductApiImpl were deleted
Sunagatov May 10, 2024
1130cc9
UserEntity was deleted in FavoriteListEntity
Sunagatov May 10, 2024
cd729c9
issues/291 (incorrect order of products when sorted by average rating…
Sunagatov May 10, 2024
8bba605
08.02.2024.part2.insert-product-review-table.sql was updated
Sunagatov May 10, 2024
17d2fae
28.07.2023.part6.insert-user-details-table-data.sql was updated
Sunagatov May 10, 2024
3b73355
added new addresses
Sunagatov May 10, 2024
8374128
Create dependabot.yml
Sunagatov May 10, 2024
77ab250
Update issue templates
Sunagatov May 11, 2024
265d8e8
Update dev-branch-pr-deployment-pipeline.yml
Sunagatov May 11, 2024
9d3f9c5
Create codacy.yml
Sunagatov May 11, 2024
2afeebf
Create codeql.yml
Sunagatov May 11, 2024
085ebef
Create sonarqube.yml
Sunagatov May 11, 2024
2d19887
Create greetings.yml
Sunagatov May 11, 2024
3e0d674
Update dependabot.yml
Sunagatov May 11, 2024
87cbfda
Update dependabot.yml
Sunagatov May 11, 2024
b2a347d
Update codeql.yml
Sunagatov May 11, 2024
a63f210
README.md was updated
Sunagatov May 11, 2024
12070a0
README.md was updated
Sunagatov May 11, 2024
8082116
fixed expressions -> @Named
ZiborovaVictoria May 13, 2024
f4b39d3
added tests for converters
ZiborovaVictoria May 13, 2024
f8a49f8
README.md was updated
Sunagatov May 14, 2024
a954692
Migration from Java 17 to Java 21 + fixed GOOGLE_AUTH error for local…
annstriganova May 16, 2024
9aadeb9
updated version in pom.xml
annstriganova May 16, 2024
49ec646
updated version in dev-branch-pr-build-and-test-pipeline.yaml
annstriganova May 17, 2024
eea40dc
Merge pull request #300 from Sunagatov/feature/java-21
Sunagatov May 17, 2024
3cefb0c
Update README.md
Sunagatov May 17, 2024
6528206
fixes
ZiborovaVictoria May 18, 2024
3ca1bc4
Update README.md
Sunagatov May 21, 2024
76cd7a4
added some mappers
ZiborovaVictoria May 9, 2024
f2f16fe
fixed npe
ZiborovaVictoria May 9, 2024
8444ba0
fixed expressions -> @Named
ZiborovaVictoria May 13, 2024
d61eb62
added tests for converters
ZiborovaVictoria May 13, 2024
f23b4db
fixes
ZiborovaVictoria May 18, 2024
dcfe493
Merge remote-tracking branch 'origin/feature/IL-287-custom-mappers-to…
ZiborovaVictoria May 26, 2024
5cb0e5f
tried to merge
ZiborovaVictoria May 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,50 +6,42 @@
import com.zufar.icedlatte.favorite.entity.FavoriteListEntity;
import com.zufar.icedlatte.openapi.dto.ProductInfoDto;
import com.zufar.icedlatte.product.entity.ProductInfo;
import com.zufar.icedlatte.user.entity.UserEntity;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.HashSet;
import java.util.Set;
import java.util.Optional;
import java.util.UUID;

@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
uses = FavoriteItemDtoConverter.class, unmappedTargetPolicy = ReportingPolicy.IGNORE, injectionStrategy = InjectionStrategy.FIELD)
public interface FavoriteListDtoConverter {

default FavoriteListDto toDto(final FavoriteListEntity favoriteListEntity) {
UUID id = favoriteListEntity.getId();
UUID userId = favoriteListEntity.getUser().getId();
Set<FavoriteItemDto> favoriteItemsDto = new HashSet<>();
@Mapping(target = "userId", source = "user", qualifiedByName = "toUserId")
@Mapping(target = "favoriteItems", source = "favoriteItems", qualifiedByName = "mapFavoriteItems")
FavoriteListDto toDto(final FavoriteListEntity favoriteListEntity);

for (FavoriteItemEntity itemEntity : favoriteListEntity.getFavoriteItems()) {
UUID favoriteItemEntityId = itemEntity.getId();
ProductInfo productInfo = itemEntity.getProductInfo();
@Mapping(target = "id", source = "productId")
ProductInfoDto convertProductInfoDto(ProductInfo productInfo);

ProductInfoDto productInfoDto = convertProductInfoDto(productInfo);
@Named("toUserId")
default UUID convertToUserId(UserEntity user) {
Optional<UserEntity> userOptional = Optional.ofNullable(user);
annstriganova marked this conversation as resolved.
Show resolved Hide resolved
Optional<UUID> userIdOptional = userOptional.map(UserEntity::getId);
return userIdOptional.orElse(null);
}

FavoriteItemDto favoriteItemDto = new FavoriteItemDto(favoriteItemEntityId, productInfoDto);
favoriteItemsDto.add(favoriteItemDto);
}
@Named("mapFavoriteItems")
default FavoriteItemDto toFavoriteItemDto(FavoriteItemEntity itemEntity) {
UUID favoriteItemEntityId = itemEntity.getId();
ProductInfo productInfo = itemEntity.getProductInfo();

return new FavoriteListDto(id, userId, favoriteItemsDto, favoriteListEntity.getUpdatedAt());
}
ProductInfoDto productInfoDto = convertProductInfoDto(productInfo);
ZiborovaVictoria marked this conversation as resolved.
Show resolved Hide resolved

private static ProductInfoDto convertProductInfoDto(ProductInfo productInfo) {
return new ProductInfoDto(
productInfo.getProductId(),
productInfo.getName(),
productInfo.getDescription(),
productInfo.getPrice(),
productInfo.getQuantity(),
productInfo.getActive(),
productInfo.getAverageRating(),
productInfo.getReviewsCount(),
productInfo.getBrandName(),
productInfo.getSellerName()

);
return new FavoriteItemDto(favoriteItemEntityId, productInfoDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import com.zufar.icedlatte.openapi.dto.ProductInfoDto;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.MappingConstants;
import org.mapstruct.ReportingPolicy;
import org.mapstruct.Mapping;
import org.mapstruct.Named;

import java.util.List;
import java.util.Set;
Expand All @@ -17,13 +18,8 @@
unmappedTargetPolicy = ReportingPolicy.IGNORE, injectionStrategy = InjectionStrategy.FIELD)
public interface ListOfFavoriteProductsDtoConverter {

default ListOfFavoriteProductsDto toListProductDto(FavoriteListDto favoriteList) {
ListOfFavoriteProductsDto listOfFavoriteProductsDto = new ListOfFavoriteProductsDto();
for (FavoriteItemDto item : favoriteList.favoriteItems()) {
listOfFavoriteProductsDto.addProductsItem(item.productInfo());
}
return listOfFavoriteProductsDto;
}
@Mapping(target = "products", source = "favoriteItems", qualifiedByName = "toListProductInfoDto")
ListOfFavoriteProductsDto toListProductDto(FavoriteListDto favoriteList);

@Named("toListProductInfoDto")
default List<ProductInfoDto> toProductInfoDto(final Set<FavoriteItemDto> favoriteItems) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;
import java.util.UUID;

@Slf4j
Expand All @@ -37,7 +38,7 @@ public ProductReviewRatingStats getRatingAndReviewStat(final UUID productId) {
log.error("The product with productId = {} was not found.", productId);
throw new ProductNotFoundException(productId);
}
String formattedAvgRating = String.format("%.1f", avgRating);
String formattedAvgRating = String.format(Locale.US, "%.1f", avgRating);
Integer reviewsCount = reviewRepository.getReviewCountProductById(productId);
RatingMap productRatingMap = productReviewDtoConverter.convertToProductRatingMap(productRatingCountPairs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,52 @@
import com.zufar.icedlatte.openapi.dto.RatingMap;
import com.zufar.icedlatte.review.dto.ProductRatingCount;
import com.zufar.icedlatte.review.entity.ProductReview;
import com.zufar.icedlatte.user.entity.UserEntity;
import org.mapstruct.InjectionStrategy;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.MappingConstants;
import org.mapstruct.ReportingPolicy;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ProductReviewDtoConverter {
@Mapper(componentModel = MappingConstants.ComponentModel.SPRING,
uses = ProductReviewDtoConverter.class, unmappedTargetPolicy = ReportingPolicy.IGNORE, injectionStrategy = InjectionStrategy.FIELD)
public interface ProductReviewDtoConverter {

public static final ProductReviewDto EMPTY_PRODUCT_REVIEW_RESPONSE =
new ProductReviewDto(null, null,null, null, null, null, null, null, null);
public static final ProductReviewDto EMPTY_PRODUCT_REVIEW_RESPONSE =
new ProductReviewDto(null, null, null, null, null, null, null, null, null);

public ProductReviewDto toProductReviewDto(ProductReview productReview) {
return new ProductReviewDto(
productReview.getId(),
productReview.getProductId(),
productReview.getProductRating(),
productReview.getText(),
productReview.getCreatedAt(),
productReview.getUser().getFirstName(),
productReview.getUser().getLastName(),
productReview.getLikesCount(),
productReview.getDislikesCount());
@Mapping(target = "productReviewId", source = "id")
@Mapping(target = "userName", source = "user", qualifiedByName = "toUserName")
@Mapping(target = "userLastname", source = "user", qualifiedByName = "toUserLastName")
ProductReviewDto toProductReviewDto(ProductReview productReview);

@Mapping(target = "page", expression = "java(page.getTotalPages())")
@Mapping(target = "size", expression = "java(page.getSize())")
@Mapping(target = "totalElements", expression = "java(page.getTotalElements())")
@Mapping(target = "totalPages", expression = "java(page.getTotalPages())")
@Mapping(target = "reviewsWithRatings", expression = "java(page.getContent())")
ProductReviewsAndRatingsWithPagination toProductReviewsAndRatingsWithPagination(final Page<ProductReviewDto> page);

@Named("toUserName")
default String convertToUserName(UserEntity user) {
Optional<UserEntity> userOptional = Optional.of(user);
Optional<String> firstNameOptional = userOptional.map(UserEntity::getFirstName);
return firstNameOptional.orElse(null);
}

public ProductReviewsAndRatingsWithPagination toProductReviewsAndRatingsWithPagination(final Page<ProductReviewDto> page) {
var result = new ProductReviewsAndRatingsWithPagination();
result.setPage(page.getTotalPages());
result.setSize(page.getSize());
result.setTotalElements(page.getTotalElements());
result.setTotalPages(page.getTotalPages());
result.setReviewsWithRatings(page.getContent());
return result;
@Named("toUserLastName")
default String convertToUserLastName(UserEntity user) {
Optional<UserEntity> userOptional = Optional.of(user);
Optional<String> lastNameOptional = userOptional.map(UserEntity::getLastName);
return lastNameOptional.orElse(null);
}

public RatingMap convertToProductRatingMap(List<ProductRatingCount> productRatingCountPairs) {
default RatingMap convertToProductRatingMap(List<ProductRatingCount> productRatingCountPairs) {
var productRatingMap = new RatingMap(0, 0, 0, 0, 0);

for (ProductRatingCount productRatingCount : productRatingCountPairs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.zufar.icedlatte.favorite.converter;

import com.zufar.icedlatte.favorite.dto.FavoriteItemDto;
import com.zufar.icedlatte.favorite.entity.FavoriteItemEntity;
import com.zufar.icedlatte.product.converter.ProductInfoDtoConverter;
import com.zufar.icedlatte.product.entity.ProductInfo;
import org.junit.jupiter.api.BeforeEach;
import org.mapstruct.factory.Mappers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import java.util.UUID;

public class FavoriteItemDtoConverterTest {

private FavoriteItemDtoConverter converter;
private ProductInfoDtoConverter productInfoDtoConverter;

@BeforeEach
void setup() {
converter = Mappers.getMapper(FavoriteItemDtoConverter.class);
productInfoDtoConverter = Mappers.getMapper(ProductInfoDtoConverter.class);
}

@Test
@DisplayName("Convert FavoriteItemEntity to FavoriteItemDto")
void toDtoTest() {
ZiborovaVictoria marked this conversation as resolved.
Show resolved Hide resolved

ProductInfo productInfo = new ProductInfo();
productInfo.setProductId(UUID.randomUUID());

FavoriteItemEntity expectedFavoriteItem = new FavoriteItemEntity();
expectedFavoriteItem.setProductInfo(productInfo);

FavoriteItemDto actualFavoriteItemDto = converter.toDto(productInfoDtoConverter, expectedFavoriteItem);
// FIXME: different types of sets
//assertThat(actualFavoriteItemDto.productInfo()).isEqualTo(expectedFavoriteItem.getProductInfo());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.zufar.icedlatte.favorite.converter;

import com.zufar.icedlatte.favorite.dto.FavoriteItemDto;
import com.zufar.icedlatte.favorite.dto.FavoriteListDto;
import com.zufar.icedlatte.favorite.entity.FavoriteItemEntity;
import com.zufar.icedlatte.favorite.entity.FavoriteListEntity;
import com.zufar.icedlatte.openapi.dto.ProductInfoDto;
import com.zufar.icedlatte.product.entity.ProductInfo;
import com.zufar.icedlatte.user.stub.UserDtoTestStub;
import org.junit.jupiter.api.BeforeEach;
import org.mapstruct.factory.Mappers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;

import java.math.BigDecimal;
import java.time.OffsetDateTime;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class FavoriteListDtoConverterTest {

private FavoriteListDtoConverter converter;

@BeforeEach
void setup() {
converter = Mappers.getMapper(FavoriteListDtoConverter.class);
}

@Test
@DisplayName("Convert FavoriteListEntity to FavoriteListDto")
void convertListEntityToDto() {

ProductInfo productInfo = new ProductInfo();
productInfo.setProductId(UUID.randomUUID());

FavoriteItemEntity favoriteItem = new FavoriteItemEntity();
favoriteItem.setProductInfo(productInfo);

FavoriteListEntity expectedFavoriteListEntity = FavoriteListEntity.builder()
.id(UUID.randomUUID())
.user(UserDtoTestStub.createUserEntity())
.favoriteItems(Set.of(favoriteItem))
.updatedAt(OffsetDateTime.now())
.build();

FavoriteListDto actualFavoriteListDto = converter.toDto(expectedFavoriteListEntity);

assertEquals(actualFavoriteListDto.id(), expectedFavoriteListEntity.getId());
assertEquals(actualFavoriteListDto.updatedAt(), expectedFavoriteListEntity.getUpdatedAt());
assertEquals(actualFavoriteListDto.userId(), Optional.of(expectedFavoriteListEntity.getUser()).get().getId());
ZiborovaVictoria marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Convert ProductInfo to ProductInfoDto")
void convertToProductInfoDto() {

ProductInfo expectedProductInfo = new ProductInfo(UUID.randomUUID(), "Coffee", "Coffee description",
new BigDecimal(100), 1, true, new BigDecimal(100), 1, "Jacobs", "Seller");

ProductInfoDto actualProductInfoDto = converter.convertProductInfoDto(expectedProductInfo);

assertThat(actualProductInfoDto.getId()).isEqualTo(expectedProductInfo.getProductId());
assertThat(actualProductInfoDto.getName()).isEqualTo(expectedProductInfo.getName());
assertThat(actualProductInfoDto.getDescription()).isEqualTo(expectedProductInfo.getDescription());
assertThat(actualProductInfoDto.getPrice()).isEqualTo(expectedProductInfo.getPrice());
assertThat(actualProductInfoDto.getQuantity()).isEqualTo(expectedProductInfo.getQuantity());
assertThat(actualProductInfoDto.getActive()).isEqualTo(expectedProductInfo.getActive());
assertThat(actualProductInfoDto.getAverageRating()).isEqualTo(expectedProductInfo.getAverageRating());
assertThat(actualProductInfoDto.getReviewsCount()).isEqualTo(expectedProductInfo.getReviewsCount());
assertThat(actualProductInfoDto.getBrandName()).isEqualTo(expectedProductInfo.getBrandName());
assertThat(actualProductInfoDto.getSellerName()).isEqualTo(expectedProductInfo.getSellerName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.zufar.icedlatte.favorite.converter;

import com.zufar.icedlatte.favorite.dto.FavoriteItemDto;
import com.zufar.icedlatte.favorite.dto.FavoriteListDto;
import com.zufar.icedlatte.openapi.dto.ListOfFavoriteProductsDto;
import com.zufar.icedlatte.openapi.dto.ProductInfoDto;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.DisplayName;
import org.mapstruct.factory.Mappers;

import java.time.OffsetDateTime;
import java.util.Set;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ListOfFavoriteProductsDtoConverterTest {

private ListOfFavoriteProductsDtoConverter converter;

@BeforeEach
void setup() {
converter = Mappers.getMapper(ListOfFavoriteProductsDtoConverter.class);
}

@Test
@DisplayName("Convert FavoriteListDto to ListOfFavoriteProductsDto")
void toListProductDto() {

ProductInfoDto productInfoDto = new ProductInfoDto();
productInfoDto.setId(UUID.randomUUID());

FavoriteItemDto favoriteItemDto = new FavoriteItemDto(
UUID.randomUUID(),
productInfoDto);

FavoriteListDto expectedFavoriteListDto = new FavoriteListDto(
UUID.randomUUID(),
UUID.randomUUID(),
Set.of(favoriteItemDto),
OffsetDateTime.now());

ListOfFavoriteProductsDto actualListOfFavoriteProductsDto = converter.toListProductDto(expectedFavoriteListDto);

assertThat(actualListOfFavoriteProductsDto.getProducts()).isEqualTo(converter.toProductInfoDto(expectedFavoriteListDto.favoriteItems()));
annstriganova marked this conversation as resolved.
Show resolved Hide resolved

assertEquals(actualListOfFavoriteProductsDto.getProducts().get(0), productInfoDto);
}
}
Loading