From 8f9a5255ad087a9b72f5103865ccb0e6754b4462 Mon Sep 17 00:00:00 2001 From: junker Date: Sun, 20 Aug 2023 17:18:47 +0900 Subject: [PATCH] =?UTF-8?q?feat=20:=20Question=20=EA=B4=80=EB=A0=A8=20of,?= =?UTF-8?q?=20toEntity=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/celeb/dto/CelebChipResDto.java | 8 + .../domain/celeb/entity/InterestedCeleb.java | 8 + .../question/dto/QuestionGetDetailResDto.java | 31 ++ .../question/dto/QuestionImgResDto.java | 20 + .../question/dto/QuestionImgSimpleResDto.java | 16 + .../question/dto/QuestionItemResDto.java | 20 + .../question/dto/QuestionListGetResDto.java | 15 - .../question/dto/QuestionPostResDto.java | 6 + .../question/dto/QuestionSimpleResDto.java | 17 + .../question/dto/QuestionVoteDataDto.java | 23 ++ .../domain/question/entity/Question.java | 4 +- .../domain/question/entity/QuestionBuy.java | 19 + .../domain/question/entity/QuestionFind.java | 19 + .../question/entity/QuestionHowabout.java | 20 +- .../domain/question/entity/QuestionImg.java | 26 +- .../domain/question/entity/QuestionItem.java | 21 +- .../domain/question/entity/QuestionLike.java | 13 +- .../question/entity/QuestionRecommend.java | 16 + .../entity/QuestionRecommendCategory.java | 13 +- .../question/entity/QuestionReport.java | 25 +- .../domain/question/entity/QuestionVote.java | 9 + .../question/entity/RecentQuestion.java | 8 + .../question/service/QuestionService.java | 359 +++++------------- .../domain/search/service/SearchService.java | 62 +-- .../domain/user/service/UserService.java | 109 +++--- 25 files changed, 455 insertions(+), 432 deletions(-) delete mode 100644 src/main/java/com/sluv/server/domain/question/dto/QuestionListGetResDto.java create mode 100644 src/main/java/com/sluv/server/domain/question/dto/QuestionVoteDataDto.java diff --git a/src/main/java/com/sluv/server/domain/celeb/dto/CelebChipResDto.java b/src/main/java/com/sluv/server/domain/celeb/dto/CelebChipResDto.java index b1f41ad2..232df928 100644 --- a/src/main/java/com/sluv/server/domain/celeb/dto/CelebChipResDto.java +++ b/src/main/java/com/sluv/server/domain/celeb/dto/CelebChipResDto.java @@ -1,6 +1,7 @@ package com.sluv.server.domain.celeb.dto; import com.sluv.server.domain.celeb.entity.Celeb; +import com.sluv.server.domain.celeb.entity.NewCeleb; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -27,4 +28,11 @@ public static CelebChipResDto of(Celeb celeb){ .build(); } + public static CelebChipResDto of(NewCeleb celeb){ + return CelebChipResDto.builder() + .celebId(celeb.getId()) + .celebName(celeb.getCelebName()) + .build(); + } + } diff --git a/src/main/java/com/sluv/server/domain/celeb/entity/InterestedCeleb.java b/src/main/java/com/sluv/server/domain/celeb/entity/InterestedCeleb.java index f948fd2e..53ee2629 100644 --- a/src/main/java/com/sluv/server/domain/celeb/entity/InterestedCeleb.java +++ b/src/main/java/com/sluv/server/domain/celeb/entity/InterestedCeleb.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.celeb.entity; +import com.sluv.server.domain.celeb.exception.CelebNotFoundException; import com.sluv.server.domain.user.entity.User; import com.sluv.server.global.common.entity.BaseEntity; import jakarta.persistence.*; @@ -31,4 +32,11 @@ public class InterestedCeleb extends BaseEntity { @NotNull private Celeb celeb; + public static InterestedCeleb toEntity(User user, Celeb celeb){ + return InterestedCeleb.builder() + .user(user) + .celeb(celeb) + .build(); + } + } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionGetDetailResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionGetDetailResDto.java index 188bb012..0c75ee42 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionGetDetailResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionGetDetailResDto.java @@ -1,7 +1,9 @@ package com.sluv.server.domain.question.dto; import com.sluv.server.domain.celeb.dto.CelebChipResDto; +import com.sluv.server.domain.question.entity.Question; import com.sluv.server.domain.user.dto.UserInfoDto; +import com.sluv.server.domain.user.entity.User; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -59,6 +61,35 @@ public class QuestionGetDetailResDto { // Question Recommend @Schema(description = "QuestionRecommend Category 리스트") private List recommendCategoryList; + public static QuestionGetDetailResDto of(Question question, String qType, User user, + List imgList, List itemList, + Long likeNum, Long commentNum, Boolean hasLike, Boolean hasMine, + CelebChipResDto celeb, CelebChipResDto newCeleb, + LocalDateTime voteEndTime, Long totalVoteNum, Long voteStatus, + List recommendCategoryList){ + UserInfoDto writer = UserInfoDto.of(user); + + return QuestionGetDetailResDto.builder() + .qType(qType) + .user(writer) + .title(question.getTitle()) + .content(question.getContent()) + .imgList(imgList) + .itemList(itemList) + .searchNum(question.getSearchNum()) + .likeNum(likeNum) + .commentNum(commentNum) + .createdAt(question.getCreatedAt()) + .hasLike(hasLike) + .hasMine(hasMine) + .voteEndTime(voteEndTime) + .totalVoteNum(totalVoteNum) + .voteStatus(voteStatus) + .celeb(celeb) + .newCeleb(newCeleb) + .recommendCategoryList(recommendCategoryList) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionImgResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionImgResDto.java index bd17ef6a..0ef7a7b2 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionImgResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionImgResDto.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.question.dto; +import com.sluv.server.domain.question.entity.QuestionImg; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -23,4 +24,23 @@ public class QuestionImgResDto { private Boolean representFlag; @Schema(description = "순서") private Integer sortOrder; + + public static QuestionImgResDto of(QuestionImg questionImg, QuestionVoteDataDto voteDataDto){ + return QuestionImgResDto.builder() + .imgUrl(questionImg.getImgUrl()) + .description(questionImg.getDescription()) + .voteNum( + voteDataDto != null + ? voteDataDto.getVoteNum() + : null + ) + .votePercent( + voteDataDto != null + ? voteDataDto.getVotePercent() + : null + ) + .representFlag(questionImg.getRepresentFlag()) + .sortOrder(questionImg.getSortOrder()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionImgSimpleResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionImgSimpleResDto.java index 60a529d7..76abe988 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionImgSimpleResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionImgSimpleResDto.java @@ -1,5 +1,7 @@ package com.sluv.server.domain.question.dto; +import com.sluv.server.domain.item.entity.ItemImg; +import com.sluv.server.domain.question.entity.QuestionImg; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -15,4 +17,18 @@ public class QuestionImgSimpleResDto { private String imgUrl; @Schema(description = "이미지 순서") private Long sortOrder; + + public static QuestionImgSimpleResDto of(QuestionImg questionImg){ + return QuestionImgSimpleResDto.builder() + .imgUrl(questionImg.getImgUrl()) + .sortOrder( (long) questionImg.getSortOrder()) + .build(); + } + + public static QuestionImgSimpleResDto of(ItemImg itemImg){ + return QuestionImgSimpleResDto.builder() + .imgUrl(itemImg.getItemImgUrl()) + .sortOrder( (long) itemImg.getSortOrder()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionItemResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionItemResDto.java index c51fd53f..c49c41ea 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionItemResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionItemResDto.java @@ -1,6 +1,7 @@ package com.sluv.server.domain.question.dto; import com.sluv.server.domain.item.dto.ItemSimpleResDto; +import com.sluv.server.domain.question.entity.QuestionItem; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -24,4 +25,23 @@ public class QuestionItemResDto { private Boolean representFlag; @Schema(description = "순서") private Integer sortOrder; + + public static QuestionItemResDto of(QuestionItem questionItem, ItemSimpleResDto itemSimpleResDto, QuestionVoteDataDto voteDataDto){ + return QuestionItemResDto.builder() + .item(itemSimpleResDto) + .description(questionItem.getDescription()) + .voteNum( + voteDataDto != null + ? voteDataDto.getVoteNum() + : null + ) + .votePercent( + voteDataDto != null + ? voteDataDto.getVotePercent() + : null + ) + .representFlag(questionItem.getRepresentFlag()) + .sortOrder(questionItem.getSortOrder()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionListGetResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionListGetResDto.java deleted file mode 100644 index bfe5efc7..00000000 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionListGetResDto.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.sluv.server.domain.question.dto; - -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; - -@Data -@NoArgsConstructor -@AllArgsConstructor -@Builder -public class QuestionListGetResDto { - private Long id; - -} diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionPostResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionPostResDto.java index a87c8ab2..8c576088 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionPostResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionPostResDto.java @@ -13,4 +13,10 @@ public class QuestionPostResDto { @Schema(description = "생성된 Question의 Id") private Long id; + + public static QuestionPostResDto of(Long questionId){ + return QuestionPostResDto.builder() + .id(questionId) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionSimpleResDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionSimpleResDto.java index e8bd3908..0bf34f22 100644 --- a/src/main/java/com/sluv/server/domain/question/dto/QuestionSimpleResDto.java +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionSimpleResDto.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.question.dto; +import com.sluv.server.domain.question.entity.Question; import io.swagger.v3.oas.annotations.media.Schema; import lombok.AllArgsConstructor; import lombok.Builder; @@ -35,4 +36,20 @@ public class QuestionSimpleResDto { //추천해 줘 @Schema(description = "QuestionRecommend 게시글 카테고리 리스트") private List categoryName; + public static QuestionSimpleResDto of(String qType, Question question, + String celebName, + List imgList, List itemImgList, + List categoryName){ + + return QuestionSimpleResDto.builder() + .qType(qType) + .id(question.getId()) + .title(question.getTitle()) + .content(question.getContent()) + .celebName(celebName) + .imgList(imgList) + .itemImgList(itemImgList) + .categoryName(categoryName) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/dto/QuestionVoteDataDto.java b/src/main/java/com/sluv/server/domain/question/dto/QuestionVoteDataDto.java new file mode 100644 index 00000000..14cc75ed --- /dev/null +++ b/src/main/java/com/sluv/server/domain/question/dto/QuestionVoteDataDto.java @@ -0,0 +1,23 @@ +package com.sluv.server.domain.question.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; + +@Data +@AllArgsConstructor +@Builder +public class QuestionVoteDataDto { + @Schema(description = "투표 수") + private Long voteNum; + @Schema(description = "투표 퍼센트") + private Double votePercent; + + public static QuestionVoteDataDto of(Long voteNum, Double votePercent) { + return QuestionVoteDataDto.builder() + .voteNum(voteNum) + .votePercent(votePercent) + .build(); + } +} diff --git a/src/main/java/com/sluv/server/domain/question/entity/Question.java b/src/main/java/com/sluv/server/domain/question/entity/Question.java index a9ad5289..712a71f7 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/Question.java +++ b/src/main/java/com/sluv/server/domain/question/entity/Question.java @@ -42,11 +42,11 @@ public class Question{ private String content; @NotNull - private Long searchNum; + private Long searchNum = 0L; @Enumerated(EnumType.STRING) @Column(columnDefinition = "varchar(45) default 'ACTIVE'") - private QuestionStatus questionStatus; + private QuestionStatus questionStatus = QuestionStatus.ACTIVE; @CreatedDate @Column(updatable = false) diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionBuy.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionBuy.java index 3ec7cc75..d185987e 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionBuy.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionBuy.java @@ -1,5 +1,9 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.celeb.entity.Celeb; +import com.sluv.server.domain.celeb.entity.NewCeleb; +import com.sluv.server.domain.question.dto.QuestionBuyPostReqDto; +import com.sluv.server.domain.question.dto.QuestionFindPostReqDto; import com.sluv.server.domain.question.enums.QuestionStatus; import com.sluv.server.domain.user.entity.User; import jakarta.persistence.DiscriminatorValue; @@ -25,4 +29,19 @@ public QuestionBuy(Long id, User user, String title, String content, Long search super(id, user, title, content, searchNum, questionStatus); this.voteEndTime = voteEndTime; } + + public static QuestionBuy toEntity(User user, QuestionBuyPostReqDto postReqDto){ + QuestionBuyBuilder builder = QuestionBuy.builder(); + + if(postReqDto.getId() != null) { + builder + .id(postReqDto.getId()); + } + + return builder + .user(user) + .title(postReqDto.getTitle()) + .voteEndTime(postReqDto.getVoteEndTime()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionFind.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionFind.java index 458f264c..172d8dbb 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionFind.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionFind.java @@ -2,6 +2,8 @@ import com.sluv.server.domain.celeb.entity.Celeb; import com.sluv.server.domain.celeb.entity.NewCeleb; +import com.sluv.server.domain.question.dto.QuestionFindPostReqDto; +import com.sluv.server.domain.question.dto.QuestionHowaboutPostReqDto; import com.sluv.server.domain.question.enums.QuestionStatus; import com.sluv.server.domain.user.entity.User; import jakarta.persistence.DiscriminatorValue; @@ -35,4 +37,21 @@ public QuestionFind(Long id, User user, String title, String content, Long searc this.celeb = celeb; this.newCeleb = newCeleb; } + + public static QuestionFind toEntity(User user, QuestionFindPostReqDto postReqDto, Celeb celeb, NewCeleb newCeleb){ + QuestionFindBuilder builder = QuestionFind.builder(); + + if(postReqDto.getId() != null){ + builder + .id(postReqDto.getId()); + } + + return builder + .user(user) + .title(postReqDto.getTitle()) + .content(postReqDto.getContent()) + .celeb(celeb) + .newCeleb(newCeleb) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionHowabout.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionHowabout.java index c7d52ea7..e4b77875 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionHowabout.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionHowabout.java @@ -1,15 +1,12 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.question.dto.QuestionHowaboutPostReqDto; import com.sluv.server.domain.question.enums.QuestionStatus; import com.sluv.server.domain.user.entity.User; import jakarta.persistence.DiscriminatorValue; import jakarta.persistence.Entity; -import lombok.AllArgsConstructor; import lombok.Builder; import lombok.NoArgsConstructor; -import lombok.experimental.SuperBuilder; - -import java.time.LocalDateTime; @Entity @NoArgsConstructor @@ -21,4 +18,19 @@ public class QuestionHowabout extends Question{ public QuestionHowabout(Long id, User user, String title, String content, Long searchNum, QuestionStatus questionStatus) { super(id, user, title, content, searchNum, questionStatus); } + + public static QuestionHowabout toEntity(User user, QuestionHowaboutPostReqDto postReqDto){ + QuestionHowaboutBuilder builder = QuestionHowabout.builder(); + + if(postReqDto.getId() != null){ + builder + .id(postReqDto.getId()); + } + + return builder + .user(user) + .title(postReqDto.getTitle()) + .content(postReqDto.getContent()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionImg.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionImg.java index a4e4cef1..6150e31a 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionImg.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionImg.java @@ -1,10 +1,12 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.question.dto.QuestionImgReqDto; import com.sluv.server.global.common.entity.BaseEntity; import com.sluv.server.global.common.enums.ItemImgOrLinkStatus; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -13,6 +15,8 @@ @Getter @Entity @NoArgsConstructor +@AllArgsConstructor +@Builder @Table(name = "question_img") public class QuestionImg extends BaseEntity { @@ -39,17 +43,17 @@ public class QuestionImg extends BaseEntity { private Integer sortOrder; @Enumerated(EnumType.STRING) + @Builder.Default @Column(columnDefinition = "varchar(45) default 'ACTIVE'") - private ItemImgOrLinkStatus itemImgOrLinkStatus; - - @Builder - public QuestionImg(Long id, Question question, String imgUrl, String description, Boolean representFlag, Integer sortOrder, ItemImgOrLinkStatus itemImgOrLinkStatus) { - this.id = id; - this.question = question; - this.imgUrl = imgUrl; - this.description = description; - this.representFlag = representFlag; - this.sortOrder = sortOrder; - this.itemImgOrLinkStatus = itemImgOrLinkStatus; + private ItemImgOrLinkStatus itemImgOrLinkStatus = ItemImgOrLinkStatus.ACTIVE; + + public static QuestionImg toEntity(Question question, QuestionImgReqDto reqDto) { + return QuestionImg.builder() + .question(question) + .imgUrl(reqDto.getImgUrl()) + .description(reqDto.getDescription()) + .representFlag(reqDto.getRepresentFlag()) + .sortOrder(reqDto.getSortOrder()) + .build(); } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionItem.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionItem.java index 968f345a..22c2b084 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionItem.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionItem.java @@ -1,10 +1,13 @@ package com.sluv.server.domain.question.entity; import com.sluv.server.domain.item.entity.Item; +import com.sluv.server.domain.question.dto.QuestionItemReqDto; +import com.sluv.server.domain.question.dto.QuestionItemResDto; import com.sluv.server.global.common.entity.BaseEntity; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -13,6 +16,8 @@ @Getter @Entity @NoArgsConstructor +@AllArgsConstructor +@Builder @Table(name = "question_item") public class QuestionItem extends BaseEntity { @@ -38,13 +43,13 @@ public class QuestionItem extends BaseEntity { private Integer sortOrder; - @Builder - public QuestionItem(Long id, Question question, Item item, String description, Boolean representFlag, Integer sortOrder) { - this.id = id; - this.question = question; - this.item = item; - this.description = description; - this.representFlag = representFlag; - this.sortOrder = sortOrder; + public static QuestionItem toEntity(Question question, Item item, QuestionItemReqDto reqDto) { + return QuestionItem.builder() + .question(question) + .item(item) + .description(reqDto.getDescription()) + .representFlag(reqDto.getRepresentFlag()) + .sortOrder(reqDto.getSortOrder()) + .build(); } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionLike.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionLike.java index b3a1c459..4b7dac6b 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionLike.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionLike.java @@ -4,6 +4,7 @@ import com.sluv.server.global.common.entity.BaseEntity; import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -11,6 +12,8 @@ @Getter @Entity @NoArgsConstructor +@AllArgsConstructor +@Builder @Table(name = "question_like") public class QuestionLike extends BaseEntity { @@ -29,10 +32,10 @@ public class QuestionLike extends BaseEntity { private User user; - @Builder - public QuestionLike(Long id, Question question, User user) { - this.id = id; - this.question = question; - this.user = user; + public static QuestionLike toEntity(User user, Question question) { + return QuestionLike.builder() + .user(user) + .question(question) + .build(); } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommend.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommend.java index ad9b5856..00a418e9 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommend.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommend.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.question.dto.QuestionRecommendPostReqDto; import com.sluv.server.domain.question.enums.QuestionStatus; import com.sluv.server.domain.user.entity.User; import jakarta.persistence.DiscriminatorValue; @@ -17,4 +18,19 @@ public class QuestionRecommend extends Question{ public QuestionRecommend(Long id, User user, String title, String content, Long searchNum, QuestionStatus questionStatus) { super(id, user, title, content, searchNum, questionStatus); } + + public static QuestionRecommend toEntity(User user, QuestionRecommendPostReqDto postReqDto){ + QuestionRecommendBuilder builder = QuestionRecommend.builder(); + + if(postReqDto.getId() != null){ + builder + .id(postReqDto.getId()); + } + + return builder + .user(user) + .title(postReqDto.getTitle()) + .content(postReqDto.getContent()) + .build(); + } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommendCategory.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommendCategory.java index f6307a74..d36256c2 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommendCategory.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionRecommendCategory.java @@ -3,12 +3,15 @@ import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @Entity @NoArgsConstructor +@AllArgsConstructor +@Builder @Getter public class QuestionRecommendCategory { @@ -24,10 +27,10 @@ public class QuestionRecommendCategory { @Size(max = 45) private String name; - @Builder - public QuestionRecommendCategory(Long id, Question question, String name) { - this.id = id; - this.question = question; - this.name = name; + public static QuestionRecommendCategory toentity(Question question, String categoryName) { + return QuestionRecommendCategory.builder() + .question(question) + .name(categoryName) + .build(); } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionReport.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionReport.java index a95717d2..e181cc5d 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionReport.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionReport.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.question.dto.QuestionReportReqDto; import com.sluv.server.domain.question.enums.QuestionReportReason; import com.sluv.server.domain.user.entity.User; import com.sluv.server.domain.user.enums.UserReportReason; @@ -8,6 +9,7 @@ import jakarta.persistence.*; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -15,6 +17,8 @@ @Entity @Getter @NoArgsConstructor +@AllArgsConstructor +@Builder @Table(name = "question_report") public class QuestionReport extends BaseEntity { @@ -39,17 +43,18 @@ public class QuestionReport extends BaseEntity { @Size(max = 1002) private String content; + @Builder.Default @Enumerated(EnumType.STRING) @Column(length = 45, columnDefinition = "varchar(45) default 'WAITING'") - private ReportStatus reportStatus; - - @Builder - public QuestionReport(Long id, User reporter, Question question, QuestionReportReason questionReportReason, String content, ReportStatus reportStatus) { - this.id = id; - this.reporter = reporter; - this.question = question; - this.questionReportReason = questionReportReason; - this.content = content; - this.reportStatus = reportStatus; + private ReportStatus reportStatus = ReportStatus.WAITING; + + + public static QuestionReport toEntity(User user, Question question, QuestionReportReqDto dto) { + return QuestionReport.builder() + .reporter(user) + .question(question) + .questionReportReason(dto.getReason()) + .content(dto.getContent()) + .build(); } } diff --git a/src/main/java/com/sluv/server/domain/question/entity/QuestionVote.java b/src/main/java/com/sluv/server/domain/question/entity/QuestionVote.java index 81054b65..56d3a692 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/QuestionVote.java +++ b/src/main/java/com/sluv/server/domain/question/entity/QuestionVote.java @@ -1,5 +1,6 @@ package com.sluv.server.domain.question.entity; +import com.sluv.server.domain.question.dto.QuestionVoteReqDto; import com.sluv.server.domain.user.entity.User; import com.sluv.server.global.common.entity.BaseEntity; import jakarta.persistence.*; @@ -30,4 +31,12 @@ public class QuestionVote extends BaseEntity { private Long voteSortOrder; + public static QuestionVote toEntity(Question question, User user, QuestionVoteReqDto dto){ + return QuestionVote.builder() + .question(question) + .user(user) + .voteSortOrder(dto.getVoteSortOrder()) + .build(); + } + } diff --git a/src/main/java/com/sluv/server/domain/question/entity/RecentQuestion.java b/src/main/java/com/sluv/server/domain/question/entity/RecentQuestion.java index 65c305c2..c58a1168 100644 --- a/src/main/java/com/sluv/server/domain/question/entity/RecentQuestion.java +++ b/src/main/java/com/sluv/server/domain/question/entity/RecentQuestion.java @@ -35,5 +35,13 @@ public class RecentQuestion extends BaseEntity { @NotNull private User user; + public static RecentQuestion toEntity(User user, String qType, Question question){ + return RecentQuestion.builder() + .user(user) + .qType(qType) + .question(question) + .build(); + } + } diff --git a/src/main/java/com/sluv/server/domain/question/service/QuestionService.java b/src/main/java/com/sluv/server/domain/question/service/QuestionService.java index f46e5ce7..49070879 100644 --- a/src/main/java/com/sluv/server/domain/question/service/QuestionService.java +++ b/src/main/java/com/sluv/server/domain/question/service/QuestionService.java @@ -23,22 +23,19 @@ import com.sluv.server.domain.question.exception.QuestionReportDuplicateException; import com.sluv.server.domain.question.exception.QuestionTypeNotFoundException; import com.sluv.server.domain.question.repository.*; -import com.sluv.server.domain.user.dto.UserInfoDto; import com.sluv.server.domain.user.entity.User; import com.sluv.server.global.common.enums.ItemImgOrLinkStatus; import com.sluv.server.global.common.enums.ReportStatus; import com.sluv.server.global.common.response.PaginationResDto; import lombok.RequiredArgsConstructor; -import org.checkerframework.checker.units.qual.A; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.time.LocalDateTime; import java.util.ArrayList; -import java.util.Collections; import java.util.List; -import java.util.stream.Stream; @Service @RequiredArgsConstructor @@ -68,12 +65,6 @@ public QuestionPostResDto postQuestionFind(User user, QuestionFindPostReqDto dto * 4. QuestionItem 저장 */ // 1. 생성 or 수정 - QuestionFind.QuestionFindBuilder questionFindBuilder = QuestionFind.builder(); - if(dto.getId() != null){ - questionFindBuilder.id(dto.getId()); - } - - // 2. QuestionFind 저장 Celeb celeb = null; if(dto.getCelebId() != null) { celeb = celebRepository.findById(dto.getCelebId()).orElse(null); @@ -83,16 +74,9 @@ public QuestionPostResDto postQuestionFind(User user, QuestionFindPostReqDto dto newCeleb = newCelebRepository.findById(dto.getNewCelebId()).orElse(null); } - QuestionFind questionFind = questionFindBuilder - .user(user) - .title(dto.getTitle()) - .content(dto.getContent()) - .searchNum(0L) - .celeb(celeb) - .newCeleb(newCeleb) - .questionStatus(QuestionStatus.ACTIVE) - .build(); + QuestionFind questionFind = QuestionFind.toEntity(user, dto, celeb, newCeleb); + // 2. QuestionFind 저장 QuestionFind newQuestionFind = questionRepository.save(questionFind); // 3. QuestionImg 저장 @@ -101,9 +85,7 @@ public QuestionPostResDto postQuestionFind(User user, QuestionFindPostReqDto dto // 4. QuestionItem 저장 postQuestionItems(dto.getItemList(), newQuestionFind); - return QuestionPostResDto.builder() - .id(newQuestionFind.getId()) - .build(); + return QuestionPostResDto.of(newQuestionFind.getId()); } @@ -117,20 +99,9 @@ public QuestionPostResDto postQuestionBuy(User user, QuestionBuyPostReqDto dto) */ // 1. 생성 or 수정 - QuestionBuy.QuestionBuyBuilder questionBuyBuilder = QuestionBuy.builder(); - if(dto.getId() != null){ - questionBuyBuilder.id(dto.getId()); - } + QuestionBuy questionBuy = QuestionBuy.toEntity(user, dto); // 2. QuestionBuy 저장 - QuestionBuy questionBuy = questionBuyBuilder - .user(user) - .title(dto.getTitle()) - .searchNum(0L) - .voteEndTime(dto.getVoteEndTime()) - .questionStatus(QuestionStatus.ACTIVE) - .build(); - QuestionBuy newQuestionBuy = questionRepository.save(questionBuy); // 3. QuestionImg 저장 @@ -139,9 +110,7 @@ public QuestionPostResDto postQuestionBuy(User user, QuestionBuyPostReqDto dto) // 4. QuestionItem 저장 postQuestionItems(dto.getItemList(), newQuestionBuy); - return QuestionPostResDto.builder() - .id(newQuestionBuy.getId()) - .build(); + return QuestionPostResDto.of(newQuestionBuy.getId()); } @Transactional @@ -154,20 +123,9 @@ public QuestionPostResDto postQuestionHowabout(User user, QuestionHowaboutPostRe */ // 1. 생성 or 수정 - QuestionHowabout.QuestionHowaboutBuilder questionHowaboutBuilder = QuestionHowabout.builder(); - if(dto.getId() != null){ - questionHowaboutBuilder.id(dto.getId()); - } + QuestionHowabout questionHowabout = QuestionHowabout.toEntity(user, dto); // 2. QuestionHotabout 저장 - QuestionHowabout questionHowabout = questionHowaboutBuilder - .user(user) - .title(dto.getTitle()) - .content(dto.getContent()) - .searchNum(0L) - .questionStatus(QuestionStatus.ACTIVE) - .build(); - QuestionHowabout newQuestionHowabout = questionRepository.save(questionHowabout); // 3. QuestionImg 저장 @@ -176,9 +134,7 @@ public QuestionPostResDto postQuestionHowabout(User user, QuestionHowaboutPostRe // 4. QuestionItem 저장 postQuestionItems(dto.getItemList(), newQuestionHowabout); - return QuestionPostResDto.builder() - .id(newQuestionHowabout.getId()) - .build(); + return QuestionPostResDto.of(newQuestionHowabout.getId()); } @Transactional @@ -192,20 +148,9 @@ public QuestionPostResDto postQuestionRecommend(User user, QuestionRecommendPost */ // 1. 생성 or 수정 - QuestionRecommend.QuestionRecommendBuilder questionRecommendBuilder = QuestionRecommend.builder(); - if(dto.getId() != null){ - questionRecommendBuilder.id(dto.getId()); - } + QuestionRecommend questionRecommend = QuestionRecommend.toEntity(user, dto); // 2. QuestionRecommend 저장 - QuestionRecommend questionRecommend = questionRecommendBuilder - .user(user) - .title(dto.getTitle()) - .content(dto.getContent()) - .searchNum(0L) - .questionStatus(QuestionStatus.ACTIVE) - .build(); - QuestionRecommend newQuestionRecommend = questionRepository.save(questionRecommend); // 3. Recommend Category 저장 @@ -214,10 +159,7 @@ public QuestionPostResDto postQuestionRecommend(User user, QuestionRecommendPost List recommendCategoryList = dto.getCategoryNameList().stream() .map(categoryName -> - QuestionRecommendCategory.builder() - .question(newQuestionRecommend) - .name(categoryName) - .build() + QuestionRecommendCategory.toentity(newQuestionRecommend, categoryName) ).toList(); questionRecommendCategoryRepository.saveAll(recommendCategoryList); @@ -229,15 +171,11 @@ public QuestionPostResDto postQuestionRecommend(User user, QuestionRecommendPost // 4. QuestionItem 저장 postQuestionItems(dto.getItemList(), newQuestionRecommend); - return QuestionPostResDto.builder() - .id(newQuestionRecommend.getId()) - .build(); + return QuestionPostResDto.of(newQuestionRecommend.getId()); } /** * Question Img 저장 메소드 - * @param dtoStream - * @param question */ private void postQuestionImgs(List dtoList, Question question){ // Question에 대한 Img 초기화 @@ -245,15 +183,9 @@ private void postQuestionImgs(List dtoList, Question question if(dtoList != null) { // Question Img들 추가 - List imgList = dtoList.stream().map(imgDto -> QuestionImg.builder() - .question(question) - .imgUrl(imgDto.getImgUrl()) - .description(imgDto.getDescription()) - .representFlag(imgDto.getRepresentFlag()) - .itemImgOrLinkStatus(ItemImgOrLinkStatus.ACTIVE) - .sortOrder(imgDto.getSortOrder()) - .build() - ).toList(); + List imgList = dtoList.stream() + .map(imgDto -> QuestionImg.toEntity(question, imgDto)) + .toList(); questionImgRepository.saveAll(imgList); } @@ -261,8 +193,7 @@ private void postQuestionImgs(List dtoList, Question question /** * Questim Item 저장 메소드 - * @param dtoStream - * @param question + * */ private void postQuestionItems(List dtoList, Question question){ // Question에 대한 Item 초기화 @@ -271,13 +202,7 @@ private void postQuestionItems(List dtoList, Question questi // Question Item들 추가 List itemList = dtoList.stream().map(itemDto -> { Item item = itemRepository.findById(itemDto.getItemId()).orElseThrow(ItemNotFoundException::new); - return QuestionItem.builder() - .question(question) - .item(item) - .description(itemDto.getDescription()) - .representFlag(itemDto.getRepresentFlag()) - .sortOrder(itemDto.getSortOrder()) - .build(); + return QuestionItem.toEntity(question, item, itemDto); } ).toList(); @@ -306,10 +231,7 @@ public void postQuestionLike(User user, Long questionId) { // like가 없다면 등록 questionLikeRepository.save( - QuestionLike.builder() - .user(user) - .question(question) - .build() + QuestionLike.toEntity(user, question) ); } question.decreaseSearchNum(); @@ -324,13 +246,7 @@ public void postQuestionReport(User user, Long questionId, QuestionReportReqDto Question question = questionRepository.findById(questionId).orElseThrow(QuestionNotFoundException::new); questionReportRepository.save( - QuestionReport.builder() - .reporter(user) - .question(question) - .questionReportReason(dto.getReason()) - .content(dto.getContent()) - .reportStatus(ReportStatus.WAITING) - .build() + QuestionReport.toEntity(user, question, dto) ); }else{ @@ -368,17 +284,13 @@ public QuestionGetDetailResDto getQuestionDetail(User user, Long questionId) { List questionImgList= questionImgRepository.findAllByQuestionId(questionId). stream() .map(questionImg -> { - QuestionImgResDto.QuestionImgResDtoBuilder builder = QuestionImgResDto.builder() - .imgUrl(questionImg.getImgUrl()) - .representFlag(questionImg.getRepresentFlag()) - .sortOrder(questionImg.getSortOrder()) - .description(questionImg.getDescription()); - + QuestionVoteDataDto voteDataDto = null; // QuestionBuy 라면 if(qType != null && qType.equals("Buy")){ - addVoteNum(questionId, (long) questionImg.getSortOrder(), builder, null); + voteDataDto = getVoteData(questionId, (long) questionImg.getSortOrder()); } - return builder.build(); + + return QuestionImgResDto.of(questionImg, voteDataDto); } ).toList(); @@ -387,117 +299,83 @@ public QuestionGetDetailResDto getQuestionDetail(User user, Long questionId) { .stream() .map(questionItem -> { List closetList = closetRepository.findAllByUserId(user.getId()); - ItemSimpleResDto dto = ItemSimpleResDto.of( + ItemSimpleResDto itemSimpleResDto = ItemSimpleResDto.of( questionItem.getItem(), itemImgRepository.findMainImg(questionItem.getItem().getId()), itemScrapRepository.getItemScrapStatus(questionItem.getItem(),closetList) ); - - QuestionItemResDto.QuestionItemResDtoBuilder builder = QuestionItemResDto.builder() - .item(dto) - .representFlag(questionItem.getRepresentFlag()) - .sortOrder(questionItem.getSortOrder()) - .description(questionItem.getDescription()); - + QuestionVoteDataDto questionVoteDataDto = null; // QuestionBuy일 경우 투표수 추가. - if(qType != null & qType.equals("Buy")){ - addVoteNum(questionId, (long) questionItem.getSortOrder(), null, builder); + questionVoteDataDto = getVoteData(questionId, (long) questionItem.getSortOrder()); } - return builder.build(); + return QuestionItemResDto.of(questionItem, itemSimpleResDto, questionVoteDataDto); }).toList(); // Question Like Num Count Long questionLikeNum = questionLikeRepository.countByQuestionId(questionId); // Question Comment Num Count - Long questionSearchNum = commentRepository.countByQuestionId(questionId); + Long questionCommentNum = commentRepository.countByQuestionId(questionId); // hasLike 검색 Boolean currentUserLike = questionLikeRepository.existsByQuestionIdAndUserId(questionId, user.getId()); - QuestionGetDetailResDto.QuestionGetDetailResDtoBuilder builder = QuestionGetDetailResDto.builder(); - if(qType.equals("Find")){ - QuestionFind questionFind = (QuestionFind) question; - CelebChipResDto.CelebChipResDtoBuilder builder1 = CelebChipResDto.builder(); - if(questionFind.getCeleb() != null){ - CelebChipResDto dto = builder1.celebId(questionFind.getCeleb().getId()).celebName(questionFind.getCeleb().getCelebNameKr()) - .build(); - builder - .celeb(dto) - .newCeleb(null); - }else { - CelebChipResDto dto = builder1.celebId(questionFind.getNewCeleb().getId()).celebName(questionFind.getNewCeleb().getCelebName()) - .build(); - builder - .celeb(null) - .newCeleb(dto); + CelebChipResDto celeb = null; + CelebChipResDto newCeleb = null; + LocalDateTime voteEndTime = null; + Long totalVoteNum = null; + Long voteStatus = null; + List recommendCategoryList = null; + + switch (qType) { + case "Find" -> { + QuestionFind questionFind = (QuestionFind) question; + if (questionFind.getCeleb() != null) { + celeb = CelebChipResDto.of(questionFind.getCeleb()); + } else { + newCeleb = CelebChipResDto.of(questionFind.getNewCeleb()); + } } - - builder - .voteEndTime(null); - }else if(qType.equals("Buy")) { - QuestionBuy questionBuy = (QuestionBuy) question; - - QuestionVote questionVote = questionVoteRepository.findByQuestionIdAndUserId(questionId, user.getId()) - .orElse(null); - - builder - .celeb(null) - .newCeleb(null) - .voteEndTime(questionBuy.getVoteEndTime()) - .totalVoteNum(questionVoteRepository.countByQuestionId(questionId)) - .voteStatus( - questionVote != null + case "Buy" -> { + QuestionBuy questionBuy = (QuestionBuy) question; + QuestionVote questionVote = questionVoteRepository.findByQuestionIdAndUserId(questionId, user.getId()) + .orElse(null); + voteEndTime = questionBuy.getVoteEndTime(); + totalVoteNum = questionVoteRepository.countByQuestionId(questionId); + voteStatus = questionVote != null ? questionVote.getVoteSortOrder() - : null - ); - }else if(qType.equals("Recommend")) { - List categoryList = questionRecommendCategoryRepository.findAllByQuestionId(questionId) - .stream().map(QuestionRecommendCategory::getName).toList(); - builder.recommendCategoryList(categoryList); - }else{ - builder.celeb(null) - .newCeleb(null) - .voteEndTime(null); + : null; + } + case "Recommend" -> + recommendCategoryList = questionRecommendCategoryRepository.findAllByQuestionId(questionId) + .stream().map(QuestionRecommendCategory::getName).toList(); } // RecentQuestion 등록 recentQuestionRepository.save( - RecentQuestion.builder() - .user(user) - .qType(qType) - .question(question) - .build() + RecentQuestion.toEntity(user, qType, question) ); // SearchNum 증가 question.increaseSearchNum(); - return builder - .qType(qType) - .user( - UserInfoDto.of(writer) - ) - .title(question.getTitle()) - .content(question.getContent()) - .imgList(questionImgList) - .itemList(questionItemList) - .searchNum(question.getSearchNum()) - .likeNum(questionLikeNum) - .commentNum(questionSearchNum) - .createdAt(question.getCreatedAt()) - .hasLike(currentUserLike) - .hasMine(user.getId().equals(writer.getId())) - - .build(); + return QuestionGetDetailResDto.of( + question, qType, writer, + questionImgList, questionItemList, + questionLikeNum, questionCommentNum, currentUserLike, + user.getId().equals(writer.getId()), + celeb, newCeleb, + voteEndTime, totalVoteNum, voteStatus, + recommendCategoryList + ); } /** * builder에 VoteNum, VotePercent 탑재 */ - private void addVoteNum(Long questionId, Long sortOrder, QuestionImgResDto.QuestionImgResDtoBuilder imgBuilder, QuestionItemResDto.QuestionItemResDtoBuilder itemBuilder) { + private QuestionVoteDataDto getVoteData(Long questionId, Long sortOrder) { // 해당 SortOrder의 투표 수 @@ -505,27 +383,12 @@ private void addVoteNum(Long questionId, Long sortOrder, QuestionImgResDto.Quest // 전체 투표 수 Long totalVoteNum = questionVoteRepository.countByQuestionId(questionId); - // builder 조립 - if(imgBuilder != null){ - imgBuilder - .voteNum(voteNum) - .votePercent( - totalVoteNum != 0 - ? getVotePercent(voteNum, totalVoteNum) - : 0 + return QuestionVoteDataDto.of( + voteNum, + totalVoteNum != 0 + ? getVotePercent(voteNum, totalVoteNum) + : 0 ); - }else if(itemBuilder != null){ - itemBuilder - .voteNum(voteNum) - .votePercent( - totalVoteNum != 0 - ? getVotePercent(voteNum, totalVoteNum) - : 0 - ); - }else { - throw new IllegalArgumentException(); - } - } /** @@ -552,11 +415,7 @@ public void postQuestionVote(User user, Long questionId, QuestionVoteReqDto dto) // QuestionVote 생성 및 저장 questionVoteRepository.save( - QuestionVote.builder() - .question(question) - .user(user) - .voteSortOrder(dto.getVoteSortOrder()) - .build() + QuestionVote.toEntity(question, user, dto) ); }else{ @@ -578,10 +437,8 @@ public List getWaitQuestionBuy(User user, Long questionId) return questionRepository.getWaitQuestionBuy(user, questionId, interestedCeleb) .stream() .map(questionBuy -> - getQuestionSimpleResDto(questionBuy.getId(), - "Buy", - questionBuy.getTitle(), - questionBuy.getContent() + getQuestionSimpleResDto(questionBuy, + "Buy" ) ).toList(); } @@ -594,10 +451,8 @@ public List getWaitQuestionRecommend(User user, Long quest return questionRepository.getWaitQuestionRecommend(user, questionId) .stream() .map(questionRecommend -> - getQuestionSimpleResDto(questionRecommend.getId(), - "Recommend", - questionRecommend.getTitle(), - questionRecommend.getContent() + getQuestionSimpleResDto(questionRecommend, + "Recommend" ) ).toList(); } @@ -610,10 +465,8 @@ public List getWaitQuestionHowabout(User user, Long questi return questionRepository.getWaitQuestionHowabout(user, questionId) .stream() .map(questionHowabout -> - getQuestionSimpleResDto(questionHowabout.getId(), - "How", - questionHowabout.getTitle(), - questionHowabout.getContent() + getQuestionSimpleResDto(questionHowabout, + "How" ) ).toList(); } @@ -628,15 +481,14 @@ public List getWaitQuestionFind(User user, Long questionId return questionRepository.getWaitQuestionFind(user, questionId, interestedCeleb) .stream() .map(questionFind -> - getQuestionSimpleResDto(questionFind.getId(), - "Find", - questionFind.getTitle(), - questionFind.getContent() + getQuestionSimpleResDto(questionFind, + "Find" ) ).toList(); } - private QuestionSimpleResDto getQuestionSimpleResDto(Long questionId, String qType, String title, String content) { +// private QuestionSimpleResDto getQuestionSimpleResDto(Long questionId, String qType, String title, String content) { + private QuestionSimpleResDto getQuestionSimpleResDto(Question question, String qType) { List imgList = new ArrayList<>(); List itemImgList = new ArrayList<>(); @@ -644,63 +496,44 @@ private QuestionSimpleResDto getQuestionSimpleResDto(Long questionId, String qTy if(!qType.equals("Buy")) { // 이미지 URL - QuestionImg questionImg = questionImgRepository.findByQuestionIdAndRepresentFlag(questionId, true); + QuestionImg questionImg = questionImgRepository.findByQuestionIdAndRepresentFlag(question.getId(), true); QuestionImgSimpleResDto img = null; // 이미지 Dto로 변경 if (questionImg != null) { - img = QuestionImgSimpleResDto.builder() - .imgUrl(questionImg.getImgUrl()) - .sortOrder((long) questionImg.getSortOrder()) - .build(); + img = QuestionImgSimpleResDto.of(questionImg); } imgList.add(img); // 아이템 이미지 URL - QuestionItem questionItem = questionItemRepository.findByQuestionIdAndRepresentFlag(questionId, true); + QuestionItem questionItem = questionItemRepository.findByQuestionIdAndRepresentFlag(question.getId(), true); QuestionImgSimpleResDto itemImg = null; // 아이템 이미지 Dto로 변경 if (questionItem != null) { ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); - itemImg = QuestionImgSimpleResDto.builder() - .imgUrl(mainImg.getItemImgUrl()) - .sortOrder((long) mainImg.getSortOrder()) - .build(); + itemImg = QuestionImgSimpleResDto.of(mainImg); } imgList.add(itemImg); }else{ // 이미지 URL - imgList = questionImgRepository.findAllByQuestionId(questionId) + imgList = questionImgRepository.findAllByQuestionId(question.getId()) .stream() - .map(img -> QuestionImgSimpleResDto.builder() - .imgUrl(img.getImgUrl()) - .sortOrder((long)img.getSortOrder()) - .build() - ).toList(); + .map(QuestionImgSimpleResDto::of).toList(); // 아이템 이미지 URL - itemImgList = questionItemRepository.findAllByQuestionId(questionId) + itemImgList = questionItemRepository.findAllByQuestionId(question.getId()) .stream() .map(item -> { ItemImg mainImg = itemImgRepository.findMainImg(item.getItem().getId()); - return QuestionImgSimpleResDto.builder() - .imgUrl(mainImg.getItemImgUrl()) - .sortOrder((long) mainImg.getSortOrder()) - .build(); + return QuestionImgSimpleResDto.of(mainImg); }).toList(); } - return QuestionSimpleResDto.builder() - .qType(qType) - .id(questionId) - .title(title) - .content(content) - .imgList(imgList) - .itemImgList(itemImgList) - .build(); + return QuestionSimpleResDto.of(qType, question, + null, imgList, itemImgList, null); } public PaginationResDto getTotalQuestionList(Pageable pageable) { @@ -718,7 +551,7 @@ public PaginationResDto getTotalQuestionList(Pageable page } else { throw new QuestionTypeNotFoundException(); } - return getQuestionSimpleResDto(question.getId(), qType, question.getTitle(), question.getContent()); + return getQuestionSimpleResDto(question, qType); }).toList(); return PaginationResDto.builder() @@ -731,7 +564,7 @@ public PaginationResDto getTotalQuestionList(Pageable page public PaginationResDto getQuestionBuyList(Pageable pageable) { Page questionPage = questionRepository.getQuestionBuyList(pageable); List content = questionPage.stream().map(question -> - getQuestionSimpleResDto(question.getId(), "Buy", question.getTitle(), question.getContent()) + getQuestionSimpleResDto(question, "Buy") ).toList(); return PaginationResDto.builder() @@ -744,7 +577,7 @@ public PaginationResDto getQuestionBuyList(Pageable pageab public PaginationResDto getQuestionFindList(Pageable pageable) { Page questionPage = questionRepository.getQuestionFindList(pageable); List content = questionPage.stream().map(question -> - getQuestionSimpleResDto(question.getId(), "Find", question.getTitle(), question.getContent()) + getQuestionSimpleResDto(question, "Find") ).toList(); return PaginationResDto.builder() @@ -757,7 +590,7 @@ public PaginationResDto getQuestionFindList(Pageable pagea public PaginationResDto getQuestionHowaboutList(Pageable pageable) { Page questionPage = questionRepository.getQuestionHowaboutList(pageable); List content = questionPage.stream().map(question -> - getQuestionSimpleResDto(question.getId(), "How", question.getTitle(), question.getContent()) + getQuestionSimpleResDto(question, "How") ).toList(); return PaginationResDto.builder() @@ -771,7 +604,7 @@ public PaginationResDto getQuestionHowaboutList(Pageable p public PaginationResDto getQuestionRecommendList(Pageable pageable) { Page questionPage = questionRepository.getQuestionRecommendList(pageable); List content = questionPage.stream().map(question -> - getQuestionSimpleResDto(question.getId(), "Recommend", question.getTitle(), question.getContent()) + getQuestionSimpleResDto(question, "Recommend") ).toList(); return PaginationResDto.builder() diff --git a/src/main/java/com/sluv/server/domain/search/service/SearchService.java b/src/main/java/com/sluv/server/domain/search/service/SearchService.java index 70a84de8..2dc7439f 100644 --- a/src/main/java/com/sluv/server/domain/search/service/SearchService.java +++ b/src/main/java/com/sluv/server/domain/search/service/SearchService.java @@ -123,21 +123,20 @@ public PaginationResDto getSearchQuestion(User user, Strin { // 해당 Question의 item 이미지 리스트 구하기 List questionItemList = questionItemRepository.findAllByQuestionId(question.getId()); - List itemImgList = questionItemList.stream().map(this::convertQuestionItemToQuestionImgSimpleResDto).toList(); +// List itemImgList = questionItemList.stream().map(this::convertQuestionItemToQuestionImgSimpleResDto).toList(); + List itemImgList = questionItemList.stream() + .map(questionItem -> { + ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); + return QuestionImgSimpleResDto.of(mainImg); + }).toList(); // 해당 Question의 이미지 리스트 구하기 List imgList = questionImgRepository.findAllByQuestionId(question.getId()) .stream() - .map(this::convertQuestionImgToQuestionImgSimpleResDto).toList(); + .map(QuestionImgSimpleResDto::of).toList(); - return QuestionSimpleResDto.builder() - .qType("Buy") - .id(question.getId()) - .title(question.getTitle()) - .content(question.getContent()) - .imgList(imgList) - .itemImgList(itemImgList) - .build(); + return QuestionSimpleResDto.of("Buy", question, null, + imgList, itemImgList, null); }).toList(); return PaginationResDto.builder() @@ -150,20 +149,14 @@ public PaginationResDto getSearchQuestion(User user, Strin Page searchQuestionPage = questionRepository.getSearchQuestionFind(questionIdList, pageable); - List content = searchQuestionPage.stream().map(question -> + List content = searchQuestionPage.stream().map(question -> { + String celebName = question.getCeleb() != null + ? question.getCeleb().getCelebNameKr() + : question.getNewCeleb().getCelebName(); - QuestionSimpleResDto.builder() - .qType("Find") - .id(question.getId()) - .title(question.getTitle()) - .content(question.getContent()) - .celebName( - question.getCeleb() != null - ?question.getCeleb().getCelebNameKr() - :question.getNewCeleb().getCelebName() - ) - .build() - ).toList(); + return QuestionSimpleResDto.of("Find", question, celebName, null, null, null); + + }).toList(); return PaginationResDto.builder() .page(searchQuestionPage.getNumber()) @@ -200,13 +193,7 @@ public PaginationResDto getSearchQuestion(User user, Strin List categoryList = questionRecommendCategoryRepository.findAllByQuestionId(question.getId()) .stream() .map(QuestionRecommendCategory::getName).toList(); - return QuestionSimpleResDto.builder() - .qType("Recommend") - .id(question.getId()) - .title(question.getTitle()) - .content(question.getContent()) - .categoryName(categoryList) - .build(); + return QuestionSimpleResDto.of("Recommend", question, null, null, null, categoryList); }).toList(); // 최근 검색 등록 @@ -385,21 +372,6 @@ public PaginationResDto getSearchKeyword(String keyword, Pa .build(); } - public QuestionImgSimpleResDto convertQuestionImgToQuestionImgSimpleResDto(QuestionImg questionImg){ - return QuestionImgSimpleResDto.builder() - .imgUrl(questionImg.getImgUrl()) - .sortOrder((long)questionImg.getSortOrder()) - .build(); - } - - public QuestionImgSimpleResDto convertQuestionItemToQuestionImgSimpleResDto(QuestionItem questionItem){ - ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); - return QuestionImgSimpleResDto.builder() - .imgUrl(mainImg.getItemImgUrl()) - .sortOrder((long) mainImg.getSortOrder()) - .build(); - } - /** * 현재 유저의 RecentSearch 키워드 삭제 */ diff --git a/src/main/java/com/sluv/server/domain/user/service/UserService.java b/src/main/java/com/sluv/server/domain/user/service/UserService.java index e8643d3d..12160c32 100644 --- a/src/main/java/com/sluv/server/domain/user/service/UserService.java +++ b/src/main/java/com/sluv/server/domain/user/service/UserService.java @@ -164,10 +164,10 @@ public void postInterestedCeleb(User user, InterestedCelebPostReqDto dto) { // 초기화 상태에서 다시 추가. List interestedCelebList = dto.getCelebIdList().stream() .map(celeb -> - InterestedCeleb.builder() - .user(user) - .celeb(celebRepository.findById(celeb).orElseThrow(CelebNotFoundException::new)) - .build() + InterestedCeleb.toEntity( + user, + celebRepository.findById(celeb).orElseThrow(CelebNotFoundException::new) + ) ).toList(); interestedCelebRepository.saveAll(interestedCelebList); @@ -306,50 +306,45 @@ public PaginationCountResDto getUserRecentQuestion(User us List content = recentQuestionPage.stream().map(recentQuestion -> { Question question = questionRepository.findById(recentQuestion.getQuestion().getId()) .orElseThrow(QuestionNotFoundException::new); + List imgList = null; + List itemImgList = null; + List categoryList = null; + String celebName = null; - QuestionSimpleResDto.QuestionSimpleResDtoBuilder builder = QuestionSimpleResDto.builder() - .qType(recentQuestion.getQType()) - .id(question.getId()) - .title(question.getTitle()) - .content(question.getContent()); if (recentQuestion.getQType().equals("Buy")) { // 이미지 Dto 생성 - List imgList = questionImgRepository.findAllByQuestionId(question.getId()) + imgList = questionImgRepository.findAllByQuestionId(question.getId()) .stream() - .map(this::convertQuestionImgToQuestionImgSimpleResDto).toList(); + .map(QuestionImgSimpleResDto::of).toList(); // 아이템 이미지 Dto 생성 - List itemImgList = questionItemRepository.findAllByQuestionId(question.getId()) + itemImgList = questionItemRepository.findAllByQuestionId(question.getId()) .stream() - .map(this::convertQuestionItemToQuestionImgSimpleResDto).toList(); - builder - .imgList(imgList) - .itemImgList(itemImgList); + .map(questionItem -> { + ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); + return QuestionImgSimpleResDto.of(mainImg); + }).toList(); } else if (recentQuestion.getQType().equals("How")) { } else if (recentQuestion.getQType().equals("Recommend")) { // Question 카테고리 - List categoryList = questionRecommendCategoryRepository.findAllByQuestionId(question.getId()) + categoryList = questionRecommendCategoryRepository.findAllByQuestionId(question.getId()) .stream() .map(QuestionRecommendCategory::getName).toList(); - builder - .categoryName(categoryList); } else if (recentQuestion.getQType().equals("Find")) { QuestionFind questionFind = (QuestionFind) question; - builder - .celebName( - questionFind.getCeleb() != null + celebName = questionFind.getCeleb() != null ?questionFind.getCeleb().getParent() != null ?questionFind.getCeleb().getParent().getCelebNameKr() + " " + questionFind.getCeleb().getCelebNameKr() :questionFind.getCeleb().getCelebNameKr() - :questionFind.getNewCeleb().getCelebName() - ); + :questionFind.getNewCeleb().getCelebName(); } else { throw new QuestionTypeNotFoundException(); } - return builder.build(); + return QuestionSimpleResDto.of(recentQuestion.getQType(), question, celebName, imgList, itemImgList, categoryList); + }).toList(); @@ -442,66 +437,52 @@ public PaginationCountResDto getUserLikeQuestion(User user } private QuestionSimpleResDto dtoBuildByQuestionType(Question question) { - QuestionSimpleResDto.QuestionSimpleResDtoBuilder builder = QuestionSimpleResDto.builder() - .id(question.getId()) - .title(question.getTitle()) - .content(question.getContent()); + String qType = null; + List imgList = null; + List itemImgList = null; + String celebName = null; + List categoryList = null; if(question instanceof QuestionBuy){ // 1. question이 QuestionBuy 일 경우 // 이미지 DTO 생성 - List imgList = questionImgRepository.findAllByQuestionId(question.getId()) + List imgSimpleList = questionImgRepository.findAllByQuestionId(question.getId()) .stream() - .map(this::convertQuestionImgToQuestionImgSimpleResDto).toList(); + .map(QuestionImgSimpleResDto::of).toList(); // 아이템 이미지 DTO 생성 - List itemImgList = questionItemRepository.findAllByQuestionId(question.getId()) + List itemImgSimpleList = questionItemRepository.findAllByQuestionId(question.getId()) .stream() - .map(this::convertQuestionItemToQuestionImgSimpleResDto).toList(); + .map(questionItem -> { + ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); + return QuestionImgSimpleResDto.of(mainImg); + }).toList(); - builder - .qType("Buy") - .imgList(imgList) - .itemImgList(itemImgList); + qType = "Buy"; + imgList = imgSimpleList; + itemImgList = itemImgSimpleList; } else if (question instanceof QuestionFind questionFind) { // 2. question이 QuestionFind 일 경우 - builder - .qType("Find") - .celebName(questionFind.getCeleb() != null + qType = "Find"; + celebName = questionFind.getCeleb() != null ?questionFind.getCeleb().getParent() != null ? questionFind.getCeleb().getParent().getCelebNameKr() + " " + questionFind.getCeleb().getCelebNameKr() : questionFind.getCeleb().getCelebNameKr() - : questionFind.getNewCeleb().getCelebName() - ); + : questionFind.getNewCeleb().getCelebName(); + } else if (question instanceof QuestionHowabout) { // 3. question이 QuestionHowabout 일 경우 - builder - .qType("How"); + qType = "How"; } else if (question instanceof QuestionRecommend) { // 4. question이 QuestionRecommend 일 경우 - List categoryList = questionRecommendCategoryRepository.findAllByQuestionId(question.getId()) + List categoryNameList = questionRecommendCategoryRepository.findAllByQuestionId(question.getId()) .stream() .map(QuestionRecommendCategory::getName).toList(); - builder - .qType("Recommend") - .categoryName(categoryList); + qType = "Recommend"; + categoryList = categoryNameList; + }else{ throw new QuestionTypeNotFoundException(); } - return builder.build(); - } - - private QuestionImgSimpleResDto convertQuestionImgToQuestionImgSimpleResDto(QuestionImg questionImg){ - return QuestionImgSimpleResDto.builder() - .imgUrl(questionImg.getImgUrl()) - .sortOrder((long)questionImg.getSortOrder()) - .build(); - } - - private QuestionImgSimpleResDto convertQuestionItemToQuestionImgSimpleResDto(QuestionItem questionItem){ - ItemImg mainImg = itemImgRepository.findMainImg(questionItem.getItem().getId()); - return QuestionImgSimpleResDto.builder() - .imgUrl(mainImg.getItemImgUrl()) - .sortOrder((long) mainImg.getSortOrder()) - .build(); + return QuestionSimpleResDto.of(qType, question, celebName, imgList, itemImgList, categoryList); } /**