Skip to content

Commit

Permalink
Merge pull request #242 from Juinjang/refactor/#241
Browse files Browse the repository at this point in the history
[refactor/#241] 스크랩 양방향 매핑을 제거하기 위한 리팩토링
  • Loading branch information
PicturePark1101 authored Oct 10, 2024
2 parents 7550af2 + 51928c0 commit aee0f79
Show file tree
Hide file tree
Showing 14 changed files with 115 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -74,11 +75,7 @@ public ApiResponse deleteLimjang(@RequestBody @Valid LimjangsDeleteRequest delet
@CrossOrigin
@Operation(summary = "임장 검색", description = "임장 게시글을 검색하는 api입니다. 집별명, 일반주소, 상세주소로 검색이 가능합니다.")
@GetMapping("/{keyword}")
public ApiResponse<LimjangTotalListResponseDTO.TotalListDto> searchLimjangs(
@AuthenticationPrincipal Member member,
@PathVariable(name = "keyword") @Valid String keyword
) {

public ApiResponse<LimjangsGetByKeywordResponse> searchLimjangs(@AuthenticationPrincipal Member member, @PathVariable(name = "keyword") String keyword) {
return ApiResponse.onSuccess(limjangQueryService.getLimjangSearchList(member, keyword));
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<LimjangByKeywordResponse> limjangList) {
record LimjangByKeywordResponse(
long limjangId,
List<String> images,
int purposeCode,
boolean isScraped,
String nickname,
int priceType,
List<String> 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<Limjang> limjangList, Map<Long, Boolean> mapLimjangToScrapStatus) {
return new LimjangsGetByKeywordResponse(limjangList.stream().map(it -> LimjangByKeywordResponse.of(it, mapLimjangToScrapStatus.get(it.getLimjangId()))).toList());
}
}
Original file line number Diff line number Diff line change
@@ -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<LimjangsResponse> limjangList
) {
public record LimjangsGetResponse(List<LimjangsResponse> limjangList) {
@Builder
public record LimjangsResponse(
record LimjangsResponse(
long limjangId,
List<String> images,
int purposeCode,
Expand All @@ -20,12 +22,26 @@ public record LimjangsResponse(
List<String> 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<Limjang> limjangList) {
return new LimjangsGetResponse(limjangList.stream().map(limjang -> LimjangsResponse.of(limjang, limjang.getLimjangPrice())).toList());
public static LimjangsGetResponse of(final List<Limjang> limjangList, Map<Long, Boolean> mapLimjangToScrapStatus) {
return new LimjangsGetResponse(limjangList.stream().map(limjang -> LimjangsResponse.of(limjang, limjang.getLimjangPrice(), mapLimjangToScrapStatus.get(limjang.getLimjangId()))).toList());
}

private static List<String> getUrlListByScrap(Limjang limjang, boolean isScraped) {
return limjang.getImageList().stream().map(Image::getImageUrl).limit(!isScraped ? 1 : 3).toList();
}
}
19 changes: 1 addition & 18 deletions src/main/java/umc/th/juinjang/model/entity/Limjang.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public class Limjang extends BaseEntity {
@OneToMany(mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true)
private List<ChecklistAnswer> 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)
Expand All @@ -91,9 +91,6 @@ public class Limjang extends BaseEntity {
@BatchSize(size = 100)
private List<Image> imageList = new ArrayList<>();

@OneToOne(fetch = FetchType.LAZY, mappedBy = "limjangId", cascade = CascadeType.ALL, orphanRemoval = true)
private Scrap scrap;

@Column(name = "record_count")
@ColumnDefault("0") //default 0
private int recordCount;
Expand All @@ -106,15 +103,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;
Expand All @@ -128,9 +116,4 @@ public void updateMemo(String memo){
public void saveImages(Image image){
this.imageList.add(image);
}

public void saveReport(Report report){
this.report = report;
}

}
5 changes: 0 additions & 5 deletions src/main/java/umc/th/juinjang/model/entity/Scrap.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -32,8 +31,4 @@ public class Scrap extends BaseEntity {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "limjang_id")
private Limjang limjangId;

public void saveLimjang(Limjang limjang){
this.limjangId = limjang;
}
}
Original file line number Diff line number Diff line change
@@ -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<Limjang> searchLimjangs(Member member, String keyword);

// List<Limjang> findTop5ByMemberIdOrderByUpdatedAtDesc(Member member);
List<Limjang> searchLimjangsWhereDeletedIsFalse(Member member, String keyword);

List<Limjang> findMainScreenContentsLimjang(Member member);

Limjang findByIdWithLimjangPrice(long memberId, long limjangId);

List<Limjang> findAllLimjangs(Member member);

List<Limjang> findAllByMemberAndOrderByParam(Member member, LimjangSortOptions sort);
List<Limjang> findAllByMemberAndDeletedIsFalseOrderByParam(Member member, LimjangSortOptions sort);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ public LimjangQueryDslRepositoryImpl(EntityManager em) {
}

@Override
public List<Limjang> searchLimjangs(Member member, String keyword) {
String rKeyword = keyword.replaceAll(" ", "");

public List<Limjang> searchLimjangsWhereDeletedIsFalse(Member member, String keyword) {
String rKeyword = removeKeywordBlank(keyword);
return queryFactory
.selectFrom(limjang)
.leftJoin(limjang.report, report).fetchJoin()
.leftJoin(limjang.scrap, scrap).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),
keywordOf(
removeBlank(limjang.nickname).containsIgnoreCase(rKeyword),
Expand All @@ -50,25 +49,18 @@ public List<Limjang> searchLimjangs(Member member, String keyword) {
.fetch();
}

@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();
private String removeKeywordBlank(String keyword) {
return keyword.replaceAll(" ", "");
}

public List<Limjang> findAllByMemberAndOrderByParam(Member member, LimjangSortOptions sort) {
public List<Limjang> findAllByMemberAndDeletedIsFalseOrderByParam(Member member, LimjangSortOptions sort) {
return queryFactory
.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))
.fetch();
}
Expand All @@ -81,9 +73,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()]);
}

Expand All @@ -110,16 +101,4 @@ public List<Limjang> findMainScreenContentsLimjang(Member member) {
.limit(5)
.fetch();
}

@Override
public List<Limjang> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public interface LimjangRepository extends JpaRepository<Limjang, Long>, Limjang
void softDeleteByIds(@Param("ids") List<Long> 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개 순서대로
Expand All @@ -57,4 +57,3 @@ public interface LimjangRepository extends JpaRepository<Limjang, Long>, Limjang
Optional<Limjang> findByLimjangIdAndMemberIdWithLimjangPriceAndDeletedIsFalse(@Param("id") Long id, @Param("member") Member member);
}


Loading

0 comments on commit aee0f79

Please sign in to comment.