From b38211176ba46aeb3c1dfc9f2688771ce9355c39 Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:08:54 +0900 Subject: [PATCH 1/9] =?UTF-8?q?refactor=20:=20Scrap=20-=20limjang=20?= =?UTF-8?q?=EA=B4=80=EA=B3=84=20=EB=B3=80=EA=B2=BD=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/th/juinjang/model/entity/Limjang.java | 20 +++---------------- 1 file changed, 3 insertions(+), 17 deletions(-) diff --git a/src/main/java/umc/th/juinjang/model/entity/Limjang.java b/src/main/java/umc/th/juinjang/model/entity/Limjang.java index a3d24dd..f56bab9 100644 --- a/src/main/java/umc/th/juinjang/model/entity/Limjang.java +++ b/src/main/java/umc/th/juinjang/model/entity/Limjang.java @@ -81,7 +81,7 @@ public class Limjang extends BaseEntity { @OneToMany(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) private List answerList = new ArrayList<>(); - @OneToOne(fetch = FetchType.LAZY, mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) + @OneToOne(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) private Report report; @OneToMany(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) @@ -91,8 +91,8 @@ public class Limjang extends BaseEntity { @BatchSize(size = 100) private List imageList = new ArrayList<>(); - @OneToOne(fetch = FetchType.LAZY, mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) - private Scrap scrap; + @OneToMany(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) + private List scrap = new ArrayList<>(); @Column(name = "record_count") @ColumnDefault("0") //default 0 @@ -106,15 +106,6 @@ public void saveMemberAndPrice(Member member, LimjangPrice limjangPrice){ this.memberId = member; } - public void addScrap(Scrap scrap) { - this.scrap = scrap; - scrap.saveLimjang(this); - } - - public void removeScrap(){ - this.scrap = null; - } - public void updateLimjang(String address, String addressDetail, String nickname, LimjangPriceType priceType){ this.address = address; this.addressDetail = addressDetail; @@ -128,9 +119,4 @@ public void updateMemo(String memo){ public void saveImages(Image image){ this.imageList.add(image); } - - public void saveReport(Report report){ - this.report = report; - } - } \ No newline at end of file From b5ea432e629700042ccfce6b95fd08e4b6e4145e Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:09:26 +0900 Subject: [PATCH 2/9] =?UTF-8?q?refactor=20:=20=EC=9E=84=EC=9E=A5=20?= =?UTF-8?q?=EA=B2=80=EC=83=89=20responseDTO=20=EB=B3=80=EA=B2=BD=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../limjang/LimjangTotalListConverter.java | 58 ------------------- .../LimjangsGetByKeywordResponse.java | 40 +++++++++++++ 2 files changed, 40 insertions(+), 58 deletions(-) delete mode 100644 src/main/java/umc/th/juinjang/converter/limjang/LimjangTotalListConverter.java create mode 100644 src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetByKeywordResponse.java diff --git a/src/main/java/umc/th/juinjang/converter/limjang/LimjangTotalListConverter.java b/src/main/java/umc/th/juinjang/converter/limjang/LimjangTotalListConverter.java deleted file mode 100644 index 5f7adc0..0000000 --- a/src/main/java/umc/th/juinjang/converter/limjang/LimjangTotalListConverter.java +++ /dev/null @@ -1,58 +0,0 @@ -package umc.th.juinjang.converter.limjang; - -import static umc.th.juinjang.service.LimjangService.LimjangPriceBridge.makePriceListVersion2; - -import java.util.Comparator; -import java.util.List; -import java.util.Optional; -import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO; -import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO.LimjangListDto; -import umc.th.juinjang.model.entity.Image; -import umc.th.juinjang.model.entity.Limjang; -import umc.th.juinjang.model.entity.LimjangPrice; - - -public class LimjangTotalListConverter { - - public static LimjangTotalListResponseDTO.TotalListDto toLimjangTotalList( - List limjangList - ) { - - List limjangListDto = limjangList.stream() - .map(limjang -> LimjangTotalListConverter.toLimjangList(limjang, limjang.getLimjangPrice())) - .toList(); - - return LimjangTotalListResponseDTO.TotalListDto.builder() - .limjangList(limjangListDto) - .build(); - } - - public static LimjangListDto toLimjangList( - Limjang limjang, LimjangPrice limjangPrice) { - - int limitImageListSize = limjang.getScrap() == null ? 1 : 3; - - List urlList = limjang.getImageList().stream() - .sorted(Comparator.comparing(Image::getCreatedAt)) // image를 createdAt 기준으로 정렬 - .map(Image::getImageUrl) - .limit(limitImageListSize) - .toList(); - - List priceList = makePriceListVersion2(limjang.getPriceType(), limjang.getPurpose(), limjangPrice); - - return LimjangListDto.builder() - .limjangId(limjang.getLimjangId()) - .images(urlList) - .nickname(limjang.getNickname()) - .isScraped(limjang.getScrap() != null) - .purposeCode(limjang.getPurpose().getValue()) - .priceType(limjang.getPriceType().getValue()) - .priceList(priceList) - .totalAverage(Optional.ofNullable(limjang.getReport()) - .flatMap(report -> Optional.ofNullable(report.getTotalRate()) - .map(Object::toString)) - .orElse(null)) - .address(limjang.getAddress()) - .build(); - } -} diff --git a/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetByKeywordResponse.java b/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetByKeywordResponse.java new file mode 100644 index 0000000..8c36bb8 --- /dev/null +++ b/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetByKeywordResponse.java @@ -0,0 +1,40 @@ +package umc.th.juinjang.model.dto.limjang.response; + +import static umc.th.juinjang.service.LimjangService.LimjangPriceBridge.makePriceListVersion2; + +import java.util.List; +import java.util.Map; +import umc.th.juinjang.model.entity.Image; +import umc.th.juinjang.model.entity.Limjang; + +public record LimjangsGetByKeywordResponse(List limjangList) { + record LimjangByKeywordResponse( + long limjangId, + List images, + int purposeCode, + boolean isScraped, + String nickname, + int priceType, + List priceList, + String totalAverage, + String address + ) { + static LimjangByKeywordResponse of(Limjang limjang, boolean isScraped) { + return new LimjangByKeywordResponse( + limjang.getLimjangId(), + limjang.getImageList().stream().map(Image::getImageUrl).toList(), + limjang.getPurpose().getValue(), + isScraped, + limjang.getNickname(), + limjang.getPriceType().getValue(), + makePriceListVersion2(limjang.getPriceType(), limjang.getPurpose(), limjang.getLimjangPrice()), + limjang.getReport() == null ? null : limjang.getReport().getTotalRate().toString(), + limjang.getAddress() + ); + } + } + + public static LimjangsGetByKeywordResponse of(List limjangList, Map mapLimjangToScrapStatus) { + return new LimjangsGetByKeywordResponse(limjangList.stream().map(it -> LimjangByKeywordResponse.of(it, mapLimjangToScrapStatus.get(it.getLimjangId()))).toList()); + } +} From 63211f8d162d420da853dc7c427189ee1b01c1f9 Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:09:42 +0900 Subject: [PATCH 3/9] =?UTF-8?q?refactor=20:=20=ED=95=A8=EC=88=98=ED=99=94?= =?UTF-8?q?=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../limjang/LimjangQueryDslRepositoryImpl.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java index 3b4d7b9..5e06944 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java @@ -33,14 +33,13 @@ public LimjangQueryDslRepositoryImpl(EntityManager em) { @Override public List searchLimjangs(Member member, String keyword) { - String rKeyword = keyword.replaceAll(" ", ""); - + String rKeyword = removeKeywordBlank(keyword); return queryFactory .selectFrom(limjang) .leftJoin(limjang.report, report).fetchJoin() - .leftJoin(limjang.scrap, scrap).fetchJoin() .leftJoin(limjang.limjangPrice, limjangPrice).fetchJoin() .leftJoin(limjang.imageList, image).fetchJoin() + .where(limjang.deleted.isFalse()) .where(limjang.memberId.eq(member), keywordOf( removeBlank(limjang.nickname).containsIgnoreCase(rKeyword), @@ -50,6 +49,10 @@ public List searchLimjangs(Member member, String keyword) { .fetch(); } + private String removeKeywordBlank(String keyword) { + return keyword.replaceAll(" ", ""); + } + @Override public Limjang findByIdWithLimjangPrice(long memberId, long limjangId) { return queryFactory @@ -69,6 +72,7 @@ public List findAllByMemberAndOrderByParam(Member member, LimjangSortOp .leftJoin(limjang.report, report).fetchJoin() .leftJoin(limjang.scrap, scrap).fetchJoin() .where(limjang.memberId.eq(member)) + .where(limjang.deleted.isFalse()) .orderBy(getOrderByLimjangSortOptions(sort)) .fetch(); } From fdf4e9cb95063e5f3d408838d97d62af02f6aeee Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:10:59 +0900 Subject: [PATCH 4/9] =?UTF-8?q?refactor=20:=20=EC=9E=84=EC=9E=A5=20-=20?= =?UTF-8?q?=EC=8A=A4=ED=81=AC=EB=9E=A9=20=EB=A7=A4=ED=95=91=ED=95=98?= =?UTF-8?q?=EB=8A=94=20Map=20=EA=B5=AC=ED=98=84,=20=EA=B4=80=EA=B3=84=20?= =?UTF-8?q?=EB=B0=94=EB=80=90=20=EA=B2=83=20=EB=B0=98=EC=98=81=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../umc/th/juinjang/model/entity/Scrap.java | 4 +-- .../repository/limjang/LimjangRepository.java | 3 +- .../repository/limjang/ScrapRepository.java | 7 ++-- .../LimjangService/LimjangQueryService.java | 3 +- .../LimjangQueryServiceImpl.java | 36 +++++++++++++------ 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/umc/th/juinjang/model/entity/Scrap.java b/src/main/java/umc/th/juinjang/model/entity/Scrap.java index 3621a41..2a001b1 100644 --- a/src/main/java/umc/th/juinjang/model/entity/Scrap.java +++ b/src/main/java/umc/th/juinjang/model/entity/Scrap.java @@ -8,7 +8,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; -import jakarta.persistence.OneToOne; +import jakarta.persistence.ManyToOne; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -29,7 +29,7 @@ public class Scrap extends BaseEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long scrapId; - @OneToOne(fetch = FetchType.LAZY) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "limjang_id") private Limjang limjangId; diff --git a/src/main/java/umc/th/juinjang/repository/limjang/LimjangRepository.java b/src/main/java/umc/th/juinjang/repository/limjang/LimjangRepository.java index 0da9f3d..0432ece 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/LimjangRepository.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/LimjangRepository.java @@ -30,7 +30,7 @@ public interface LimjangRepository extends JpaRepository, Limjang void softDeleteByIds(@Param("ids") List ids); @Modifying - @Query(value ="DELETE FROM limjang l WHERE l.deleted = true AND l.updated_at < :dateTime", nativeQuery = true) + @Query(value = "DELETE FROM limjang l WHERE l.deleted = true AND l.updated_at < :dateTime", nativeQuery = true) void hardDelete(@Param("dateTime") LocalDateTime dateTime); // 가장 최근에 update된 5개 순서대로 @@ -57,4 +57,3 @@ public interface LimjangRepository extends JpaRepository, Limjang Optional findByLimjangIdAndMemberIdWithLimjangPriceAndDeletedIsFalse(@Param("id") Long id, @Param("member") Member member); } - diff --git a/src/main/java/umc/th/juinjang/repository/limjang/ScrapRepository.java b/src/main/java/umc/th/juinjang/repository/limjang/ScrapRepository.java index 0bcb047..d4a4e6d 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/ScrapRepository.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/ScrapRepository.java @@ -1,5 +1,6 @@ package umc.th.juinjang.repository.limjang; +import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -11,8 +12,6 @@ import umc.th.juinjang.model.entity.Scrap; public interface ScrapRepository extends JpaRepository { - Scrap findScrapByLimjangId(Limjang limjangId); - @Query("select s from Scrap s where s.limjangId = :limjang") Optional serachByLimjang(@Param("limjang")Limjang limjang); @@ -23,5 +22,7 @@ public interface ScrapRepository extends JpaRepository { @Transactional @Modifying @Query(value = "DELETE FROM scrap s WHERE s.limjang_id = :limjangId", nativeQuery = true) - void deleteByLimjangId(@Param("limjangId") Long limjangId);; + void deleteByLimjangId(@Param("limjangId") Long limjangId); + + List findAllByLimjangIdIn(List limjangs); } \ No newline at end of file diff --git a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryService.java b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryService.java index d626823..cace7b9 100644 --- a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryService.java +++ b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryService.java @@ -4,6 +4,7 @@ import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO; import umc.th.juinjang.model.dto.limjang.enums.LimjangSortOptions; +import umc.th.juinjang.model.dto.limjang.response.LimjangsGetByKeywordResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsMainGetResponse; import umc.th.juinjang.model.entity.Member; @@ -14,7 +15,7 @@ public interface LimjangQueryService { LimjangsMainGetResponse getLimjangsMain(Member member); - LimjangTotalListResponseDTO.TotalListDto getLimjangSearchList(Member member, String keyword); + LimjangsGetByKeywordResponse getLimjangSearchList(Member member, String keyword); LimjangDetailResponseDTO.DetailDto getLimjangDetail(Long limjangId); } diff --git a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java index 9ada514..2134c33 100644 --- a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java +++ b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java @@ -1,6 +1,10 @@ package umc.th.juinjang.service.LimjangService; +import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -9,9 +13,8 @@ import umc.th.juinjang.apiPayload.exception.handler.LimjangHandler; import umc.th.juinjang.apiPayload.exception.handler.MemberHandler; import umc.th.juinjang.converter.limjang.LimjangDetailConverter; -import umc.th.juinjang.converter.limjang.LimjangTotalListConverter; import umc.th.juinjang.model.dto.limjang.response.LimjangDetailResponseDTO.DetailDto; -import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO; +import umc.th.juinjang.model.dto.limjang.response.LimjangsGetByKeywordResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsMainGetResponse; import umc.th.juinjang.model.entity.Limjang; @@ -19,6 +22,7 @@ import umc.th.juinjang.model.dto.limjang.enums.LimjangSortOptions; import umc.th.juinjang.repository.limjang.LimjangRepository; import umc.th.juinjang.repository.limjang.MemberRepository; +import umc.th.juinjang.repository.limjang.ScrapRepository; @Slf4j @Service @@ -27,29 +31,39 @@ public class LimjangQueryServiceImpl implements LimjangQueryService{ private final LimjangRepository limjangRepository; private final MemberRepository memberRepository; + private final ScrapRepository scrapRepository; @Override @Transactional(readOnly = true) public LimjangsGetResponse getLimjangTotalList(Member member, LimjangSortOptions sort) { - return LimjangsGetResponse.of(limjangRepository.findAllByMemberAndOrderByParam(findMemberById(member), sort)); + return LimjangsGetResponse.of(limjangRepository.findAllByMemberAndOrderByParam(checkMemberExist(member), sort)); } @Override @Transactional(readOnly = true) public LimjangsMainGetResponse getLimjangsMain(final Member member) { - return LimjangsMainGetResponse.of(limjangRepository.findMainScreenContentsLimjang(findMemberById(member))); + return LimjangsMainGetResponse.of(limjangRepository.findMainScreenContentsLimjang( + checkMemberExist(member))); } @Override @Transactional(readOnly = true) - public LimjangTotalListResponseDTO.TotalListDto getLimjangSearchList(Member member, String keyword) { - - Member findMember = memberRepository.findById(member.getMemberId()) - .orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); + public LimjangsGetByKeywordResponse getLimjangSearchList(Member member, String keyword) { + checkMemberExist(member); + List limjangList = limjangRepository.searchLimjangs(member, keyword).stream().toList(); + return LimjangsGetByKeywordResponse.of(limjangList, mapLimjangToScrapStatus(limjangList)); + } - List findLimjangListByKeyword = limjangRepository.searchLimjangs(findMember, keyword).stream().toList(); + private Map mapLimjangToScrapStatus(List limjangList) { + Set limjangIdsInScrap = getLimjangIdsInScrap(limjangList); + return limjangList.stream().collect(Collectors.toMap( + Limjang::getLimjangId, + it -> limjangIdsInScrap.contains(it.getLimjangId()) + )); + } - return LimjangTotalListConverter.toLimjangTotalList(findLimjangListByKeyword); + private Set getLimjangIdsInScrap(List limjangList) { + return new HashSet<>(scrapRepository.findAllByLimjangIdIn(limjangList).stream().map(it -> it.getLimjangId().getLimjangId()).toList()); } @Override @@ -62,7 +76,7 @@ public DetailDto getLimjangDetail(Long limjangId) { return LimjangDetailConverter.toDetail(findLimjang, findLimjang.getLimjangPrice()); } - private Member findMemberById(Member member) { + private Member checkMemberExist(Member member) { return memberRepository.findById(member.getMemberId()).orElseThrow(() -> new MemberHandler(ErrorStatus.MEMBER_NOT_FOUND)); } } From 2562a4eb6a22c0669b34571f6f714ce2c9908bd8 Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:11:16 +0900 Subject: [PATCH 5/9] =?UTF-8?q?refactor=20:=20responseDTO=20=EB=B0=94?= =?UTF-8?q?=EB=80=90=20=EA=B2=83=20=EB=B0=98=EC=98=81=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/umc/th/juinjang/controller/LimjangController.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/java/umc/th/juinjang/controller/LimjangController.java b/src/main/java/umc/th/juinjang/controller/LimjangController.java index 93b0d48..d217601 100644 --- a/src/main/java/umc/th/juinjang/controller/LimjangController.java +++ b/src/main/java/umc/th/juinjang/controller/LimjangController.java @@ -25,6 +25,7 @@ import umc.th.juinjang.model.dto.limjang.response.LimjangDetailResponseDTO; import umc.th.juinjang.model.dto.limjang.response.LimjangPostResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangTotalListResponseDTO; +import umc.th.juinjang.model.dto.limjang.response.LimjangsGetByKeywordResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse; import umc.th.juinjang.model.dto.limjang.response.LimjangsMainGetResponse; import umc.th.juinjang.model.entity.Member; @@ -74,11 +75,7 @@ public ApiResponse deleteLimjang(@RequestBody @Valid LimjangsDeleteRequest delet @CrossOrigin @Operation(summary = "임장 검색", description = "임장 게시글을 검색하는 api입니다. 집별명, 일반주소, 상세주소로 검색이 가능합니다.") @GetMapping("/{keyword}") - public ApiResponse searchLimjangs( - @AuthenticationPrincipal Member member, - @PathVariable(name = "keyword") @Valid String keyword - ) { - + public ApiResponse searchLimjangs(@AuthenticationPrincipal Member member, @PathVariable(name = "keyword") String keyword) { return ApiResponse.onSuccess(limjangQueryService.getLimjangSearchList(member, keyword)); } From 125c728007c5b059a8bb0a7bbaf8ad8d3789cc34 Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:45:36 +0900 Subject: [PATCH 6/9] =?UTF-8?q?refactor=20:=20=EC=9E=84=EC=9E=A5=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20=EC=BF=BC=EB=A6=AC=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/limjang/LimjangQueryDslRepositoryImpl.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java index 5e06944..80a3622 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java @@ -70,7 +70,7 @@ public List findAllByMemberAndOrderByParam(Member member, LimjangSortOp .selectFrom(limjang) .join(limjang.limjangPrice, limjangPrice).fetchJoin() .leftJoin(limjang.report, report).fetchJoin() - .leftJoin(limjang.scrap, scrap).fetchJoin() + .leftJoin(limjang.imageList, image).fetchJoin() .where(limjang.memberId.eq(member)) .where(limjang.deleted.isFalse()) .orderBy(getOrderByLimjangSortOptions(sort)) @@ -85,9 +85,8 @@ private OrderSpecifier[] getOrderByLimjangSortOptions(LimjangSortOptions sort) { orders.add(new OrderSpecifier<>(DESC, report.totalRate.coalesce(0f), OrderSpecifier.NullHandling.NullsLast)); orders.add(new OrderSpecifier<>(DESC, limjang.createdAt)); } - case CREATED -> new OrderSpecifier<>(DESC, limjang.createdAt); + case CREATED -> orders.add(new OrderSpecifier<>(DESC, limjang.createdAt)); } - return orders.toArray(new OrderSpecifier[orders.size()]); } From d26127d86f60dc85917c91ca335c987d040164fb Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Wed, 9 Oct 2024 17:46:04 +0900 Subject: [PATCH 7/9] =?UTF-8?q?refactor=20:=20=EC=9E=84=EC=9E=A5=20?= =?UTF-8?q?=EC=A0=84=EC=B2=B4=20=EC=A1=B0=ED=9A=8C=20=EC=BB=A8=EB=B2=84?= =?UTF-8?q?=ED=84=B0=20=EC=A0=9C=EA=B1=B0,=20DTO=20=EC=88=98=EC=A0=95=20#2?= =?UTF-8?q?41?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../limjang/LimjangsGetResponseConverter.java | 40 ------------------- .../limjang/response/LimjangsGetResponse.java | 34 +++++++++++----- .../LimjangQueryServiceImpl.java | 7 ++-- 3 files changed, 29 insertions(+), 52 deletions(-) delete mode 100644 src/main/java/umc/th/juinjang/converter/limjang/LimjangsGetResponseConverter.java diff --git a/src/main/java/umc/th/juinjang/converter/limjang/LimjangsGetResponseConverter.java b/src/main/java/umc/th/juinjang/converter/limjang/LimjangsGetResponseConverter.java deleted file mode 100644 index 9d16566..0000000 --- a/src/main/java/umc/th/juinjang/converter/limjang/LimjangsGetResponseConverter.java +++ /dev/null @@ -1,40 +0,0 @@ -package umc.th.juinjang.converter.limjang; - -import static umc.th.juinjang.service.LimjangService.LimjangPriceBridge.makePriceListVersion2; - -import java.util.Comparator; -import java.util.List; -import java.util.Optional; -import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse; -import umc.th.juinjang.model.dto.limjang.response.LimjangsGetResponse.LimjangsResponse; -import umc.th.juinjang.model.entity.Image; -import umc.th.juinjang.model.entity.Limjang; -import umc.th.juinjang.model.entity.LimjangPrice; - -public class LimjangsGetResponseConverter { - - public static LimjangsGetResponse.LimjangsResponse convertToLimjangsResponse(Limjang limjang, LimjangPrice limjangPrice) { - return LimjangsResponse.builder() - .limjangId(limjang.getLimjangId()) - .images(getUrlListByScrap(limjang)) - .nickname(limjang.getNickname()) - .isScraped(limjang.getScrap() != null) - .purposeCode(limjang.getPurpose().getValue()) - .priceType(limjang.getPriceType().getValue()) - .priceList(makePriceListVersion2(limjang.getPriceType(), limjang.getPurpose(), limjangPrice)) - .totalAverage(Optional.ofNullable(limjang.getReport()) - .flatMap(report -> Optional.ofNullable(report.getTotalRate()) - .map(Object::toString)) - .orElse(null)) - .address(limjang.getAddress()) - .build(); - } - - private static List getUrlListByScrap(Limjang limjang) { - return limjang.getImageList().stream() - .sorted(Comparator.comparing(Image::getCreatedAt)) - .map(Image::getImageUrl) - .limit(limjang.getScrap() == null ? 1 : 3) - .toList(); - } -} \ No newline at end of file diff --git a/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetResponse.java b/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetResponse.java index e7934a9..e629ffd 100644 --- a/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetResponse.java +++ b/src/main/java/umc/th/juinjang/model/dto/limjang/response/LimjangsGetResponse.java @@ -1,16 +1,18 @@ package umc.th.juinjang.model.dto.limjang.response; +import static umc.th.juinjang.service.LimjangService.LimjangPriceBridge.makePriceListVersion2; + +import java.util.Comparator; import java.util.List; +import java.util.Map; import lombok.Builder; -import umc.th.juinjang.converter.limjang.LimjangsGetResponseConverter; +import umc.th.juinjang.model.entity.Image; import umc.th.juinjang.model.entity.Limjang; import umc.th.juinjang.model.entity.LimjangPrice; -public record LimjangsGetResponse( - List limjangList -) { +public record LimjangsGetResponse(List limjangList) { @Builder - public record LimjangsResponse( + record LimjangsResponse( long limjangId, List images, int purposeCode, @@ -20,12 +22,26 @@ public record LimjangsResponse( List priceList, String totalAverage, String address) { - public static LimjangsResponse of(Limjang limjang, LimjangPrice limjangPrice) { - return LimjangsGetResponseConverter.convertToLimjangsResponse(limjang, limjangPrice); + static LimjangsResponse of(Limjang limjang, LimjangPrice limjangPrice, boolean isScraped) { + return LimjangsResponse.builder() + .limjangId(limjang.getLimjangId()) + .images(getUrlListByScrap(limjang, isScraped)) + .nickname(limjang.getNickname()) + .isScraped(isScraped) + .purposeCode(limjang.getPurpose().getValue()) + .priceType(limjang.getPriceType().getValue()) + .priceList(makePriceListVersion2(limjang.getPriceType(), limjang.getPurpose(), limjangPrice)) + .totalAverage(limjang.getReport() == null ? null : limjang.getReport().getTotalRate().toString()) + .address(limjang.getAddress()) + .build(); } } - public static LimjangsGetResponse of(final List limjangList) { - return new LimjangsGetResponse(limjangList.stream().map(limjang -> LimjangsResponse.of(limjang, limjang.getLimjangPrice())).toList()); + public static LimjangsGetResponse of(final List limjangList, Map mapLimjangToScrapStatus) { + return new LimjangsGetResponse(limjangList.stream().map(limjang -> LimjangsResponse.of(limjang, limjang.getLimjangPrice(), mapLimjangToScrapStatus.get(limjang.getLimjangId()))).toList()); + } + + private static List getUrlListByScrap(Limjang limjang, boolean isScraped) { + return limjang.getImageList().stream().map(Image::getImageUrl).limit(!isScraped ? 1 : 3).toList(); } } diff --git a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java index 2134c33..ab5b248 100644 --- a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java +++ b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java @@ -36,14 +36,15 @@ public class LimjangQueryServiceImpl implements LimjangQueryService{ @Override @Transactional(readOnly = true) public LimjangsGetResponse getLimjangTotalList(Member member, LimjangSortOptions sort) { - return LimjangsGetResponse.of(limjangRepository.findAllByMemberAndOrderByParam(checkMemberExist(member), sort)); + checkMemberExist(member); + List limjangList = limjangRepository.findAllByMemberAndOrderByParam(member, sort); + return LimjangsGetResponse.of(limjangList, mapLimjangToScrapStatus(limjangList)); } @Override @Transactional(readOnly = true) public LimjangsMainGetResponse getLimjangsMain(final Member member) { - return LimjangsMainGetResponse.of(limjangRepository.findMainScreenContentsLimjang( - checkMemberExist(member))); + return LimjangsMainGetResponse.of(limjangRepository.findMainScreenContentsLimjang(checkMemberExist(member))); } @Override From 9a05dfc7edc50a3a8802e622f952d14ac83b354f Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Thu, 10 Oct 2024 20:11:18 +0900 Subject: [PATCH 8/9] =?UTF-8?q?refactor=20:=20scrap=20=EC=96=91=EB=B0=A9?= =?UTF-8?q?=ED=96=A5=20=EB=A7=A4=ED=95=91=20=EC=A0=9C=EA=B1=B0=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/umc/th/juinjang/model/entity/Limjang.java | 3 --- src/main/java/umc/th/juinjang/model/entity/Scrap.java | 9 ++------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/src/main/java/umc/th/juinjang/model/entity/Limjang.java b/src/main/java/umc/th/juinjang/model/entity/Limjang.java index f56bab9..d967f7c 100644 --- a/src/main/java/umc/th/juinjang/model/entity/Limjang.java +++ b/src/main/java/umc/th/juinjang/model/entity/Limjang.java @@ -91,9 +91,6 @@ public class Limjang extends BaseEntity { @BatchSize(size = 100) private List imageList = new ArrayList<>(); - @OneToMany(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true) - private List scrap = new ArrayList<>(); - @Column(name = "record_count") @ColumnDefault("0") //default 0 private int recordCount; diff --git a/src/main/java/umc/th/juinjang/model/entity/Scrap.java b/src/main/java/umc/th/juinjang/model/entity/Scrap.java index 2a001b1..5aa4680 100644 --- a/src/main/java/umc/th/juinjang/model/entity/Scrap.java +++ b/src/main/java/umc/th/juinjang/model/entity/Scrap.java @@ -1,6 +1,5 @@ package umc.th.juinjang.model.entity; -import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -8,7 +7,7 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; -import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToOne; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -29,11 +28,7 @@ public class Scrap extends BaseEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long scrapId; - @ManyToOne(fetch = FetchType.LAZY) + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "limjang_id") private Limjang limjangId; - - public void saveLimjang(Limjang limjang){ - this.limjangId = limjang; - } } From 51928c0ea0f2f5c0e6d63e803504bc1e10f3ebd6 Mon Sep 17 00:00:00 2001 From: PicturePark1101 Date: Thu, 10 Oct 2024 20:11:28 +0900 Subject: [PATCH 9/9] =?UTF-8?q?refactor=20:=20=EC=BF=BC=EB=A6=AC=EB=AA=85?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=20#241?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../limjang/LimjangQueryDslRepository.java | 11 ++----- .../LimjangQueryDslRepositoryImpl.java | 30 ++----------------- .../LimjangQueryServiceImpl.java | 4 +-- .../limjang/LimjangQuerydslTest.java | 8 ++--- 4 files changed, 9 insertions(+), 44 deletions(-) diff --git a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepository.java b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepository.java index 6ece740..aee0fef 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepository.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepository.java @@ -1,22 +1,15 @@ package umc.th.juinjang.repository.limjang; import java.util.List; -import org.springframework.data.domain.Pageable; import umc.th.juinjang.model.dto.limjang.enums.LimjangSortOptions; import umc.th.juinjang.model.entity.Limjang; import umc.th.juinjang.model.entity.Member; public interface LimjangQueryDslRepository { - List searchLimjangs(Member member, String keyword); - - // List findTop5ByMemberIdOrderByUpdatedAtDesc(Member member); + List searchLimjangsWhereDeletedIsFalse(Member member, String keyword); List findMainScreenContentsLimjang(Member member); - Limjang findByIdWithLimjangPrice(long memberId, long limjangId); - - List findAllLimjangs(Member member); - - List findAllByMemberAndOrderByParam(Member member, LimjangSortOptions sort); + List findAllByMemberAndDeletedIsFalseOrderByParam(Member member, LimjangSortOptions sort); } diff --git a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java index 80a3622..acb39ea 100644 --- a/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java +++ b/src/main/java/umc/th/juinjang/repository/limjang/LimjangQueryDslRepositoryImpl.java @@ -32,12 +32,12 @@ public LimjangQueryDslRepositoryImpl(EntityManager em) { } @Override - public List searchLimjangs(Member member, String keyword) { + public List searchLimjangsWhereDeletedIsFalse(Member member, String keyword) { String rKeyword = removeKeywordBlank(keyword); return queryFactory .selectFrom(limjang) .leftJoin(limjang.report, report).fetchJoin() - .leftJoin(limjang.limjangPrice, limjangPrice).fetchJoin() + .join(limjang.limjangPrice, limjangPrice).fetchJoin() .leftJoin(limjang.imageList, image).fetchJoin() .where(limjang.deleted.isFalse()) .where(limjang.memberId.eq(member), @@ -53,19 +53,7 @@ private String removeKeywordBlank(String keyword) { return keyword.replaceAll(" ", ""); } - @Override - public Limjang findByIdWithLimjangPrice(long memberId, long limjangId) { - return queryFactory - .selectFrom(limjang) - .join(limjang.limjangPrice, limjangPrice).fetchJoin() - .leftJoin(limjang.report, report).fetchJoin() - .leftJoin(limjang.scrap, scrap).fetchJoin() - .where(limjang.memberId.memberId.eq(memberId)) - .where(limjang.limjangId.eq(limjangId)) - .fetchOne(); - } - - public List findAllByMemberAndOrderByParam(Member member, LimjangSortOptions sort) { + public List findAllByMemberAndDeletedIsFalseOrderByParam(Member member, LimjangSortOptions sort) { return queryFactory .selectFrom(limjang) .join(limjang.limjangPrice, limjangPrice).fetchJoin() @@ -113,16 +101,4 @@ public List findMainScreenContentsLimjang(Member member) { .limit(5) .fetch(); } - - @Override - public List findAllLimjangs(Member member) { - return queryFactory - .selectFrom(limjang) - .leftJoin(limjang.report, report).fetchJoin() - .leftJoin(limjang.scrap, scrap).fetchJoin() - .leftJoin(limjang.limjangPrice, limjangPrice).fetchJoin() - .leftJoin(limjang.imageList, image).fetchJoin() - .where(limjang.memberId.eq(member)) - .fetch(); - } } diff --git a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java index ab5b248..54b9bd2 100644 --- a/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java +++ b/src/main/java/umc/th/juinjang/service/LimjangService/LimjangQueryServiceImpl.java @@ -37,7 +37,7 @@ public class LimjangQueryServiceImpl implements LimjangQueryService{ @Transactional(readOnly = true) public LimjangsGetResponse getLimjangTotalList(Member member, LimjangSortOptions sort) { checkMemberExist(member); - List limjangList = limjangRepository.findAllByMemberAndOrderByParam(member, sort); + List limjangList = limjangRepository.findAllByMemberAndDeletedIsFalseOrderByParam(member, sort); return LimjangsGetResponse.of(limjangList, mapLimjangToScrapStatus(limjangList)); } @@ -51,7 +51,7 @@ public LimjangsMainGetResponse getLimjangsMain(final Member member) { @Transactional(readOnly = true) public LimjangsGetByKeywordResponse getLimjangSearchList(Member member, String keyword) { checkMemberExist(member); - List limjangList = limjangRepository.searchLimjangs(member, keyword).stream().toList(); + List limjangList = limjangRepository.searchLimjangsWhereDeletedIsFalse(member, keyword); return LimjangsGetByKeywordResponse.of(limjangList, mapLimjangToScrapStatus(limjangList)); } diff --git a/src/test/java/umc/th/juinjang/repository/limjang/LimjangQuerydslTest.java b/src/test/java/umc/th/juinjang/repository/limjang/LimjangQuerydslTest.java index 5e52eba..3d344dd 100644 --- a/src/test/java/umc/th/juinjang/repository/limjang/LimjangQuerydslTest.java +++ b/src/test/java/umc/th/juinjang/repository/limjang/LimjangQuerydslTest.java @@ -11,11 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.context.annotation.Import; -import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ActiveProfiles; -import org.springframework.transaction.annotation.Transactional; -import umc.th.juinjang.apiPayload.code.status.ErrorStatus; -import umc.th.juinjang.apiPayload.exception.handler.LimjangHandler; import umc.th.juinjang.config.TestConfig; import umc.th.juinjang.model.entity.Limjang; import umc.th.juinjang.model.entity.Member; @@ -58,7 +54,7 @@ void testIncludeKeyword() { // when String keyword = "인창"; - List findLimjangs = limjangRepository.searchLimjangs(member, keyword); + List findLimjangs = limjangRepository.searchLimjangsWhereDeletedIsFalse(member, keyword); // then for (int i = 0; i < findLimjangs.size(); i++) { @@ -78,7 +74,7 @@ void testNotIncludeKeyword() { // when String keyword = "인창"; - List findLimjangs = limjangRepository.searchLimjangs(member, keyword); + List findLimjangs = limjangRepository.searchLimjangsWhereDeletedIsFalse(member, keyword); // then assertThat(findLimjangs)