From 9940a6ffa1497732e42e999dadd7ed2b1463713c Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 13:48:40 +0900 Subject: [PATCH 01/28] =?UTF-8?q?[Refactor]:=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?API=20=EB=A6=AC=ED=8C=A9=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PostSaveRequest로 받는 데이터 validation 추가 Related to: #559 --- .../community/controller/CommunityController.java | 3 ++- .../controller/dto/request}/PostSaveRequest.java | 10 +++++++++- .../community/service/CommunityPostService.java | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) rename src/main/java/org/sopt/makers/internal/{dto/community => community/controller/dto/request}/PostSaveRequest.java (60%) diff --git a/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java b/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java index 61578e49..22f69f08 100644 --- a/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java +++ b/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java @@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor; import lombok.val; import org.sopt.makers.internal.common.InfiniteScrollUtil; +import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; import org.sopt.makers.internal.community.service.CommunityPostService; import org.sopt.makers.internal.domain.InternalMemberDetails; import org.sopt.makers.internal.dto.community.*; @@ -97,7 +98,7 @@ public ResponseEntity> upPostHit( @PostMapping("/posts") public ResponseEntity createPost( @Parameter(hidden = true) @AuthenticationPrincipal InternalMemberDetails memberDetails, - @RequestBody PostSaveRequest request + @RequestBody @Valid PostSaveRequest request ) { val response = communityPostService.createPost(memberDetails.getId(), request); return ResponseEntity.status(HttpStatus.CREATED).body(response); diff --git a/src/main/java/org/sopt/makers/internal/dto/community/PostSaveRequest.java b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java similarity index 60% rename from src/main/java/org/sopt/makers/internal/dto/community/PostSaveRequest.java rename to src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java index 307244d8..ce1b8cfc 100644 --- a/src/main/java/org/sopt/makers/internal/dto/community/PostSaveRequest.java +++ b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java @@ -1,13 +1,21 @@ -package org.sopt.makers.internal.dto.community; +package org.sopt.makers.internal.community.controller.dto.request; import io.swagger.v3.oas.annotations.media.Schema; public record PostSaveRequest( @Schema(required = true) Long categoryId, + String title, + + @Schema(required = true) String content, + + @Schema(required = true) Boolean isQuestion, + + @Schema(required = true) Boolean isBlindWriter, + String[] images ) {} diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 46bcdf0c..621e7ad7 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -6,6 +6,7 @@ import lombok.val; import org.sopt.makers.internal.common.MakersMemberId; import org.sopt.makers.internal.common.SlackMessageUtil; +import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; import org.sopt.makers.internal.community.domain.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPostLike; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; From 8c0115374542212bc17cf1b7b0430f042e8604b6 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 14:57:21 +0900 Subject: [PATCH 02/28] =?UTF-8?q?[Refactor]:=20CommunityPostRepository=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit repository/community -> community/repository 폴더로 이동 Related to: #559 --- .../repository}/CommunityPostRepository.java | 3 +-- .../internal/community/service/CommunityCommentService.java | 2 +- .../internal/community/service/CommunityPostService.java | 1 + .../internal/community/service/CommunityPostServiceUtil.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) rename src/main/java/org/sopt/makers/internal/{repository/community => community/repository}/CommunityPostRepository.java (77%) diff --git a/src/main/java/org/sopt/makers/internal/repository/community/CommunityPostRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java similarity index 77% rename from src/main/java/org/sopt/makers/internal/repository/community/CommunityPostRepository.java rename to src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java index 2007a01f..2a5fcfe1 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/CommunityPostRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java @@ -1,10 +1,9 @@ -package org.sopt.makers.internal.repository.community; +package org.sopt.makers.internal.community.repository; import java.time.LocalDateTime; import java.util.List; import java.util.Optional; -import org.sopt.makers.internal.community.repository.CommunityPostRepositoryCustom; import org.sopt.makers.internal.domain.community.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java index f762c4a3..c6ab3f98 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java @@ -23,7 +23,7 @@ import org.sopt.makers.internal.repository.community.AnonymousNicknameRepository; import org.sopt.makers.internal.repository.community.AnonymousPostProfileRepository; import org.sopt.makers.internal.repository.community.CommunityCommentRepository; -import org.sopt.makers.internal.repository.community.CommunityPostRepository; +import org.sopt.makers.internal.community.repository.CommunityPostRepository; import org.sopt.makers.internal.repository.community.CommunityQueryRepository; import org.sopt.makers.internal.repository.community.DeletedCommunityCommentRepository; import org.sopt.makers.internal.repository.community.ReportCommentRepository; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 621e7ad7..3b6b8b53 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -10,6 +10,7 @@ import org.sopt.makers.internal.community.domain.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPostLike; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; +import org.sopt.makers.internal.community.repository.CommunityPostRepository; import org.sopt.makers.internal.community.repository.category.CategoryRepository; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.community.*; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java index 1647fdd2..f86a0fe6 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java @@ -3,7 +3,7 @@ import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; import org.sopt.makers.internal.domain.community.CommunityPost; import org.sopt.makers.internal.exception.NotFoundDBEntityException; -import org.sopt.makers.internal.repository.community.CommunityPostRepository; +import org.sopt.makers.internal.community.repository.CommunityPostRepository; public class CommunityPostServiceUtil { From 19045a187e7da37f3cd7d37775fa4b931434a8a9 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 15:15:06 +0900 Subject: [PATCH 03/28] =?UTF-8?q?[Refactor]:=20CommunityPost.java=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EB=B0=8F=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit community domain 패키지로 이동 validation 필요한 필드 추가 Related to: #559 --- .../domain}/CommunityPost.java | 29 +++++++++---------- .../community/domain/CommunityPostLike.java | 1 - .../repository/CommunityPostRepository.java | 2 +- .../service/CommunityPostService.java | 1 + .../service/CommunityPostServiceUtil.java | 2 +- .../community/AnonymousPostProfile.java | 1 + .../dto/community/CategoryPostMemberDao.java | 2 +- .../dto/community/CommunityPostMemberVo.java | 1 - .../dto/community/HotPostResponse.java | 2 +- .../dto/community/PostCategoryDao.java | 2 +- .../internal/mapper/CommunityMapper.java | 2 +- .../mapper/CommunityResponseMapper.java | 2 +- .../internal/repository/PostRepository.java | 2 +- .../AnonymousPostProfileRepository.java | 2 +- .../community/CommunityQueryRepository.java | 1 + .../scheduler/PushNotificationScheduler.java | 2 +- 16 files changed, 26 insertions(+), 28 deletions(-) rename src/main/java/org/sopt/makers/internal/{domain/community => community/domain}/CommunityPost.java (69%) diff --git a/src/main/java/org/sopt/makers/internal/domain/community/CommunityPost.java b/src/main/java/org/sopt/makers/internal/community/domain/CommunityPost.java similarity index 69% rename from src/main/java/org/sopt/makers/internal/domain/community/CommunityPost.java rename to src/main/java/org/sopt/makers/internal/community/domain/CommunityPost.java index 7846fed5..4456c373 100644 --- a/src/main/java/org/sopt/makers/internal/domain/community/CommunityPost.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/CommunityPost.java @@ -1,12 +1,12 @@ -package org.sopt.makers.internal.domain.community; +package org.sopt.makers.internal.community.domain; import lombok.*; import org.hibernate.annotations.Type; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.common.AuditingTimeEntity; +import org.sopt.makers.internal.domain.community.CommunityComment; import javax.persistence.*; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; @@ -20,49 +20,46 @@ public class CommunityPost extends AuditingTimeEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) - @Column private Long id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "writer_id") private Member member; - @Column + @Column(nullable = false) private Long categoryId; @Column private String title; - @Column(columnDefinition = "TEXT") + @Column(columnDefinition = "TEXT", nullable = false) private String content; @Builder.Default - @Column + @Column(nullable = false) private Integer hits = 0; @Type(type = "string-array") @Column(name = "images", columnDefinition = "text[]") private String[] images; - @Builder.Default - @Column - private Boolean isQuestion = false; + @Column(nullable = false) + private Boolean isQuestion; - @Builder.Default - @Column - private Boolean isBlindWriter = false; + @Column(nullable = false) + private Boolean isBlindWriter; @Builder.Default - @Column + @Column(nullable = false) private Boolean isReported = false; @Builder.Default - @Column + @Column(nullable = false) private Boolean isHot = false; @Builder.Default @OneToMany( - cascade = CascadeType.ALL, + cascade = CascadeType.REMOVE, orphanRemoval = true ) @JoinColumn(name = "postId") diff --git a/src/main/java/org/sopt/makers/internal/community/domain/CommunityPostLike.java b/src/main/java/org/sopt/makers/internal/community/domain/CommunityPostLike.java index 24e9300a..5ed04497 100644 --- a/src/main/java/org/sopt/makers/internal/community/domain/CommunityPostLike.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/CommunityPostLike.java @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.common.AuditingTimeEntity; -import org.sopt.makers.internal.domain.community.CommunityPost; import javax.persistence.*; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java index 2a5fcfe1..e5ce9fce 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepository.java @@ -4,7 +4,7 @@ import java.util.List; import java.util.Optional; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; public interface CommunityPostRepository extends JpaRepository, CommunityPostRepositoryCustom { diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 3b6b8b53..811e82fc 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -8,6 +8,7 @@ import org.sopt.makers.internal.common.SlackMessageUtil; import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.community.domain.CommunityPostLike; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; import org.sopt.makers.internal.community.repository.CommunityPostRepository; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java index f86a0fe6..9e548c96 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostServiceUtil.java @@ -1,7 +1,7 @@ package org.sopt.makers.internal.community.service; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.exception.NotFoundDBEntityException; import org.sopt.makers.internal.community.repository.CommunityPostRepository; diff --git a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java b/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java index f6fefd9a..91e46185 100644 --- a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java +++ b/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java @@ -12,6 +12,7 @@ import javax.persistence.OneToOne; import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.common.AuditingTimeEntity; diff --git a/src/main/java/org/sopt/makers/internal/dto/community/CategoryPostMemberDao.java b/src/main/java/org/sopt/makers/internal/dto/community/CategoryPostMemberDao.java index 2e8ea0ab..4a3db378 100644 --- a/src/main/java/org/sopt/makers/internal/dto/community/CategoryPostMemberDao.java +++ b/src/main/java/org/sopt/makers/internal/dto/community/CategoryPostMemberDao.java @@ -3,7 +3,7 @@ import com.querydsl.core.annotations.QueryProjection; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.community.domain.category.Category; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; public record CategoryPostMemberDao( CommunityPost posts, diff --git a/src/main/java/org/sopt/makers/internal/dto/community/CommunityPostMemberVo.java b/src/main/java/org/sopt/makers/internal/dto/community/CommunityPostMemberVo.java index c6bcd9dc..5519589c 100644 --- a/src/main/java/org/sopt/makers/internal/dto/community/CommunityPostMemberVo.java +++ b/src/main/java/org/sopt/makers/internal/dto/community/CommunityPostMemberVo.java @@ -1,7 +1,6 @@ package org.sopt.makers.internal.dto.community; import com.querydsl.core.annotations.QueryProjection; -import org.sopt.makers.internal.domain.community.CommunityPost; public record CommunityPostMemberVo( MemberVo member, diff --git a/src/main/java/org/sopt/makers/internal/dto/community/HotPostResponse.java b/src/main/java/org/sopt/makers/internal/dto/community/HotPostResponse.java index ac28f3be..d0caa45c 100644 --- a/src/main/java/org/sopt/makers/internal/dto/community/HotPostResponse.java +++ b/src/main/java/org/sopt/makers/internal/dto/community/HotPostResponse.java @@ -1,6 +1,6 @@ package org.sopt.makers.internal.dto.community; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; public record HotPostResponse( Long id, diff --git a/src/main/java/org/sopt/makers/internal/dto/community/PostCategoryDao.java b/src/main/java/org/sopt/makers/internal/dto/community/PostCategoryDao.java index cba4484a..7c0dc428 100644 --- a/src/main/java/org/sopt/makers/internal/dto/community/PostCategoryDao.java +++ b/src/main/java/org/sopt/makers/internal/dto/community/PostCategoryDao.java @@ -2,7 +2,7 @@ import com.querydsl.core.annotations.QueryProjection; import org.sopt.makers.internal.community.domain.category.Category; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; public record PostCategoryDao( CommunityPost post, diff --git a/src/main/java/org/sopt/makers/internal/mapper/CommunityMapper.java b/src/main/java/org/sopt/makers/internal/mapper/CommunityMapper.java index 83c0ada3..694f31ab 100644 --- a/src/main/java/org/sopt/makers/internal/mapper/CommunityMapper.java +++ b/src/main/java/org/sopt/makers/internal/mapper/CommunityMapper.java @@ -2,7 +2,7 @@ import org.mapstruct.Mapper; import org.sopt.makers.internal.domain.community.CommunityComment; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.domain.community.DeletedCommunityComment; import org.sopt.makers.internal.domain.community.DeletedCommunityPost; diff --git a/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java b/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java index 881b0d76..d8d678bb 100644 --- a/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java +++ b/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java @@ -6,7 +6,7 @@ import org.sopt.makers.internal.domain.community.AnonymousCommentProfile; import org.sopt.makers.internal.domain.community.AnonymousPostProfile; import org.sopt.makers.internal.community.domain.category.Category; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.dto.community.*; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/sopt/makers/internal/repository/PostRepository.java b/src/main/java/org/sopt/makers/internal/repository/PostRepository.java index ec1a049e..7c4354ba 100644 --- a/src/main/java/org/sopt/makers/internal/repository/PostRepository.java +++ b/src/main/java/org/sopt/makers/internal/repository/PostRepository.java @@ -1,6 +1,6 @@ package org.sopt.makers.internal.repository; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; import java.util.Optional; diff --git a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java b/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java index bac83301..5fe6bb41 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java +++ b/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java @@ -5,7 +5,7 @@ import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.community.AnonymousPostProfile; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; public interface AnonymousPostProfileRepository extends JpaRepository { diff --git a/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java b/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java index 6b49264c..1c590bfd 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java +++ b/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java @@ -5,6 +5,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import lombok.val; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.community.domain.category.QCategory; import org.sopt.makers.internal.domain.*; import org.sopt.makers.internal.domain.community.*; diff --git a/src/main/java/org/sopt/makers/internal/scheduler/PushNotificationScheduler.java b/src/main/java/org/sopt/makers/internal/scheduler/PushNotificationScheduler.java index e91a2c9e..888fe684 100644 --- a/src/main/java/org/sopt/makers/internal/scheduler/PushNotificationScheduler.java +++ b/src/main/java/org/sopt/makers/internal/scheduler/PushNotificationScheduler.java @@ -7,7 +7,7 @@ import javax.persistence.PessimisticLockException; import org.sopt.makers.internal.community.service.CommunityPostService; -import org.sopt.makers.internal.domain.community.CommunityPost; +import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.dto.pushNotification.PushNotificationRequest; import org.sopt.makers.internal.service.PushNotificationService; import org.springframework.dao.PessimisticLockingFailureException; From 29e84f466735067f396028c06dd059cb547fcbdc Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 15:25:39 +0900 Subject: [PATCH 04/28] =?UTF-8?q?[Refactor]:=20AnonymousPostProfileReposit?= =?UTF-8?q?ory=20=ED=8C=8C=EC=9D=BC=20=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../repository/anonymous}/AnonymousPostProfileRepository.java | 2 +- .../internal/community/service/CommunityCommentService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) rename src/main/java/org/sopt/makers/internal/{repository/community => community/repository/anonymous}/AnonymousPostProfileRepository.java (92%) diff --git a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java similarity index 92% rename from src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java rename to src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java index 5fe6bb41..f626c9e2 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousPostProfileRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java @@ -1,4 +1,4 @@ -package org.sopt.makers.internal.repository.community; +package org.sopt.makers.internal.community.repository.anonymous; import java.util.List; import java.util.Optional; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java index c6ab3f98..7a801b74 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java @@ -21,7 +21,7 @@ import org.sopt.makers.internal.repository.MemberRepository; import org.sopt.makers.internal.repository.community.AnonymousCommentProfileRepository; import org.sopt.makers.internal.repository.community.AnonymousNicknameRepository; -import org.sopt.makers.internal.repository.community.AnonymousPostProfileRepository; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; import org.sopt.makers.internal.repository.community.CommunityCommentRepository; import org.sopt.makers.internal.community.repository.CommunityPostRepository; import org.sopt.makers.internal.repository.community.CommunityQueryRepository; From 023e5a7255ffe59ea2a5b0661db83817e4004f62 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 15:36:55 +0900 Subject: [PATCH 05/28] =?UTF-8?q?[Refactor]:=20AnonymousProfileImageReposi?= =?UTF-8?q?tory=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../{ => anonymous}/AnonymousProfileImageRepository.java | 0 .../community/service/AnonymousProfileImageService.java | 2 +- 2 files changed, 1 insertion(+), 1 deletion(-) rename src/main/java/org/sopt/makers/internal/community/repository/{ => anonymous}/AnonymousProfileImageRepository.java (100%) diff --git a/src/main/java/org/sopt/makers/internal/community/repository/AnonymousProfileImageRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java similarity index 100% rename from src/main/java/org/sopt/makers/internal/community/repository/AnonymousProfileImageRepository.java rename to src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java diff --git a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java b/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java index 46639303..0aa64085 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java @@ -1,7 +1,7 @@ package org.sopt.makers.internal.community.service; import org.sopt.makers.internal.community.domain.AnonymousProfileImage; -import org.sopt.makers.internal.community.repository.AnonymousProfileImageRepository; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; From e56b2aa645535eb77a4d9d0bd85519b98e71b6be Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 15:37:39 +0900 Subject: [PATCH 06/28] =?UTF-8?q?[Refactor]:=20AnonymousProfileImageReposi?= =?UTF-8?q?tory=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../repository/anonymous/AnonymousProfileImageRepository.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java index 463e1aa9..887bb3ea 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java @@ -1,4 +1,4 @@ -package org.sopt.makers.internal.community.repository; +package org.sopt.makers.internal.community.repository.anonymous; import org.sopt.makers.internal.community.domain.AnonymousProfileImage; import org.springframework.data.jpa.repository.JpaRepository; From 97b719ad857af0d929293dba1291b151eace3907 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 17:50:10 +0900 Subject: [PATCH 07/28] =?UTF-8?q?[Refactor]:=20repository=20=ED=8C=8C?= =?UTF-8?q?=EC=9D=BC=20=EC=9D=B4=EB=8F=99=20=EB=B0=8F=20querydsl=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousNicknameRepository.java | 2 +- .../AnonymousPostProfileRepository.java | 7 +---- .../AnonymousPostProfileRepositoryCustom.java | 10 +++++++ ...nymousPostProfileRepositoryCustomImpl.java | 26 +++++++++++++++++++ .../service/AnonymousNicknameServiceUtil.java | 17 ------------ .../community/CommunityQueryRepository.java | 1 + 6 files changed, 39 insertions(+), 24 deletions(-) rename src/main/java/org/sopt/makers/internal/{repository/community => community/repository/anonymous}/AnonymousNicknameRepository.java (92%) create mode 100644 src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java create mode 100644 src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java delete mode 100644 src/main/java/org/sopt/makers/internal/community/service/AnonymousNicknameServiceUtil.java diff --git a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousNicknameRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java similarity index 92% rename from src/main/java/org/sopt/makers/internal/repository/community/AnonymousNicknameRepository.java rename to src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java index 2d359441..e8a7470d 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/AnonymousNicknameRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java @@ -1,4 +1,4 @@ -package org.sopt.makers.internal.repository.community; +package org.sopt.makers.internal.community.repository.anonymous; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java index f626c9e2..a634b205 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java @@ -8,19 +8,14 @@ import org.sopt.makers.internal.community.domain.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; -public interface AnonymousPostProfileRepository extends JpaRepository { +public interface AnonymousPostProfileRepository extends JpaRepository, AnonymousPostProfileRepositoryCustom { // CREATE // READ Optional findAnonymousPostProfileByMemberIdAndCommunityPostId(Long memberId, Long communityPostId); - Optional findByMemberAndCommunityPost(Member member, CommunityPost post); - List findTop4ByOrderByCreatedAtDesc(); - - List findTop50ByOrderByCreatedAtDesc(); - // UPDATE //DELETE diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java new file mode 100644 index 00000000..a5e23b84 --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java @@ -0,0 +1,10 @@ +package org.sopt.makers.internal.community.repository.anonymous; + +import org.sopt.makers.internal.domain.community.AnonymousPostProfile; + +import java.util.List; + +public interface AnonymousPostProfileRepositoryCustom { + + List findTopByOrderByIdDescWithLimit(int limit); +} diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java new file mode 100644 index 00000000..3fbfcc65 --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java @@ -0,0 +1,26 @@ +package org.sopt.makers.internal.community.repository.anonymous; + +import com.querydsl.jpa.impl.JPAQueryFactory; +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.domain.community.QAnonymousPostProfile; + +import java.util.List; + +@RequiredArgsConstructor +public class AnonymousPostProfileRepositoryCustomImpl implements AnonymousPostProfileRepositoryCustom { + + private final JPAQueryFactory queryFactory; + + @Override + public List findTopByOrderByIdDescWithLimit(int limit) { + + QAnonymousPostProfile anonymousPostProfile = QAnonymousPostProfile.anonymousPostProfile; + + return queryFactory + .selectFrom(anonymousPostProfile) + .orderBy(anonymousPostProfile.createdAt.desc()) + .limit(limit) + .fetch(); + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/AnonymousNicknameServiceUtil.java b/src/main/java/org/sopt/makers/internal/community/service/AnonymousNicknameServiceUtil.java deleted file mode 100644 index 2be16c31..00000000 --- a/src/main/java/org/sopt/makers/internal/community/service/AnonymousNicknameServiceUtil.java +++ /dev/null @@ -1,17 +0,0 @@ -package org.sopt.makers.internal.community.service; - -import org.sopt.makers.internal.domain.community.AnonymousNickname; -import org.sopt.makers.internal.repository.community.AnonymousNicknameRepository; - -import java.util.List; - -public class AnonymousNicknameServiceUtil { - - public static AnonymousNickname getRandomNickname(AnonymousNicknameRepository anonymousNicknameRepository, List excludes) { - if (excludes.isEmpty()) { - return anonymousNicknameRepository.findRandomOne(); - } - return anonymousNicknameRepository.findRandomOneByIdNotIn( - excludes.stream().map(AnonymousNickname::getId).toList()); - } -} diff --git a/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java b/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java index 1c590bfd..6648886b 100644 --- a/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java +++ b/src/main/java/org/sopt/makers/internal/repository/community/CommunityQueryRepository.java @@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor; import lombok.val; import org.sopt.makers.internal.community.domain.CommunityPost; +import org.sopt.makers.internal.community.domain.QCommunityPost; import org.sopt.makers.internal.community.domain.category.QCategory; import org.sopt.makers.internal.domain.*; import org.sopt.makers.internal.domain.community.*; From ce89e5a9dd71da9c1bf3f07647ef671a3a199b58 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 18:57:01 +0900 Subject: [PATCH 08/28] =?UTF-8?q?[Refactor]:=20request=20body=20validation?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../dto/request/PostSaveRequest.java | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java index ce1b8cfc..89e48d57 100644 --- a/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java +++ b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java @@ -2,20 +2,30 @@ import io.swagger.v3.oas.annotations.media.Schema; +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + public record PostSaveRequest( - @Schema(required = true) - Long categoryId, + @Schema(required = true) + @NotNull + Long categoryId, - String title, + String title, - @Schema(required = true) - String content, + @Schema(required = true) + @NotBlank + String content, - @Schema(required = true) - Boolean isQuestion, + @Schema(required = true) + @NotNull + Boolean isQuestion, - @Schema(required = true) - Boolean isBlindWriter, + @Schema(required = true) + @NotNull + Boolean isBlindWriter, - String[] images -) {} + @Schema(required = true) + @NotNull + String[] images +) { +} From 573a9ce81c0f660b9c43dcea69c749f0b6739b00 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 18:58:22 +0900 Subject: [PATCH 09/28] =?UTF-8?q?[Refactor]:=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81=20=EA=B4=80=EB=A0=A8=20=ED=8C=8C=EC=9D=BC=20=EC=9D=B4?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit import문 변경있음 Related to: #559 --- .../internal/community/service/CommunityCommentService.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java index 7a801b74..13e71484 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java @@ -7,6 +7,7 @@ import org.apache.commons.lang3.StringUtils; import org.sopt.makers.internal.common.SlackMessageUtil; +import org.sopt.makers.internal.community.service.anonymous.AnonymousNicknameRetriever; import org.sopt.makers.internal.domain.community.AnonymousCommentProfile; import org.sopt.makers.internal.domain.community.CommunityComment; import org.sopt.makers.internal.domain.community.ReportComment; @@ -20,7 +21,7 @@ import org.sopt.makers.internal.mapper.CommunityMapper; import org.sopt.makers.internal.repository.MemberRepository; import org.sopt.makers.internal.repository.community.AnonymousCommentProfileRepository; -import org.sopt.makers.internal.repository.community.AnonymousNicknameRepository; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; import org.sopt.makers.internal.repository.community.CommunityCommentRepository; import org.sopt.makers.internal.community.repository.CommunityPostRepository; @@ -47,6 +48,7 @@ public class CommunityCommentService { @Value("${spring.profiles.active}") private String activeProfile; private final DeletedCommunityCommentRepository deletedCommunityCommentRepository; + private final AnonymousNicknameRetriever anonymousNicknameRetriever; private final CommunityMapper communityMapper; private final MemberRepository memberRepository; private final CommunityPostRepository communityPostRepository; @@ -94,7 +96,7 @@ public void createComment(Long writerId, Long postId, CommentSaveRequest request if (request.isBlindWriter() && anonymousCommentProfile.isEmpty()) { anonymousCommentProfileRepository.save(AnonymousCommentProfile.builder() - .nickname(member.equals(post.getMember()) ? anonymousPostProfile.get().getNickname() : AnonymousNicknameServiceUtil.getRandomNickname(anonymousNicknameRepository, excludeNickname)) + .nickname(member.equals(post.getMember()) ? anonymousPostProfile.get().getNickname() : anonymousNicknameRetriever.findRandomAnonymousNickname(excludeNickname)) .profileImg(member.equals(post.getMember()) ? anonymousPostProfile.get().getProfileImg() : anonymousProfileImageService.getRandomProfileImage(excludeImgList)) .member(member) .communityComment(comment) From ca94af5d42be03fd3bb53a9ff972c62420700658 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 18:59:24 +0900 Subject: [PATCH 10/28] =?UTF-8?q?[Refactor]:=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EC=BD=94=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Service 계층에 밀집된 비즈니스 로직 분리 하위 도메인 정의 및 코드 분리 Related to: #559 --- .../service/CommunityPostModifier.java | 33 ++++++++++++ .../service/CommunityPostService.java | 54 ++++++++----------- .../anonymous/AnonymousNicknameRetriever.java | 24 +++++++++ .../AnonymousPostProfileModifier.java | 28 ++++++++++ .../AnonymousPostProfileRetriever.java | 20 +++++++ .../AnonymousPostProfileService.java | 36 +++++++++++++ .../AnonymousProfileImageRetriever.java | 43 +++++++++++++++ 7 files changed, 205 insertions(+), 33 deletions(-) create mode 100644 src/main/java/org/sopt/makers/internal/community/service/CommunityPostModifier.java create mode 100644 src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java create mode 100644 src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java create mode 100644 src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java create mode 100644 src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java create mode 100644 src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostModifier.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostModifier.java new file mode 100644 index 00000000..e6e8449c --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostModifier.java @@ -0,0 +1,33 @@ +package org.sopt.makers.internal.community.service; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; +import org.sopt.makers.internal.community.repository.CommunityPostRepository; +import org.sopt.makers.internal.domain.Member; +import org.sopt.makers.internal.community.domain.CommunityPost; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; + +@Component +@RequiredArgsConstructor +public class CommunityPostModifier { + + private final CommunityPostRepository communityPostRepository; + + // CREATE + + public CommunityPost createCommunityPost(Member member, PostSaveRequest request) { + + return communityPostRepository.save(CommunityPost.builder() + .member(member) + .categoryId(request.categoryId()) + .title(request.title()) + .content(request.content()) + .images(request.images()) + .isQuestion(request.isQuestion()) + .isBlindWriter(request.isBlindWriter()) + .comments(new ArrayList<>()) + .build()); + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 811e82fc..13a39109 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -7,12 +7,17 @@ import org.sopt.makers.internal.common.MakersMemberId; import org.sopt.makers.internal.common.SlackMessageUtil; import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.community.domain.CommunityPostLike; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; import org.sopt.makers.internal.community.repository.CommunityPostRepository; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; import org.sopt.makers.internal.community.repository.category.CategoryRepository; +import org.sopt.makers.internal.community.service.anonymous.AnonymousNicknameRetriever; +import org.sopt.makers.internal.community.service.anonymous.AnonymousPostProfileModifier; +import org.sopt.makers.internal.community.service.anonymous.AnonymousPostProfileService; +import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.community.*; import org.sopt.makers.internal.dto.community.*; @@ -21,6 +26,7 @@ import org.sopt.makers.internal.external.slack.SlackClient; import org.sopt.makers.internal.mapper.CommunityMapper; import org.sopt.makers.internal.mapper.CommunityResponseMapper; +import org.sopt.makers.internal.member.service.MemberRetriever; import org.sopt.makers.internal.repository.MemberRepository; import org.sopt.makers.internal.repository.PostRepository; import org.sopt.makers.internal.repository.community.*; @@ -34,7 +40,6 @@ import java.time.LocalDateTime; import java.time.LocalTime; import java.time.ZoneId; -import java.util.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Objects; @@ -45,6 +50,16 @@ @Service public class CommunityPostService { + private final AnonymousPostProfileService anonymousPostProfileService; + + private final CommunityPostModifier communityPostModifier; + + private final AnonymousPostProfileModifier anonymousPostProfileModifier; + private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; + private final AnonymousNicknameRetriever anonymousNicknameRetriever; + + private final MemberRetriever memberRetriever; + private final CommunityCommentRepository communityCommentRepository; private final CommunityPostLikeRepository communityPostLikeRepository; private final CommunityPostRepository communityPostRepository; @@ -107,38 +122,11 @@ public CommunityPostMemberVo getPostById(Long memberId, Long postId, Boolean isB @Transactional public PostSaveResponse createPost(Long writerId, PostSaveRequest request) { - Member member = MemberServiceUtil.findMemberById(memberRepository, writerId); - CommunityPost post = communityPostRepository.save(CommunityPost.builder() - .member(member) - .categoryId(request.categoryId()) - .title(request.title()) - .content(request.content()) - .hits(0) - .images(request.images()) - .isQuestion(request.isQuestion()) - .isBlindWriter(request.isBlindWriter()) - .comments(new ArrayList<>()) - .build()); - - if (request.isBlindWriter()) { - List lastFourAnonymousPostProfiles = anonymousPostProfileRepository.findTop4ByOrderByCreatedAtDesc(); - List lastFiftyAnonymousNickname = anonymousPostProfileRepository.findTop50ByOrderByCreatedAtDesc(); - List usedAnonymousProfileImages = lastFourAnonymousPostProfiles.stream() - .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); - List usedAnonymousNicknames = lastFiftyAnonymousNickname.stream() - .map(AnonymousPostProfile::getNickname).toList(); - - AnonymousProfileImage anonymousProfileImg = anonymousProfileImageService.getRandomProfileImage(usedAnonymousProfileImages); - AnonymousNickname anonymousNickname = AnonymousNicknameServiceUtil.getRandomNickname(anonymousNicknameRepository, usedAnonymousNicknames); - anonymousPostProfileRepository.save(AnonymousPostProfile.builder() - .member(member) - .nickname(anonymousNickname) - .profileImg(anonymousProfileImg) - .communityPost(post) - .build() - ); - } + Member member = memberRetriever.findMemberById(writerId); + CommunityPost post = communityPostModifier.createCommunityPost(member, request); + anonymousPostProfileService.createAnonymousPostProfile(request.isBlindWriter(), member, post); + // 비메이커스가 글 작성시 슬랙 발송 if (!MakersMemberId.getMakersMember().contains(member.getId()) && Objects.equals(activeProfile, "prod")) { val slackRequest = createPostSlackRequest(post.getId()); slackClient.postNotMakersMessage(slackRequest.toString()); diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java new file mode 100644 index 00000000..aca7b7ff --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java @@ -0,0 +1,24 @@ +package org.sopt.makers.internal.community.service.anonymous; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; +import org.sopt.makers.internal.domain.community.AnonymousNickname; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class AnonymousNicknameRetriever { + + private final AnonymousNicknameRepository anonymousNicknameRepository; + + public AnonymousNickname findRandomAnonymousNickname(List recentUsedAnonymousNicknames) { + if (recentUsedAnonymousNicknames.isEmpty()) { + return anonymousNicknameRepository.findRandomOne(); + } + + return anonymousNicknameRepository.findRandomOneByIdNotIn( + recentUsedAnonymousNicknames.stream().map(AnonymousNickname::getId).toList()); + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java new file mode 100644 index 00000000..e4a2c230 --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java @@ -0,0 +1,28 @@ +package org.sopt.makers.internal.community.service.anonymous; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.CommunityPost; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; +import org.sopt.makers.internal.domain.Member; +import org.sopt.makers.internal.domain.community.AnonymousNickname; +import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.springframework.stereotype.Component; + +@Component +@RequiredArgsConstructor +public class AnonymousPostProfileModifier { + + private final AnonymousPostProfileRepository anonymousPostProfileRepository; + + public void createAnonymousPostProfile(Member member, AnonymousNickname anonymousNickname, AnonymousProfileImage anonymousProfileImage, CommunityPost communityPost) { + + anonymousPostProfileRepository.save(AnonymousPostProfile.builder() + .member(member) + .nickname(anonymousNickname) + .profileImg(anonymousProfileImage) + .communityPost(communityPost) + .build() + ); + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java new file mode 100644 index 00000000..5a11466b --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java @@ -0,0 +1,20 @@ +package org.sopt.makers.internal.community.service.anonymous; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; +import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class AnonymousPostProfileRetriever { + + private final AnonymousPostProfileRepository anonymousPostProfileRepository; + + public List getTopByOrderByCreatedAt(int limit) { + + return anonymousPostProfileRepository.findTopByOrderByIdDescWithLimit(limit); + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java new file mode 100644 index 00000000..a97b29f8 --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -0,0 +1,36 @@ +package org.sopt.makers.internal.community.service.anonymous; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.CommunityPost; +import org.sopt.makers.internal.domain.Member; +import org.sopt.makers.internal.domain.community.AnonymousNickname; +import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.springframework.stereotype.Component; + +import java.util.List; + +@Component +@RequiredArgsConstructor +public class AnonymousPostProfileService { + + private final AnonymousPostProfileModifier anonymousPostProfileModifier; + private final AnonymousPostProfileRetriever anonymousPostProfileRetriever; + private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; + private final AnonymousNicknameRetriever anonymousNicknameRetriever; + + public void createAnonymousPostProfile(Boolean isBlindWriter, Member member, CommunityPost post) { + if (isBlindWriter) { + List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4); + List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50); + List usedAnonymousProfileImageIds = lastFourAnonymousPostProfiles.stream() + .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); + List usedAnonymousNicknames = lastFiftyAnonymousPostProfiles.stream() + .map(AnonymousPostProfile::getNickname).toList(); + + AnonymousNickname anonymousNickname = anonymousNicknameRetriever.findRandomAnonymousNickname(usedAnonymousNicknames); + AnonymousProfileImage anonymousProfileImage = anonymousProfileImageRetriever.getAnonymousProfileImage(usedAnonymousProfileImageIds); + anonymousPostProfileModifier.createAnonymousPostProfile(member, anonymousNickname, anonymousProfileImage, post); + } + } +} diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java new file mode 100644 index 00000000..76b83fc9 --- /dev/null +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java @@ -0,0 +1,43 @@ +package org.sopt.makers.internal.community.service.anonymous; + +import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; +import org.sopt.makers.internal.exception.BusinessLogicException; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Component +@RequiredArgsConstructor +public class AnonymousProfileImageRetriever { + + private final AnonymousProfileImageRepository anonymousProfileImageRepository; + + private static final Map profileImageMap = new HashMap<>(); + private final static Long MAKERS_LOGO_IMAGE_ID = 6L; + + @PostConstruct + public void initializeProfileImageMap() { + List anonymousProfileImages = anonymousProfileImageRepository.findAllByIdNot(MAKERS_LOGO_IMAGE_ID); + for (AnonymousProfileImage image : anonymousProfileImages) { + profileImageMap.put(image.getId(), image); + } + } + + public AnonymousProfileImage getAnonymousProfileImage(List recentUsedAnonymousProfileImageIds) { + if (recentUsedAnonymousProfileImageIds.isEmpty()) { + long randomImageNumber = (long) ((Math.random() * 5) + 1); + return profileImageMap.get(randomImageNumber); + } + + return profileImageMap.keySet().stream() + .filter(id -> !recentUsedAnonymousProfileImageIds.contains(id)) + .findFirst() + .map(profileImageMap::get) + .orElseThrow(() -> new BusinessLogicException("존재하지 않는 익명 프로필 ID 입니다.")); + } +} From 4a1aac2b73b322f2a493ae8e7f3369f536e71654 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Wed, 27 Nov 2024 18:59:48 +0900 Subject: [PATCH 11/28] =?UTF-8?q?[Refactor]:=20=EC=BB=A4=EB=AE=A4=EB=8B=88?= =?UTF-8?q?=ED=8B=B0=20=EA=B2=8C=EC=8B=9C=EA=B8=80=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EB=8B=A8=EC=9C=84=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../community/CommunityServiceUnitTest.java | 101 ++++++++++++++++++ 1 file changed, 101 insertions(+) create mode 100644 src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java diff --git a/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java b/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java new file mode 100644 index 00000000..00d55056 --- /dev/null +++ b/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java @@ -0,0 +1,101 @@ +package org.sopt.makers.internal.service.community; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; +import org.sopt.makers.internal.community.domain.CommunityPost; +import org.sopt.makers.internal.community.service.CommunityPostModifier; +import org.sopt.makers.internal.community.service.CommunityPostService; +import org.sopt.makers.internal.community.service.anonymous.AnonymousPostProfileService; +import org.sopt.makers.internal.domain.Member; +import org.sopt.makers.internal.dto.community.PostSaveResponse; +import org.sopt.makers.internal.mapper.CommunityResponseMapper; +import org.sopt.makers.internal.member.service.MemberRetriever; + +import java.time.LocalDateTime; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class CommunityServiceUnitTest { + + @InjectMocks + private CommunityPostService communityPostService; + + @Mock + private MemberRetriever memberRetriever; + + @Mock + private CommunityPostModifier communityPostModifier; + + @Mock + private AnonymousPostProfileService anonymousPostProfileService; + + @Mock + private CommunityResponseMapper communityResponseMapper; + + @Test + @DisplayName("커뮤니티 게시글 생성") + public void testCreateCommunityPostWithBlindWriter() { + // Given + Long writerId = 1L; + PostSaveRequest request = new PostSaveRequest( + 1L, + "Sample Title", + "Sample Content", + true, + true, + new String[]{"image1.png", "image2.png"} + ); + + Member mockMember = Member.builder() + .id(writerId) + .build(); + + CommunityPost mockPost = CommunityPost.builder() + .id(1L) + .member(mockMember) + .categoryId(1L) + .title("Sample Title") + .content("Sample Content") + .images(new String[]{"image1.png", "image2.png"}) + .isQuestion(true) + .isBlindWriter(true) + .build(); + + PostSaveResponse mockResponse = new PostSaveResponse( + 1L, + 1L, + "Sample Title", + "Sample Content", + 0, + new String[]{"image1.jpg", "image2.jpg"}, + true, + true, + LocalDateTime.now() + ); + + when(memberRetriever.findMemberById(writerId)).thenReturn(mockMember); + when(communityPostModifier.createCommunityPost(mockMember, request)).thenReturn(mockPost); + doNothing().when(anonymousPostProfileService).createAnonymousPostProfile(true, mockMember, mockPost); + when(communityResponseMapper.toPostSaveResponse(mockPost)).thenReturn(mockResponse); + + // When + PostSaveResponse response = communityPostService.createPost(writerId, request); + + // Then + verify(memberRetriever).findMemberById(writerId); + verify(communityPostModifier).createCommunityPost(mockMember, request); + verify(anonymousPostProfileService).createAnonymousPostProfile(true, mockMember, mockPost); + verify(communityResponseMapper).toPostSaveResponse(mockPost); + + assertNotNull(response); + assertEquals(mockResponse, response); + } +} From 4559e1c64447c8d25a6e8a8e8514f0654b639c2f Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Thu, 28 Nov 2024 21:31:57 +0900 Subject: [PATCH 12/28] =?UTF-8?q?[Refactor]:=20=EB=8F=84=EB=A9=94=EC=9D=B8?= =?UTF-8?q?=20=EC=A0=95=EC=9D=98=EC=97=90=20=EB=A7=9E=EA=B2=8C=20=ED=8C=A8?= =?UTF-8?q?=ED=82=A4=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../domain/anonymous}/AnonymousNickname.java | 11 ++-- .../anonymous}/AnonymousPostProfile.java | 3 +- .../AnonymousProfileImage.java | 7 ++- .../AnonymousNicknameRepository.java | 2 +- .../AnonymousPostProfileRepository.java | 3 +- .../AnonymousPostProfileRepositoryCustom.java | 2 +- ...nymousPostProfileRepositoryCustomImpl.java | 2 +- .../AnonymousProfileImageRepository.java | 2 +- .../service/AnonymousProfileImageService.java | 2 +- .../service/CommunityPostService.java | 1 + .../anonymous/AnonymousNicknameRetriever.java | 2 +- .../AnonymousPostProfileModifier.java | 6 +-- .../AnonymousPostProfileRetriever.java | 2 +- .../AnonymousPostProfileService.java | 6 +-- .../AnonymousProfileImageRetriever.java | 2 +- .../community/AnonymousCommentProfile.java | 5 +- .../domain/community/AnonymousProfileImg.java | 52 ------------------- .../mapper/CommunityResponseMapper.java | 2 +- 18 files changed, 30 insertions(+), 82 deletions(-) rename src/main/java/org/sopt/makers/internal/{domain/community => community/domain/anonymous}/AnonymousNickname.java (66%) rename src/main/java/org/sopt/makers/internal/{domain/community => community/domain/anonymous}/AnonymousPostProfile.java (93%) rename src/main/java/org/sopt/makers/internal/community/domain/{ => anonymous}/AnonymousProfileImage.java (54%) delete mode 100644 src/main/java/org/sopt/makers/internal/domain/community/AnonymousProfileImg.java diff --git a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousNickname.java b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java similarity index 66% rename from src/main/java/org/sopt/makers/internal/domain/community/AnonymousNickname.java rename to src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java index b47e9742..54191b9f 100644 --- a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousNickname.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java @@ -1,4 +1,4 @@ -package org.sopt.makers.internal.domain.community; +package org.sopt.makers.internal.community.domain.anonymous; import javax.persistence.Column; import javax.persistence.Entity; @@ -6,18 +6,17 @@ import javax.persistence.GenerationType; import javax.persistence.Id; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; @Entity @Getter +@Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) @AllArgsConstructor(access = AccessLevel.PROTECTED) public class AnonymousNickname { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "anonymous_nickname_id") private Long id; diff --git a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousPostProfile.java similarity index 93% rename from src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java rename to src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousPostProfile.java index 91e46185..d6f56542 100644 --- a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousPostProfile.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousPostProfile.java @@ -1,4 +1,4 @@ -package org.sopt.makers.internal.domain.community; +package org.sopt.makers.internal.community.domain.anonymous; import javax.persistence.Column; import javax.persistence.ConstraintMode; @@ -11,7 +11,6 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToOne; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.common.AuditingTimeEntity; diff --git a/src/main/java/org/sopt/makers/internal/community/domain/AnonymousProfileImage.java b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java similarity index 54% rename from src/main/java/org/sopt/makers/internal/community/domain/AnonymousProfileImage.java rename to src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java index 802f0da0..958880d0 100644 --- a/src/main/java/org/sopt/makers/internal/community/domain/AnonymousProfileImage.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java @@ -1,11 +1,14 @@ -package org.sopt.makers.internal.community.domain; +package org.sopt.makers.internal.community.domain.anonymous; -import lombok.Getter; +import lombok.*; import javax.persistence.*; @Entity @Getter +@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor(access = AccessLevel.PROTECTED) public class AnonymousProfileImage { @Id diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java index e8a7470d..81813731 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousNicknameRepository.java @@ -2,7 +2,7 @@ import java.util.List; -import org.sopt.makers.internal.domain.community.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java index a634b205..f9d589df 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepository.java @@ -1,10 +1,9 @@ package org.sopt.makers.internal.community.repository.anonymous; -import java.util.List; import java.util.Optional; import org.sopt.makers.internal.domain.Member; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.sopt.makers.internal.community.domain.CommunityPost; import org.springframework.data.jpa.repository.JpaRepository; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java index a5e23b84..60c909a2 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustom.java @@ -1,6 +1,6 @@ package org.sopt.makers.internal.community.repository.anonymous; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java index 3fbfcc65..11702375 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java @@ -2,7 +2,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.sopt.makers.internal.domain.community.QAnonymousPostProfile; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java index 887bb3ea..c2ff6a52 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousProfileImageRepository.java @@ -1,6 +1,6 @@ package org.sopt.makers.internal.community.repository.anonymous; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java b/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java index 0aa64085..5c725b0a 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java @@ -1,6 +1,6 @@ package org.sopt.makers.internal.community.service; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 13a39109..1e900989 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -9,6 +9,7 @@ import org.sopt.makers.internal.community.controller.dto.request.PostSaveRequest; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.community.domain.CommunityPostLike; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.sopt.makers.internal.community.repository.CommunityPostLikeRepository; import org.sopt.makers.internal.community.repository.CommunityPostRepository; import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java index aca7b7ff..3b32171c 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousNicknameRetriever.java @@ -2,7 +2,7 @@ import lombok.RequiredArgsConstructor; import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; -import org.sopt.makers.internal.domain.community.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; import org.springframework.stereotype.Component; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java index e4a2c230..f2498cea 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileModifier.java @@ -1,12 +1,12 @@ package org.sopt.makers.internal.community.service.anonymous; import lombok.RequiredArgsConstructor; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; import org.sopt.makers.internal.domain.Member; -import org.sopt.makers.internal.domain.community.AnonymousNickname; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.springframework.stereotype.Component; @Component diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java index 5a11466b..dd143296 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileRetriever.java @@ -2,7 +2,7 @@ import lombok.RequiredArgsConstructor; import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.springframework.stereotype.Component; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index a97b29f8..f874d1b8 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -1,11 +1,11 @@ package org.sopt.makers.internal.community.service.anonymous; import lombok.RequiredArgsConstructor; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.domain.Member; -import org.sopt.makers.internal.domain.community.AnonymousNickname; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.springframework.stereotype.Component; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java index 76b83fc9..05c69326 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousProfileImageRetriever.java @@ -1,7 +1,7 @@ package org.sopt.makers.internal.community.service.anonymous; import lombok.RequiredArgsConstructor; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; import org.sopt.makers.internal.exception.BusinessLogicException; import org.springframework.stereotype.Component; diff --git a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousCommentProfile.java b/src/main/java/org/sopt/makers/internal/domain/community/AnonymousCommentProfile.java index 6e1e0a1a..41cc14f7 100644 --- a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousCommentProfile.java +++ b/src/main/java/org/sopt/makers/internal/domain/community/AnonymousCommentProfile.java @@ -3,8 +3,6 @@ import javax.persistence.Column; import javax.persistence.ConstraintMode; import javax.persistence.Entity; -import javax.persistence.EnumType; -import javax.persistence.Enumerated; import javax.persistence.ForeignKey; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; @@ -13,7 +11,8 @@ import javax.persistence.ManyToOne; import javax.persistence.OneToOne; -import org.sopt.makers.internal.community.domain.AnonymousProfileImage; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.common.AuditingTimeEntity; diff --git a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousProfileImg.java b/src/main/java/org/sopt/makers/internal/domain/community/AnonymousProfileImg.java deleted file mode 100644 index 5bd2af41..00000000 --- a/src/main/java/org/sopt/makers/internal/domain/community/AnonymousProfileImg.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.sopt.makers.internal.domain.community; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import lombok.Getter; - -@Getter -public enum AnonymousProfileImg { - // TODO 실제 S3 URL로 변경 - BLUE(1, "profile_blue.svg"), - ORANGE(2, "profile_orange.svg"), - PINK(3, "profile_pink.svg"), - SKYBLUE(4, "profile_skyblue.svg"), - YELLOW(5, "profile_yellow.svg"); - - private final int index; - private final String content; - - private static final Map profileImgMap = new HashMap<>(); - - AnonymousProfileImg(int index, String content) { - this.index = index; - this.content = content; - } - - static { - for (AnonymousProfileImg img : AnonymousProfileImg.values()) { - profileImgMap.put(img.index, img); - } - } - - - public static AnonymousProfileImg filtered(List excludes) { - return profileImgMap.keySet().stream() - .filter(i -> !excludes.contains(i)) - .findFirst() - .map(profileImgMap::get).orElse(null); - } - - public static AnonymousProfileImg shuffle(int index) { - return profileImgMap.get(index); - } - - public static AnonymousProfileImg getRandomProfileImg(List excludes) { - if (excludes.isEmpty() || excludes.size() >= AnonymousProfileImg.values().length) { - return shuffle((int)(Math.random() * 5)); - } - return filtered(excludes); - } -} diff --git a/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java b/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java index d8d678bb..d602af79 100644 --- a/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java +++ b/src/main/java/org/sopt/makers/internal/mapper/CommunityResponseMapper.java @@ -4,7 +4,7 @@ import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.MemberCareer; import org.sopt.makers.internal.domain.community.AnonymousCommentProfile; -import org.sopt.makers.internal.domain.community.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.sopt.makers.internal.community.domain.category.Category; import org.sopt.makers.internal.community.domain.CommunityPost; import org.sopt.makers.internal.dto.community.*; From 7904d59ef283fc1e37deccdf476d1aa8be983d2c Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Thu, 28 Nov 2024 21:47:55 +0900 Subject: [PATCH 13/28] =?UTF-8?q?[Refactor]:=20val=20=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=EC=A7=80=EC=96=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CommunityController.java에 있는 val 삭제 Related to: #559 --- .../internal/community/controller/CommunityController.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java b/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java index 22f69f08..2aa5a5f8 100644 --- a/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java +++ b/src/main/java/org/sopt/makers/internal/community/controller/CommunityController.java @@ -100,8 +100,10 @@ public ResponseEntity createPost( @Parameter(hidden = true) @AuthenticationPrincipal InternalMemberDetails memberDetails, @RequestBody @Valid PostSaveRequest request ) { - val response = communityPostService.createPost(memberDetails.getId(), request); - return ResponseEntity.status(HttpStatus.CREATED).body(response); + + return ResponseEntity.status(HttpStatus.CREATED).body( + communityPostService.createPost(memberDetails.getId(), request) + ); } @Operation(summary = "커뮤니티 글 수정") From 4d67c92ebb8e2aca62bb195c12e24ab2bb4e2ea9 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Thu, 28 Nov 2024 21:54:23 +0900 Subject: [PATCH 14/28] =?UTF-8?q?[Refactor]:=20=EC=82=AC=EC=9A=A9=EB=90=98?= =?UTF-8?q?=EC=A7=80=20=EC=95=8A=EB=8A=94=20=ED=8C=8C=EC=9D=BC=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- ...nymousPostProfileRepositoryCustomImpl.java | 2 +- .../service/AnonymousProfileImageService.java | 51 ------------------- .../service/CommunityCommentService.java | 5 +- .../service/CommunityPostService.java | 6 +-- 4 files changed, 5 insertions(+), 59 deletions(-) delete mode 100644 src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java diff --git a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java index 11702375..b5eabd17 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/anonymous/AnonymousPostProfileRepositoryCustomImpl.java @@ -3,7 +3,7 @@ import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; -import org.sopt.makers.internal.domain.community.QAnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.QAnonymousPostProfile; import java.util.List; diff --git a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java b/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java deleted file mode 100644 index 5c725b0a..00000000 --- a/src/main/java/org/sopt/makers/internal/community/service/AnonymousProfileImageService.java +++ /dev/null @@ -1,51 +0,0 @@ -package org.sopt.makers.internal.community.service; - -import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; -import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Service -public class AnonymousProfileImageService { - - private final AnonymousProfileImageRepository anonymousProfileImageRepository; - private final static Long MAKERS_LOGO_IMAGE_ID = 6L; - - public AnonymousProfileImageService(AnonymousProfileImageRepository anonymousProfileImageRepository) { - this.anonymousProfileImageRepository = anonymousProfileImageRepository; - initializeProfileImageMap(); - } - - private static final Map profileImageMap = new HashMap<>(); - - @Transactional(readOnly = true) - public AnonymousProfileImage getRandomProfileImage(List excludes) { - if (excludes.isEmpty() || excludes.size() >= profileImageMap.size()) { - return shuffle((long) (Math.random() * 5)); - } - return filtered(excludes); - } - - private AnonymousProfileImage filtered(List excludes) { - return profileImageMap.keySet().stream() - .filter(i -> !excludes.contains(i)) - .findFirst() - .map(profileImageMap::get) - .orElseGet(() -> shuffle((long) (Math.random() * 5))); - } - - private AnonymousProfileImage shuffle(Long index) { - return profileImageMap.get(index); - } - - private void initializeProfileImageMap() { - List anonymousProfileImages = anonymousProfileImageRepository.findAllByIdNot(MAKERS_LOGO_IMAGE_ID); - for (AnonymousProfileImage image : anonymousProfileImages) { - profileImageMap.put(image.getId(), image); - } - } -} diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java index 13e71484..5c724795 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityCommentService.java @@ -8,6 +8,7 @@ import org.apache.commons.lang3.StringUtils; import org.sopt.makers.internal.common.SlackMessageUtil; import org.sopt.makers.internal.community.service.anonymous.AnonymousNicknameRetriever; +import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; import org.sopt.makers.internal.domain.community.AnonymousCommentProfile; import org.sopt.makers.internal.domain.community.CommunityComment; import org.sopt.makers.internal.domain.community.ReportComment; @@ -45,6 +46,7 @@ @Slf4j public class CommunityCommentService { + private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; @Value("${spring.profiles.active}") private String activeProfile; private final DeletedCommunityCommentRepository deletedCommunityCommentRepository; @@ -59,7 +61,6 @@ public class CommunityCommentService { private final AnonymousPostProfileRepository anonymousPostProfileRepository; private final AnonymousNicknameRepository anonymousNicknameRepository; private final InternalApiService internalApiService; - private final AnonymousProfileImageService anonymousProfileImageService; private final PushNotificationService pushNotificationService; private final SlackMessageUtil slackMessageUtil; private final SlackClient slackClient; @@ -97,7 +98,7 @@ public void createComment(Long writerId, Long postId, CommentSaveRequest request if (request.isBlindWriter() && anonymousCommentProfile.isEmpty()) { anonymousCommentProfileRepository.save(AnonymousCommentProfile.builder() .nickname(member.equals(post.getMember()) ? anonymousPostProfile.get().getNickname() : anonymousNicknameRetriever.findRandomAnonymousNickname(excludeNickname)) - .profileImg(member.equals(post.getMember()) ? anonymousPostProfile.get().getProfileImg() : anonymousProfileImageService.getRandomProfileImage(excludeImgList)) + .profileImg(member.equals(post.getMember()) ? anonymousPostProfile.get().getProfileImg() : anonymousProfileImageRetriever.getAnonymousProfileImage(excludeImgList)) .member(member) .communityComment(comment) .build()); diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 1e900989..8ad833c3 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -15,10 +15,7 @@ import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; import org.sopt.makers.internal.community.repository.anonymous.AnonymousPostProfileRepository; import org.sopt.makers.internal.community.repository.category.CategoryRepository; -import org.sopt.makers.internal.community.service.anonymous.AnonymousNicknameRetriever; -import org.sopt.makers.internal.community.service.anonymous.AnonymousPostProfileModifier; -import org.sopt.makers.internal.community.service.anonymous.AnonymousPostProfileService; -import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; +import org.sopt.makers.internal.community.service.anonymous.*; import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.domain.community.*; import org.sopt.makers.internal.dto.community.*; @@ -76,7 +73,6 @@ public class CommunityPostService { private final MemberRepository memberRepository; private final MemberBlockRepository memberBlockRepository; - private final AnonymousProfileImageService anonymousProfileImageService; private final AnonymousPostProfileRepository anonymousPostProfileRepository; private final AnonymousNicknameRepository anonymousNicknameRepository; From 450ea1e4112387642e032b0a7c31f8408809c038 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Fri, 29 Nov 2024 07:24:07 +0900 Subject: [PATCH 15/28] =?UTF-8?q?[Refactor]:=20Qclass=20=EA=B2=BD=EB=A1=9C?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../community/repository/CommunityPostRepositoryCustomImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepositoryCustomImpl.java b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepositoryCustomImpl.java index cc395c32..84bbacce 100644 --- a/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepositoryCustomImpl.java +++ b/src/main/java/org/sopt/makers/internal/community/repository/CommunityPostRepositoryCustomImpl.java @@ -3,8 +3,8 @@ import com.querydsl.core.types.dsl.BooleanExpression; import com.querydsl.jpa.impl.JPAQueryFactory; import lombok.RequiredArgsConstructor; +import org.sopt.makers.internal.community.domain.QCommunityPost; import org.sopt.makers.internal.community.domain.category.QCategory; -import org.sopt.makers.internal.domain.community.QCommunityPost; import org.sopt.makers.internal.dto.community.PostCategoryDao; import org.sopt.makers.internal.dto.community.QPostCategoryDao; From 3d2c71ea9775b6cb930c8c3aa4873e2379ac6869 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 17:10:40 +0900 Subject: [PATCH 16/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20Map?= =?UTF-8?q?=20=EC=B4=88=EA=B8=B0=ED=99=94=20=ED=99=95=EC=9D=B8=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousPostProfileService.java | 4 +- .../AnonymousProfileImageRetrieverTest.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index f874d1b8..b646331a 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -6,11 +6,11 @@ import org.sopt.makers.internal.domain.Member; import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; -import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import java.util.List; -@Component +@Service @RequiredArgsConstructor public class AnonymousPostProfileService { diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java new file mode 100644 index 00000000..805c8f80 --- /dev/null +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java @@ -0,0 +1,47 @@ +package org.sopt.makers.internal.service.community.anonymous; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; +import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; + +import java.util.List; + +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class AnonymousProfileImageRetrieverTest { + + @InjectMocks + private AnonymousProfileImageRetriever anonymousProfileImageRetriever; + + @Mock + private AnonymousProfileImageRepository anonymousProfileImageRepository; + + private static final Long MAKERS_LOGO_IMAGE_ID = 6L; + + @Test + @DisplayName("initializeProfileImageMap 호출 시 profileImageMap이 올바르게 초기화된다.") + void initializeProfileImageMap_success() { + // Given + List dummyImages = List.of( + AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build(), + AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build(), + AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build(), + AnonymousProfileImage.builder().id(4L).imageUrl("Image4").build(), + AnonymousProfileImage.builder().id(5L).imageUrl("Image5").build() + ); + when(anonymousProfileImageRepository.findAllByIdNot(MAKERS_LOGO_IMAGE_ID)).thenReturn(dummyImages); + + // When + anonymousProfileImageRetriever.initializeProfileImageMap(); + + // Then + verify(anonymousProfileImageRepository, times(1)).findAllByIdNot(MAKERS_LOGO_IMAGE_ID); + } +} From 7aafe6ddf12c79e701e7382a3358128c5b06ef0b Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 17:19:12 +0900 Subject: [PATCH 17/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=9D=B4=EB=AF=B8=EC=A7=80=201~5?= =?UTF-8?q?=20=EC=82=AC=EC=9D=B4=EC=9D=98=20id=EA=B0=92=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousProfileImageRetrieverTest.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java index 805c8f80..acaeaf03 100644 --- a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java @@ -10,8 +10,10 @@ import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; +import java.util.Collections; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -44,4 +46,25 @@ void initializeProfileImageMap_success() { // Then verify(anonymousProfileImageRepository, times(1)).findAllByIdNot(MAKERS_LOGO_IMAGE_ID); } + + @Test + @DisplayName("recentUsedAnonymousProfileImageIds가 비어있을 때 랜덤 이미지를 반환한다.") + void getAnonymousProfileImage_randomSelection() { + // Given + List dummyImages = List.of( + AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build(), + AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build(), + AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build(), + AnonymousProfileImage.builder().id(4L).imageUrl("Image4").build(), + AnonymousProfileImage.builder().id(5L).imageUrl("Image5").build() + ); + when(anonymousProfileImageRepository.findAllByIdNot(6L)).thenReturn(dummyImages); + anonymousProfileImageRetriever.initializeProfileImageMap(); + + // When + AnonymousProfileImage randomImage = anonymousProfileImageRetriever.getAnonymousProfileImage(Collections.emptyList()); + + // Then + assertThat(randomImage.getId()).isBetween(1L, 5L); + } } From 0b3449f977a14e13afd849ff74f1d364928130f1 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 17:20:09 +0900 Subject: [PATCH 18/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=9D=B4=EB=AF=B8=EC=A7=80=201~5?= =?UTF-8?q?=20=EC=82=AC=EC=9D=B4=EC=9D=98=20id=EA=B0=92=20=ED=99=95?= =?UTF-8?q?=EC=9D=B8=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../anonymous/AnonymousProfileImageRetrieverTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java index acaeaf03..50ee857a 100644 --- a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java @@ -1,6 +1,7 @@ package org.sopt.makers.internal.service.community.anonymous; import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.RepeatedTest; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -47,7 +48,7 @@ void initializeProfileImageMap_success() { verify(anonymousProfileImageRepository, times(1)).findAllByIdNot(MAKERS_LOGO_IMAGE_ID); } - @Test + @RepeatedTest(10) @DisplayName("recentUsedAnonymousProfileImageIds가 비어있을 때 랜덤 이미지를 반환한다.") void getAnonymousProfileImage_randomSelection() { // Given From 491c716152c95ed6a05b6180ed69c696d4737c78 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 17:33:15 +0900 Subject: [PATCH 19/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=20=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousProfileImageRetrieverTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java index 50ee857a..54fbec1b 100644 --- a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousProfileImageRetrieverTest.java @@ -10,11 +10,13 @@ import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; import org.sopt.makers.internal.community.repository.anonymous.AnonymousProfileImageRepository; import org.sopt.makers.internal.community.service.anonymous.AnonymousProfileImageRetriever; +import org.sopt.makers.internal.exception.BusinessLogicException; import java.util.Collections; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) @@ -68,4 +70,48 @@ void getAnonymousProfileImage_randomSelection() { // Then assertThat(randomImage.getId()).isBetween(1L, 5L); } + + @Test + @DisplayName("recentUsedAnonymousProfileImageIds에 포함되지 않은 이미지를 반환한다.") + void getAnonymousProfileImage_excludeRecentIds() { + // Given + List dummyImages = List.of( + AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build(), + AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build(), + AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build(), + AnonymousProfileImage.builder().id(4L).imageUrl("Image4").build(), + AnonymousProfileImage.builder().id(5L).imageUrl("Image5").build() + ); + when(anonymousProfileImageRepository.findAllByIdNot(6L)).thenReturn(dummyImages); + anonymousProfileImageRetriever.initializeProfileImageMap(); + List recentIds = List.of(1L, 2L, 3L, 4L); + + // When + AnonymousProfileImage image = anonymousProfileImageRetriever.getAnonymousProfileImage(recentIds); + + // Then + assertThat(image.getId()).isIn(5L); + assertThat(image.getId()).isNotIn(recentIds); + } + + @Test + @DisplayName("모든 이미지 ID가 recentUsedAnonymousProfileImageIds에 포함되었을 때 예외를 던진다.") + void getAnonymousProfileImage_allIdsExcluded() { + // Given + List dummyImages = List.of( + AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build(), + AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build(), + AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build(), + AnonymousProfileImage.builder().id(4L).imageUrl("Image4").build(), + AnonymousProfileImage.builder().id(5L).imageUrl("Image5").build() + ); + when(anonymousProfileImageRepository.findAllByIdNot(6L)).thenReturn(dummyImages); + anonymousProfileImageRetriever.initializeProfileImageMap(); + List recentIds = List.of(1L, 2L, 3L, 4L, 5L); + + // When & Then + assertThatThrownBy(() -> anonymousProfileImageRetriever.getAnonymousProfileImage(recentIds)) + .isInstanceOf(BusinessLogicException.class) + .hasMessage("존재하지 않는 익명 프로필 ID 입니다."); + } } From cab2bd93932549d8e9bd75b36ef77c0405b62169 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 17:55:25 +0900 Subject: [PATCH 20/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EB=8B=89=EB=84=A4=EC=9E=84=20?= =?UTF-8?q?=EB=9E=9C=EB=8D=A4=20=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousNicknameRetrieverTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousNicknameRetrieverTest.java diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousNicknameRetrieverTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousNicknameRetrieverTest.java new file mode 100644 index 00000000..34b9327e --- /dev/null +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousNicknameRetrieverTest.java @@ -0,0 +1,69 @@ +package org.sopt.makers.internal.service.community.anonymous; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; +import org.sopt.makers.internal.community.repository.anonymous.AnonymousNicknameRepository; +import org.sopt.makers.internal.community.service.anonymous.AnonymousNicknameRetriever; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class AnonymousNicknameRetrieverTest { + + @InjectMocks + private AnonymousNicknameRetriever anonymousNicknameRetriever; + + @Mock + private AnonymousNicknameRepository anonymousNicknameRepository; + + + @Test + @DisplayName("recentUsedAnonymousNicknames가 비어있을 때 랜덤 닉네임을 반환한다.") + void findRandomAnonymousNickname_whenRecentUsedIsEmpty() { + // Given + AnonymousNickname randomNickname = AnonymousNickname.builder().id(1L).nickname("Nickname1").build(); + when(anonymousNicknameRepository.findRandomOne()).thenReturn(randomNickname); + + // When + AnonymousNickname result = anonymousNicknameRetriever.findRandomAnonymousNickname(List.of()); + + // Then + assertThat(result).isNotNull(); + assertThat(result.getId()).isEqualTo(1L); + assertThat(result.getNickname()).isEqualTo("Nickname1"); + verify(anonymousNicknameRepository, times(1)).findRandomOne(); + verify(anonymousNicknameRepository, never()).findRandomOneByIdNotIn(any()); + } + + @Test + @DisplayName("recentUsedAnonymousNicknames가 비어있지 않을 때 ID가 포함되지 않은 랜덤 닉네임을 반환한다.") + void findRandomAnonymousNickname_whenRecentUsedIsNotEmpty() { + // Given + List recentUsedNicknames = List.of( + AnonymousNickname.builder().id(1L).nickname("Nickname1").build(), + AnonymousNickname.builder().id(2L).nickname("Nickname2").build() + ); + AnonymousNickname randomNickname = AnonymousNickname.builder().id(3L).nickname("Nickname3").build(); + List excludedIds = recentUsedNicknames.stream().map(AnonymousNickname::getId).toList(); + + when(anonymousNicknameRepository.findRandomOneByIdNotIn(excludedIds)).thenReturn(randomNickname); + + // When + AnonymousNickname result = anonymousNicknameRetriever.findRandomAnonymousNickname(recentUsedNicknames); + + // Then + assertThat(result).isNotNull(); + assertThat(result.getId()).isEqualTo(3L); + assertThat(result.getNickname()).isEqualTo("Nickname3"); + verify(anonymousNicknameRepository, never()).findRandomOne(); + verify(anonymousNicknameRepository, times(1)).findRandomOneByIdNotIn(excludedIds); + } +} From 3dae0af319ce20cc220a8159311b88a7ab287003 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Sat, 30 Nov 2024 18:08:55 +0900 Subject: [PATCH 21/28] =?UTF-8?q?[Refactor]:=20=EA=B2=8C=EC=8B=9C=EB=AC=BC?= =?UTF-8?q?=20=EC=9D=B5=EB=AA=85=EC=9C=BC=EB=A1=9C=20=EC=83=9D=EC=84=B1=20?= =?UTF-8?q?=EC=8B=9C=20=EC=9D=B5=EB=AA=85=20=ED=94=84=EB=A1=9C=ED=95=84=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../AnonymousPostProfileServiceTest.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java new file mode 100644 index 00000000..e177696d --- /dev/null +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java @@ -0,0 +1,122 @@ +package org.sopt.makers.internal.service.community.anonymous; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.sopt.makers.internal.community.domain.CommunityPost; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; +import org.sopt.makers.internal.community.domain.anonymous.AnonymousProfileImage; +import org.sopt.makers.internal.community.service.anonymous.*; +import org.sopt.makers.internal.domain.Member; + +import java.util.List; + +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class AnonymousPostProfileServiceTest { + + @InjectMocks + private AnonymousPostProfileService anonymousPostProfileService; + + @Mock + private AnonymousPostProfileModifier anonymousPostProfileModifier; + + @Mock + private AnonymousPostProfileRetriever anonymousPostProfileRetriever; + + @Mock + private AnonymousProfileImageRetriever anonymousProfileImageRetriever; + + @Mock + private AnonymousNicknameRetriever anonymousNicknameRetriever; + + @Test + @DisplayName("isBlindWriter가 false일 경우 아무 작업도 수행되지 않는다.") + void createAnonymousPostProfile_whenIsBlindWriterFalse() { + // Given + Boolean isBlindWriter = false; + Member member = mock(Member.class); + CommunityPost post = mock(CommunityPost.class); + + // When + anonymousPostProfileService.createAnonymousPostProfile(isBlindWriter, member, post); + + // Then + verifyNoInteractions(anonymousPostProfileModifier, anonymousPostProfileRetriever, + anonymousProfileImageRetriever, anonymousNicknameRetriever); + } + + @Test + @DisplayName("isBlindWriter가 true일 경우 익명 게시물 프로필이 생성된다.") + void createAnonymousPostProfile_whenIsBlindWriterTrue_withBuilder() { + // Given + Boolean isBlindWriter = true; + Member member = mock(Member.class); + CommunityPost post = mock(CommunityPost.class); + + // AnonymousPostProfile의 최근 4개 및 50개의 프로필 Mock 데이터 생성 + List lastFourProfiles = List.of( + AnonymousPostProfile.builder() + .profileImg(AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build()) + .nickname(AnonymousNickname.builder().id(1L).nickname("Nickname1").build()) + .build(), + AnonymousPostProfile.builder() + .profileImg(AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build()) + .nickname(AnonymousNickname.builder().id(2L).nickname("Nickname2").build()) + .build() + ); + + List lastFiftyProfiles = List.of( + AnonymousPostProfile.builder() + .profileImg(AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build()) + .nickname(AnonymousNickname.builder().id(1L).nickname("Nickname1").build()) + .build(), + AnonymousPostProfile.builder() + .profileImg(AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build()) + .nickname(AnonymousNickname.builder().id(2L).nickname("Nickname2").build()) + .build(), + AnonymousPostProfile.builder() + .profileImg(AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build()) + .nickname(AnonymousNickname.builder().id(3L).nickname("Nickname3").build()) + .build() + ); + + // Mock 객체 동작 설정 + when(anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4)).thenReturn(lastFourProfiles); + when(anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50)).thenReturn(lastFiftyProfiles); + + // 최근 사용된 ID 및 닉네임 리스트 생성 + List usedImageIds = lastFourProfiles.stream() + .map(profile -> profile.getProfileImg().getId()).toList(); + List usedNicknames = lastFiftyProfiles.stream() + .map(AnonymousPostProfile::getNickname).toList(); + + // Mock 반환값 설정 + AnonymousNickname randomNickname = AnonymousNickname.builder() + .id(10L) + .nickname("RandomNickname") + .build(); + AnonymousProfileImage randomImage = AnonymousProfileImage.builder() + .id(10L) + .imageUrl("RandomImage") + .build(); + + when(anonymousNicknameRetriever.findRandomAnonymousNickname(usedNicknames)).thenReturn(randomNickname); + when(anonymousProfileImageRetriever.getAnonymousProfileImage(usedImageIds)).thenReturn(randomImage); + + // When + anonymousPostProfileService.createAnonymousPostProfile(isBlindWriter, member, post); + + // Then + verify(anonymousPostProfileRetriever, times(1)).getTopByOrderByCreatedAt(4); + verify(anonymousPostProfileRetriever, times(1)).getTopByOrderByCreatedAt(50); + verify(anonymousNicknameRetriever, times(1)).findRandomAnonymousNickname(usedNicknames); + verify(anonymousProfileImageRetriever, times(1)).getAnonymousProfileImage(usedImageIds); + verify(anonymousPostProfileModifier, times(1)).createAnonymousPostProfile(member, randomNickname, randomImage, post); + } +} From 6e527e10e58f33d09c68f0aef9d5c99c72cdd916 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:11:48 +0900 Subject: [PATCH 22/28] =?UTF-8?q?[Refactor]:=20PostSaveRequest.java=20@Val?= =?UTF-8?q?id=20message=20=ED=95=84=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 Related to: #559 --- .../controller/dto/request/PostSaveRequest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java index 89e48d57..97b7d833 100644 --- a/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java +++ b/src/main/java/org/sopt/makers/internal/community/controller/dto/request/PostSaveRequest.java @@ -7,25 +7,25 @@ public record PostSaveRequest( @Schema(required = true) - @NotNull + @NotNull(message = "카테고리 id는 필수 입력값입니다.") Long categoryId, String title, @Schema(required = true) - @NotBlank + @NotBlank(message = "게시글 본문은 공백일 수 없습니다.") String content, @Schema(required = true) - @NotNull + @NotNull(message = "질문글 여부 필드는 필수 입력값입니다.") Boolean isQuestion, @Schema(required = true) - @NotNull + @NotNull(message = "익명글 여부 필드는 필수 입력값입니다.") Boolean isBlindWriter, @Schema(required = true) - @NotNull + @NotNull(message = "이미지 필드는 필수 필드입니다.") String[] images ) { } From e22f3d897250982987e31e5cbc8bdd20850e95ea Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:16:04 +0900 Subject: [PATCH 23/28] =?UTF-8?q?[Refactor]:=20Domain=20=EA=B0=9D=EC=B2=B4?= =?UTF-8?q?=20=EC=83=9D=EC=84=B1=EB=B0=A9=EC=8B=9D=20=ED=86=B5=EC=9D=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../community/domain/anonymous/AnonymousNickname.java | 8 ++++++-- .../community/domain/anonymous/AnonymousProfileImage.java | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java index 54191b9f..c05b5a05 100644 --- a/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousNickname.java @@ -10,9 +10,7 @@ @Entity @Getter -@Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor(access = AccessLevel.PROTECTED) public class AnonymousNickname { @Id @@ -22,4 +20,10 @@ public class AnonymousNickname { @Column(nullable = false) String nickname; + + @Builder + private AnonymousNickname(Long id, String nickname) { + this.id = id; + this.nickname = nickname; + } } diff --git a/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java index 958880d0..4605c030 100644 --- a/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java +++ b/src/main/java/org/sopt/makers/internal/community/domain/anonymous/AnonymousProfileImage.java @@ -6,9 +6,7 @@ @Entity @Getter -@Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor(access = AccessLevel.PROTECTED) public class AnonymousProfileImage { @Id @@ -17,4 +15,10 @@ public class AnonymousProfileImage { @Column(nullable = false) private String imageUrl; + + @Builder + private AnonymousProfileImage(Long id, String imageUrl) { + this.id = id; + this.imageUrl = imageUrl; + } } From 4f9c01bbdaca53d87c4590e96c2abc5ea313122e Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:21:23 +0900 Subject: [PATCH 24/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=EA=B8=80?= =?UTF-8?q?=EC=97=90=20=EB=94=B0=EB=A5=B8=20=EB=B6=84=EA=B8=B0=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=A1=9C=EC=A7=81=20service=20=EA=B3=84=EC=B8=B5?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EB=8B=B4=EB=8B=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../service/CommunityPostService.java | 9 +++----- .../AnonymousPostProfileService.java | 22 +++++++++---------- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java index 8ad833c3..a374994a 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/CommunityPostService.java @@ -52,10 +52,6 @@ public class CommunityPostService { private final CommunityPostModifier communityPostModifier; - private final AnonymousPostProfileModifier anonymousPostProfileModifier; - private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; - private final AnonymousNicknameRetriever anonymousNicknameRetriever; - private final MemberRetriever memberRetriever; private final CommunityCommentRepository communityCommentRepository; @@ -74,7 +70,6 @@ public class CommunityPostService { private final MemberBlockRepository memberBlockRepository; private final AnonymousPostProfileRepository anonymousPostProfileRepository; - private final AnonymousNicknameRepository anonymousNicknameRepository; private final CommunityMapper communityMapper; private final CommunityResponseMapper communityResponseMapper; @@ -121,7 +116,9 @@ public CommunityPostMemberVo getPostById(Long memberId, Long postId, Boolean isB public PostSaveResponse createPost(Long writerId, PostSaveRequest request) { Member member = memberRetriever.findMemberById(writerId); CommunityPost post = communityPostModifier.createCommunityPost(member, request); - anonymousPostProfileService.createAnonymousPostProfile(request.isBlindWriter(), member, post); + if (request.isBlindWriter()) { + anonymousPostProfileService.createAnonymousPostProfile(member, post); + } // 비메이커스가 글 작성시 슬랙 발송 if (!MakersMemberId.getMakersMember().contains(member.getId()) && Objects.equals(activeProfile, "prod")) { diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index b646331a..d0b8cd3f 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -19,18 +19,16 @@ public class AnonymousPostProfileService { private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; private final AnonymousNicknameRetriever anonymousNicknameRetriever; - public void createAnonymousPostProfile(Boolean isBlindWriter, Member member, CommunityPost post) { - if (isBlindWriter) { - List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4); - List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50); - List usedAnonymousProfileImageIds = lastFourAnonymousPostProfiles.stream() - .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); - List usedAnonymousNicknames = lastFiftyAnonymousPostProfiles.stream() - .map(AnonymousPostProfile::getNickname).toList(); + public void createAnonymousPostProfile(Member member, CommunityPost post) { + List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4); + List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50); + List usedAnonymousProfileImageIds = lastFourAnonymousPostProfiles.stream() + .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); + List usedAnonymousNicknames = lastFiftyAnonymousPostProfiles.stream() + .map(AnonymousPostProfile::getNickname).toList(); - AnonymousNickname anonymousNickname = anonymousNicknameRetriever.findRandomAnonymousNickname(usedAnonymousNicknames); - AnonymousProfileImage anonymousProfileImage = anonymousProfileImageRetriever.getAnonymousProfileImage(usedAnonymousProfileImageIds); - anonymousPostProfileModifier.createAnonymousPostProfile(member, anonymousNickname, anonymousProfileImage, post); - } + AnonymousNickname anonymousNickname = anonymousNicknameRetriever.findRandomAnonymousNickname(usedAnonymousNicknames); + AnonymousProfileImage anonymousProfileImage = anonymousProfileImageRetriever.getAnonymousProfileImage(usedAnonymousProfileImageIds); + anonymousPostProfileModifier.createAnonymousPostProfile(member, anonymousNickname, anonymousProfileImage, post); } } From a6d9eeba972f51a06e42586fb5c36b2334f7b612 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:28:22 +0900 Subject: [PATCH 25/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=B5=9C=EC=8B=A0=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=A1=B0=ED=9A=8C=20limit=20=EC=83=81?= =?UTF-8?q?=EC=88=98=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../service/anonymous/AnonymousPostProfileService.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index d0b8cd3f..dd6f9c51 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -14,14 +14,17 @@ @RequiredArgsConstructor public class AnonymousPostProfileService { + private static final int RECENT_PROFILES_LIMIT = 4; + private static final int RECENT_PROFILES_LIMIT = 50; + private final AnonymousPostProfileModifier anonymousPostProfileModifier; private final AnonymousPostProfileRetriever anonymousPostProfileRetriever; private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; private final AnonymousNicknameRetriever anonymousNicknameRetriever; public void createAnonymousPostProfile(Member member, CommunityPost post) { - List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4); - List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50); + List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_PROFILES_LIMIT); + List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_PROFILES_LIMIT); List usedAnonymousProfileImageIds = lastFourAnonymousPostProfiles.stream() .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); List usedAnonymousNicknames = lastFiftyAnonymousPostProfiles.stream() From bfc19086e5f7cc2b1a03fb8a0b03bb16e4d8f8b2 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:29:55 +0900 Subject: [PATCH 26/28] =?UTF-8?q?[Refactor]:=20=EC=9D=B5=EB=AA=85=20?= =?UTF-8?q?=ED=94=84=EB=A1=9C=ED=95=84=20=EC=B5=9C=EC=8B=A0=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EC=A1=B0=ED=9A=8C=20limit=20=EC=83=81?= =?UTF-8?q?=EC=88=98=20=EC=84=A0=EC=96=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../service/anonymous/AnonymousPostProfileService.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index dd6f9c51..5ccd1621 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -14,8 +14,8 @@ @RequiredArgsConstructor public class AnonymousPostProfileService { - private static final int RECENT_PROFILES_LIMIT = 4; - private static final int RECENT_PROFILES_LIMIT = 50; + private static final int RECENT_IMAGE_LIMIT = 4; + private static final int RECENT_NICKNAME_LIMIT = 50; private final AnonymousPostProfileModifier anonymousPostProfileModifier; private final AnonymousPostProfileRetriever anonymousPostProfileRetriever; @@ -23,8 +23,8 @@ public class AnonymousPostProfileService { private final AnonymousNicknameRetriever anonymousNicknameRetriever; public void createAnonymousPostProfile(Member member, CommunityPost post) { - List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_PROFILES_LIMIT); - List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_PROFILES_LIMIT); + List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_IMAGE_LIMIT); + List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_NICKNAME_LIMIT); List usedAnonymousProfileImageIds = lastFourAnonymousPostProfiles.stream() .map(anonymousProfile -> anonymousProfile.getProfileImg().getId()).toList(); List usedAnonymousNicknames = lastFiftyAnonymousPostProfiles.stream() From 84f4195bf990d7fb64a39b2b57a6ce4a08112125 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:31:26 +0900 Subject: [PATCH 27/28] =?UTF-8?q?[Refactor]:=20=EC=84=9C=EB=B9=84=EC=8A=A4?= =?UTF-8?q?=20=EB=A0=88=EC=9D=B4=EC=96=B4=EC=97=90=20@Transactional=20?= =?UTF-8?q?=EC=96=B4=EB=85=B8=ED=85=8C=EC=9D=B4=EC=85=98=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../service/anonymous/AnonymousPostProfileService.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java index 5ccd1621..a25b7b01 100644 --- a/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java +++ b/src/main/java/org/sopt/makers/internal/community/service/anonymous/AnonymousPostProfileService.java @@ -7,6 +7,7 @@ import org.sopt.makers.internal.community.domain.anonymous.AnonymousNickname; import org.sopt.makers.internal.community.domain.anonymous.AnonymousPostProfile; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -22,6 +23,7 @@ public class AnonymousPostProfileService { private final AnonymousProfileImageRetriever anonymousProfileImageRetriever; private final AnonymousNicknameRetriever anonymousNicknameRetriever; + @Transactional public void createAnonymousPostProfile(Member member, CommunityPost post) { List lastFourAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_IMAGE_LIMIT); List lastFiftyAnonymousPostProfiles = anonymousPostProfileRetriever.getTopByOrderByCreatedAt(RECENT_NICKNAME_LIMIT); From e2e317f0664d2268a20c9f3666a17343fac14ea9 Mon Sep 17 00:00:00 2001 From: dev-Crayon Date: Mon, 16 Dec 2024 12:35:46 +0900 Subject: [PATCH 28/28] =?UTF-8?q?[Refactor]:=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=20=EC=BD=94=EB=93=9C=20=EB=8D=94=EB=AF=B8=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=20=EC=83=9D=EC=84=B1=20=ED=95=A8=EC=88=98=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Related to: #559 --- .../community/CommunityServiceUnitTest.java | 4 +- .../AnonymousPostProfileServiceTest.java | 117 +++++++++--------- 2 files changed, 58 insertions(+), 63 deletions(-) diff --git a/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java b/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java index 00d55056..bcc06a65 100644 --- a/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java +++ b/src/test/java/org/sopt/makers/internal/service/community/CommunityServiceUnitTest.java @@ -83,7 +83,7 @@ public void testCreateCommunityPostWithBlindWriter() { when(memberRetriever.findMemberById(writerId)).thenReturn(mockMember); when(communityPostModifier.createCommunityPost(mockMember, request)).thenReturn(mockPost); - doNothing().when(anonymousPostProfileService).createAnonymousPostProfile(true, mockMember, mockPost); + doNothing().when(anonymousPostProfileService).createAnonymousPostProfile(mockMember, mockPost); when(communityResponseMapper.toPostSaveResponse(mockPost)).thenReturn(mockResponse); // When @@ -92,7 +92,7 @@ public void testCreateCommunityPostWithBlindWriter() { // Then verify(memberRetriever).findMemberById(writerId); verify(communityPostModifier).createCommunityPost(mockMember, request); - verify(anonymousPostProfileService).createAnonymousPostProfile(true, mockMember, mockPost); + verify(anonymousPostProfileService).createAnonymousPostProfile(mockMember, mockPost); verify(communityResponseMapper).toPostSaveResponse(mockPost); assertNotNull(response); diff --git a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java index e177696d..1d571062 100644 --- a/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java +++ b/src/test/java/org/sopt/makers/internal/service/community/anonymous/AnonymousPostProfileServiceTest.java @@ -36,87 +36,82 @@ public class AnonymousPostProfileServiceTest { private AnonymousNicknameRetriever anonymousNicknameRetriever; @Test - @DisplayName("isBlindWriter가 false일 경우 아무 작업도 수행되지 않는다.") - void createAnonymousPostProfile_whenIsBlindWriterFalse() { + @DisplayName("isBlindWriter가 true일 경우 익명 게시물 프로필이 생성된다.") + void createAnonymousPostProfile_whenIsBlindWriterTrue() { // Given - Boolean isBlindWriter = false; Member member = mock(Member.class); CommunityPost post = mock(CommunityPost.class); - // When - anonymousPostProfileService.createAnonymousPostProfile(isBlindWriter, member, post); + List lastFourProfiles = createAnonymousPostProfiles(List.of( + new ProfileData(1L, "Image1", 1L, "Nickname1"), + new ProfileData(2L, "Image2", 2L, "Nickname2") + )); - // Then - verifyNoInteractions(anonymousPostProfileModifier, anonymousPostProfileRetriever, - anonymousProfileImageRetriever, anonymousNicknameRetriever); - } + List lastFiftyProfiles = createAnonymousPostProfiles(List.of( + new ProfileData(1L, "Image1", 1L, "Nickname1"), + new ProfileData(2L, "Image2", 2L, "Nickname2"), + new ProfileData(3L, "Image3", 3L, "Nickname3") + )); - @Test - @DisplayName("isBlindWriter가 true일 경우 익명 게시물 프로필이 생성된다.") - void createAnonymousPostProfile_whenIsBlindWriterTrue_withBuilder() { - // Given - Boolean isBlindWriter = true; - Member member = mock(Member.class); - CommunityPost post = mock(CommunityPost.class); - - // AnonymousPostProfile의 최근 4개 및 50개의 프로필 Mock 데이터 생성 - List lastFourProfiles = List.of( - AnonymousPostProfile.builder() - .profileImg(AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build()) - .nickname(AnonymousNickname.builder().id(1L).nickname("Nickname1").build()) - .build(), - AnonymousPostProfile.builder() - .profileImg(AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build()) - .nickname(AnonymousNickname.builder().id(2L).nickname("Nickname2").build()) - .build() - ); - - List lastFiftyProfiles = List.of( - AnonymousPostProfile.builder() - .profileImg(AnonymousProfileImage.builder().id(1L).imageUrl("Image1").build()) - .nickname(AnonymousNickname.builder().id(1L).nickname("Nickname1").build()) - .build(), - AnonymousPostProfile.builder() - .profileImg(AnonymousProfileImage.builder().id(2L).imageUrl("Image2").build()) - .nickname(AnonymousNickname.builder().id(2L).nickname("Nickname2").build()) - .build(), - AnonymousPostProfile.builder() - .profileImg(AnonymousProfileImage.builder().id(3L).imageUrl("Image3").build()) - .nickname(AnonymousNickname.builder().id(3L).nickname("Nickname3").build()) - .build() - ); - - // Mock 객체 동작 설정 when(anonymousPostProfileRetriever.getTopByOrderByCreatedAt(4)).thenReturn(lastFourProfiles); when(anonymousPostProfileRetriever.getTopByOrderByCreatedAt(50)).thenReturn(lastFiftyProfiles); - // 최근 사용된 ID 및 닉네임 리스트 생성 - List usedImageIds = lastFourProfiles.stream() - .map(profile -> profile.getProfileImg().getId()).toList(); - List usedNicknames = lastFiftyProfiles.stream() - .map(AnonymousPostProfile::getNickname).toList(); + List usedImageIds = extractProfileImageIds(lastFourProfiles); + List usedNicknames = extractNicknames(lastFiftyProfiles); - // Mock 반환값 설정 - AnonymousNickname randomNickname = AnonymousNickname.builder() - .id(10L) - .nickname("RandomNickname") - .build(); - AnonymousProfileImage randomImage = AnonymousProfileImage.builder() - .id(10L) - .imageUrl("RandomImage") - .build(); + AnonymousNickname randomNickname = createAnonymousNickname(10L, "RandomNickname"); + AnonymousProfileImage randomImage = createAnonymousProfileImage(10L, "RandomImage"); when(anonymousNicknameRetriever.findRandomAnonymousNickname(usedNicknames)).thenReturn(randomNickname); when(anonymousProfileImageRetriever.getAnonymousProfileImage(usedImageIds)).thenReturn(randomImage); // When - anonymousPostProfileService.createAnonymousPostProfile(isBlindWriter, member, post); + anonymousPostProfileService.createAnonymousPostProfile(member, post); // Then verify(anonymousPostProfileRetriever, times(1)).getTopByOrderByCreatedAt(4); verify(anonymousPostProfileRetriever, times(1)).getTopByOrderByCreatedAt(50); verify(anonymousNicknameRetriever, times(1)).findRandomAnonymousNickname(usedNicknames); verify(anonymousProfileImageRetriever, times(1)).getAnonymousProfileImage(usedImageIds); - verify(anonymousPostProfileModifier, times(1)).createAnonymousPostProfile(member, randomNickname, randomImage, post); + verify(anonymousPostProfileModifier, times(1)) + .createAnonymousPostProfile(member, randomNickname, randomImage, post); + } + + private List createAnonymousPostProfiles(List data) { + return data.stream() + .map(d -> AnonymousPostProfile.builder() + .profileImg(createAnonymousProfileImage(d.profileImageId, d.profileImageUrl)) + .nickname(createAnonymousNickname(d.nicknameId, d.nickname)) + .build()) + .toList(); + } + + private AnonymousNickname createAnonymousNickname(Long id, String nickname) { + return AnonymousNickname.builder() + .id(id) + .nickname(nickname) + .build(); + } + + private AnonymousProfileImage createAnonymousProfileImage(Long id, String imageUrl) { + return AnonymousProfileImage.builder() + .id(id) + .imageUrl(imageUrl) + .build(); + } + + private List extractProfileImageIds(List profiles) { + return profiles.stream() + .map(profile -> profile.getProfileImg().getId()) + .toList(); + } + + private List extractNicknames(List profiles) { + return profiles.stream() + .map(AnonymousPostProfile::getNickname) + .toList(); + } + + private record ProfileData(Long profileImageId, String profileImageUrl, Long nicknameId, String nickname) { } }