diff --git a/build.gradle b/build.gradle index 1e3b1d93..5602def3 100644 --- a/build.gradle +++ b/build.gradle @@ -49,8 +49,6 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-oauth2-client' implementation 'com.google.api-client:google-api-client:2.2.0' implementation 'com.google.auth:google-auth-library-oauth2-http:1.16.0' -// implementation 'com.fasterxml.jackson.core:jackson-core:2.14.2' -// implementation 'com.fasterxml.jackson.core:jackson-databind:2.14.2' // QueryDsl implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' @@ -61,6 +59,10 @@ dependencies { // AWS S3 implementation 'com.amazonaws:aws-java-sdk-s3:1.12.232' + + // DtoMapper + implementation 'org.mapstruct:mapstruct:1.4.2.Final' + annotationProcessor 'org.mapstruct:mapstruct-processor:1.4.2.Final' } diff --git a/src/main/java/com/sluv/server/domain/brand/mapper/BrandMapper.java b/src/main/java/com/sluv/server/domain/brand/mapper/BrandMapper.java new file mode 100644 index 00000000..e99bcbe0 --- /dev/null +++ b/src/main/java/com/sluv/server/domain/brand/mapper/BrandMapper.java @@ -0,0 +1,12 @@ +package com.sluv.server.domain.brand.mapper; + +import com.sluv.server.domain.brand.dto.BrandSearchResDto; +import com.sluv.server.domain.brand.entity.Brand; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface BrandMapper { + + BrandSearchResDto toBrandSearchResDto(Brand brand); + +} \ No newline at end of file diff --git a/src/main/java/com/sluv/server/domain/brand/mapper/NewBrandMapper.java b/src/main/java/com/sluv/server/domain/brand/mapper/NewBrandMapper.java new file mode 100644 index 00000000..f8564399 --- /dev/null +++ b/src/main/java/com/sluv/server/domain/brand/mapper/NewBrandMapper.java @@ -0,0 +1,22 @@ +package com.sluv.server.domain.brand.mapper; + +import com.sluv.server.domain.brand.dto.NewBrandPostReqDto; +import com.sluv.server.domain.brand.dto.NewBrandPostResDto; +import com.sluv.server.domain.brand.entity.NewBrand; +import com.sluv.server.domain.brand.enums.NewBrandStatus; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +@Mapper(componentModel = "spring") +public interface NewBrandMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "brandName", source = "newBrandPostReqDto.newBrandName") + @Mapping(target = "newBrandStatus", source = "newBrandStatus", defaultValue = "ACTIVE") + NewBrand toEntity(NewBrandPostReqDto newBrandPostReqDto, NewBrandStatus newBrandStatus); + + @Mapping(target = "newBrandId", source = "newBrand.id") + @Mapping(target = "newBrandName", source = "newBrand.brandName") + NewBrandPostResDto toNewBrandPostResDto(NewBrand newBrand); + +} diff --git a/src/main/java/com/sluv/server/domain/brand/mapper/RecentSelectBrandMapper.java b/src/main/java/com/sluv/server/domain/brand/mapper/RecentSelectBrandMapper.java new file mode 100644 index 00000000..b31b18a3 --- /dev/null +++ b/src/main/java/com/sluv/server/domain/brand/mapper/RecentSelectBrandMapper.java @@ -0,0 +1,68 @@ +package com.sluv.server.domain.brand.mapper; + +import com.sluv.server.domain.brand.dto.RecentSelectBrandResDto; +import com.sluv.server.domain.brand.entity.Brand; +import com.sluv.server.domain.brand.entity.NewBrand; +import com.sluv.server.domain.brand.entity.RecentSelectBrand; +import com.sluv.server.domain.user.entity.User; +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.Named; + +@Mapper(componentModel = "spring") +public interface RecentSelectBrandMapper { + + @Mapping(target = "id", ignore = true) + @Mapping(target = "brand", source = "brand") + @Mapping(target = "newBrand", source = "newBrand") + @Mapping(target = "user", source = "user") + RecentSelectBrand toEntity(Brand brand, NewBrand newBrand, User user); + + @Mapping(target = "id", source = "recentSelectBrand", qualifiedByName = "mapId") + @Mapping(target = "brandName", source = "recentSelectBrand", qualifiedByName = "mapBrandName") + @Mapping(target = "brandImgUrl", source = "recentSelectBrand", qualifiedByName = "mapBrandUrl") + @Mapping(target = "flag", source = "recentSelectBrand", qualifiedByName = "mapFlag") + RecentSelectBrandResDto toRecentSelectBrandResDto(RecentSelectBrand recentSelectBrand); + + @Named("mapId") + default Long mapId(RecentSelectBrand recentSelectBrand) { + Long brandId; + String flag = recentSelectBrand.getBrand() != null ? "Y" :"N"; + if(flag.equals("Y")){ + brandId = recentSelectBrand.getBrand().getId(); + }else{ + brandId = recentSelectBrand.getNewBrand().getId(); + } + return brandId; + } + + @Named("mapBrandName") + static String mapBrandName(RecentSelectBrand recentSelectBrand) { + String brandName; + String flag = recentSelectBrand.getBrand() != null ? "Y" :"N"; + if(flag.equals("Y")){ + brandName = recentSelectBrand.getBrand().getBrandKr(); + }else{ + brandName = recentSelectBrand.getNewBrand().getBrandName(); + } + return brandName; + } + + @Named("mapBrandUrl") + static String mapBrandUrl(RecentSelectBrand recentSelectBrand) { + String brandImgUrl; + String flag = recentSelectBrand.getBrand() != null ? "Y" :"N"; + if(flag.equals("Y")){ + brandImgUrl = recentSelectBrand.getBrand().getBrandImgUrl(); + }else{ + brandImgUrl = null; + } + return brandImgUrl; + } + + @Named("mapFlag") + static String mapFlag(RecentSelectBrand recentSelectBrand) { + String flag = recentSelectBrand.getBrand() != null ? "Y" :"N"; + return flag; + } +} diff --git a/src/main/java/com/sluv/server/domain/brand/service/BrandService.java b/src/main/java/com/sluv/server/domain/brand/service/BrandService.java index 656f4081..b667b594 100644 --- a/src/main/java/com/sluv/server/domain/brand/service/BrandService.java +++ b/src/main/java/com/sluv/server/domain/brand/service/BrandService.java @@ -4,6 +4,8 @@ import com.sluv.server.domain.brand.dto.RecentSelectBrandResDto; import com.sluv.server.domain.brand.entity.Brand; import com.sluv.server.domain.brand.entity.RecentSelectBrand; +import com.sluv.server.domain.brand.mapper.BrandMapper; +import com.sluv.server.domain.brand.mapper.RecentSelectBrandMapper; import com.sluv.server.domain.brand.repository.BrandRepository; import com.sluv.server.domain.brand.repository.RecentSelectBrandRepository; import com.sluv.server.domain.user.entity.User; @@ -23,6 +25,8 @@ public class BrandService { private final BrandRepository brandRepository; private final RecentSelectBrandRepository recentSelectBrandRepository; private final UserRepository userRepository; + private final BrandMapper brandMapper; + private final RecentSelectBrandMapper recentSelectBrandMapper; public PaginationResDto findAllBrand(String brandName, Pageable pageable){ @@ -30,12 +34,13 @@ public PaginationResDto findAllBrand(String brandName, Pageab Page brandPage = brandRepository.findByAllBrandKrOrBrandEnStartingWith(brandName, pageable); List dtoList = brandPage.stream() - .map(data -> BrandSearchResDto.builder() - .id(data.getId()) - .brandKr(data.getBrandKr()) - .brandEn(data.getBrandEn()) - .brandImgUrl(data.getBrandImgUrl()) - .build() + .map(brandMapper::toBrandSearchResDto +// .map(data -> BrandSearchResDto.builder() +// .id(data.getId()) +// .brandKr(data.getBrandKr()) +// .brandEn(data.getBrandEn()) +// .brandImgUrl(data.getBrandImgUrl()) +// .build() ).toList(); return PaginationResDto.builder() @@ -49,12 +54,7 @@ public PaginationResDto findAllBrand(String brandName, Pageab public List findTopBrand() { return brandRepository.findTop10By().stream() - .map(data -> BrandSearchResDto.builder() - .id(data.getId()) - .brandKr(data.getBrandKr()) - .brandEn(data.getBrandEn()) - .brandImgUrl(data.getBrandImgUrl()) - .build() + .map(brandMapper::toBrandSearchResDto ).collect(Collectors.toList()); } @@ -62,27 +62,9 @@ public List findRecentSelectBrand(User user) { List recentSelectBrandList = recentSelectBrandRepository.getRecentSelectBrandTop20(user); - return recentSelectBrandList.stream().map(recentSelectBrand -> { - Long brandId; - String brandName; - String brandImgUrl; - String flag = recentSelectBrand.getBrand() != null ? "Y" :"N"; - if(flag.equals("Y")){ - brandId = recentSelectBrand.getBrand().getId(); - brandName = recentSelectBrand.getBrand().getBrandKr(); - brandImgUrl = recentSelectBrand.getBrand().getBrandImgUrl(); - }else{ - brandId = recentSelectBrand.getNewBrand().getId(); - brandName = recentSelectBrand.getNewBrand().getBrandName(); - brandImgUrl = null; - } - return RecentSelectBrandResDto.builder() - .id(brandId) - .brandName(brandName) - .brandImgUrl(brandImgUrl) - .flag(flag) - .build(); - }).toList(); + return recentSelectBrandList.stream() + .map(recentSelectBrandMapper::toRecentSelectBrandResDto) + .toList(); } } diff --git a/src/main/java/com/sluv/server/domain/brand/service/NewBrandService.java b/src/main/java/com/sluv/server/domain/brand/service/NewBrandService.java index 10bc2d76..95ef297b 100644 --- a/src/main/java/com/sluv/server/domain/brand/service/NewBrandService.java +++ b/src/main/java/com/sluv/server/domain/brand/service/NewBrandService.java @@ -4,6 +4,7 @@ import com.sluv.server.domain.brand.dto.NewBrandPostResDto; import com.sluv.server.domain.brand.entity.NewBrand; import com.sluv.server.domain.brand.enums.NewBrandStatus; +import com.sluv.server.domain.brand.mapper.NewBrandMapper; import com.sluv.server.domain.brand.repository.NewBrandRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -12,20 +13,15 @@ @RequiredArgsConstructor public class NewBrandService { private final NewBrandRepository newBrandRepository; + private final NewBrandMapper newBrandMapper; public NewBrandPostResDto postNewBrand(NewBrandPostReqDto dto){ NewBrand newBrand = newBrandRepository.save( - NewBrand.builder() - .brandName(dto.getNewBrandName()) - .newBrandStatus(NewBrandStatus.ACTIVE) - .build() + newBrandMapper.toEntity(dto, null) ); - return NewBrandPostResDto.builder() - .newBrandId(newBrand.getId()) - .newBrandName(newBrand.getBrandName()) - .build(); + return newBrandMapper.toNewBrandPostResDto(newBrand); } diff --git a/src/main/java/com/sluv/server/domain/brand/service/RecentSelectBrandService.java b/src/main/java/com/sluv/server/domain/brand/service/RecentSelectBrandService.java index 9be2bbe3..7de739c2 100644 --- a/src/main/java/com/sluv/server/domain/brand/service/RecentSelectBrandService.java +++ b/src/main/java/com/sluv/server/domain/brand/service/RecentSelectBrandService.java @@ -6,6 +6,7 @@ import com.sluv.server.domain.brand.entity.RecentSelectBrand; import com.sluv.server.domain.brand.exception.BrandNotFoundException; import com.sluv.server.domain.brand.exception.NewBrandNotFoundException; +import com.sluv.server.domain.brand.mapper.RecentSelectBrandMapper; import com.sluv.server.domain.brand.repository.BrandRepository; import com.sluv.server.domain.brand.repository.NewBrandRepository; import com.sluv.server.domain.brand.repository.RecentSelectBrandRepository; @@ -21,6 +22,8 @@ public class RecentSelectBrandService { private final NewBrandRepository newBrandRepository; private final RecentSelectBrandRepository recentSelectBrandRepository; + private final RecentSelectBrandMapper recentSelectBrandMapper; + public void postRecentSelectBrand(User user, RecentSelectBrandReqDto dto){ Brand brand = dto.getBrandId() != null ? brandRepository.findById(dto.getBrandId()) @@ -33,11 +36,7 @@ public void postRecentSelectBrand(User user, RecentSelectBrandReqDto dto){ : null; recentSelectBrandRepository.save( - RecentSelectBrand.builder() - .brand(brand) - .newBrand(newBrand) - .user(user) - .build() + recentSelectBrandMapper.toEntity(brand, newBrand, user) ); } diff --git a/src/main/java/com/sluv/server/domain/item/service/ItemService.java b/src/main/java/com/sluv/server/domain/item/service/ItemService.java index c62a4d04..56ea0439 100644 --- a/src/main/java/com/sluv/server/domain/item/service/ItemService.java +++ b/src/main/java/com/sluv/server/domain/item/service/ItemService.java @@ -5,6 +5,7 @@ import com.sluv.server.domain.brand.entity.NewBrand; import com.sluv.server.domain.brand.exception.BrandNotFoundException; import com.sluv.server.domain.brand.exception.NewBrandNotFoundException; +import com.sluv.server.domain.brand.mapper.BrandMapper; import com.sluv.server.domain.brand.repository.BrandRepository; import com.sluv.server.domain.brand.repository.NewBrandRepository; import com.sluv.server.domain.celeb.dto.CelebSearchResDto; @@ -63,6 +64,8 @@ public class ItemService { private final ClosetRepository closetRepository; private final ItemScrapRepository itemScrapRepository; + private final BrandMapper brandMapper; + @Transactional public ItemPostResDto postItem(User user, ItemPostReqDto reqDto) { @@ -262,12 +265,13 @@ public ItemDetailResDto getItemDetail(User user, Long itemId) { // 4. Brand BrandSearchResDto brand = item.getBrand() != null ? - BrandSearchResDto.builder() - .id(item.getBrand().getId()) - .brandKr(item.getBrand().getBrandKr()) - .brandEn(item.getBrand().getBrandEn()) - .brandImgUrl(item.getBrand().getBrandImgUrl()) - .build() + brandMapper.toBrandSearchResDto(item.getBrand()) +// BrandSearchResDto.builder() +// .id(item.getBrand().getId()) +// .brandKr(item.getBrand().getBrandKr()) +// .brandEn(item.getBrand().getBrandEn()) +// .brandImgUrl(item.getBrand().getBrandImgUrl()) +// .build() : null; String newBrand = item.getNewBrand() != null ? diff --git a/src/main/java/com/sluv/server/domain/item/service/TempItemService.java b/src/main/java/com/sluv/server/domain/item/service/TempItemService.java index 01492206..5cc1c8a3 100644 --- a/src/main/java/com/sluv/server/domain/item/service/TempItemService.java +++ b/src/main/java/com/sluv/server/domain/item/service/TempItemService.java @@ -5,6 +5,7 @@ import com.sluv.server.domain.brand.entity.NewBrand; import com.sluv.server.domain.brand.exception.BrandNotFoundException; import com.sluv.server.domain.brand.exception.NewBrandNotFoundException; +import com.sluv.server.domain.brand.mapper.NewBrandMapper; import com.sluv.server.domain.brand.repository.BrandRepository; import com.sluv.server.domain.brand.repository.NewBrandRepository; import com.sluv.server.domain.celeb.dto.CelebDto; @@ -55,6 +56,9 @@ public class TempItemService { private final NewCelebRepository newCelebRepository; + private final NewBrandMapper newBrandMapper; + + @Transactional public Long postTempItem(User user, TempItemPostReqDto reqDto) { Celeb celeb = reqDto.getCelebId() != null ? celebRepository.findById(reqDto.getCelebId()) @@ -223,10 +227,7 @@ public PaginationCountResDto getTempItemList(User user, Pageable ) .newBrand( tempItem.getNewBrand() != null - ? NewBrandPostResDto.builder() - .newBrandId(tempItem.getNewBrand().getId()) - .newBrandName(tempItem.getNewBrand().getBrandName()) - .build() + ? newBrandMapper.toNewBrandPostResDto(tempItem.getNewBrand()) :null ) .updatedAt(tempItem.getUpdatedAt())