diff --git a/module-api/src/main/java/com/mile/controller/external/InternalController.java b/module-api/src/main/java/com/mile/controller/external/InternalController.java index 991d2159..dcabf3b5 100644 --- a/module-api/src/main/java/com/mile/controller/external/InternalController.java +++ b/module-api/src/main/java/com/mile/controller/external/InternalController.java @@ -1,7 +1,12 @@ package com.mile.controller.external; import com.mile.common.CacheService; +import com.mile.dto.SuccessResponse; +import com.mile.exception.message.SuccessMessage; +import com.mile.moim.service.MoimService; import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @@ -10,9 +15,15 @@ public class InternalController { private final CacheService cacheService; + private final MoimService moimService; @PostMapping("/api/v1/moim/info/cache") public void deleteMoimInfoCache() { cacheService.deleteMoimCache(); } + + @GetMapping("/api/internal/post-data") + public ResponseEntity getPostAndMoimIdForMetaData() { + return ResponseEntity.ok(SuccessResponse.of(SuccessMessage.MOIM_POST_MAP_GET_SUCCESS, moimService.getAllPostMoimMap())); + } } diff --git a/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java b/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java index ab18bf94..22ff3a5e 100644 --- a/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java +++ b/module-common/src/main/java/com/mile/exception/message/SuccessMessage.java @@ -54,6 +54,7 @@ public enum SuccessMessage { WRITER_NAME_DESCRIPTION_UPDATE_SUCCESS(HttpStatus.OK.value(), "소개글 수정이 완료 되었습니다."), MOIM_PUBLIC_STATUS_GET_SUCCESS(HttpStatus.OK.value(), "글모임 공개여부 조회가 완료되었습니다."), MOIM_DELETE_SUCCESS(HttpStatus.OK.value(), "글모임 삭제가 완료되었습니다."), + MOIM_POST_MAP_GET_SUCCESS(HttpStatus.OK.value(), "메타데이터를 위한 글-글모임 전체데이터가 조회가 완료되었습니다"), /* 201 CREATED */ diff --git a/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java b/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java index 85b7fa5f..057d0f5d 100644 --- a/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java +++ b/module-domain/src/main/java/com/mile/moim/repository/MoimRepository.java @@ -16,6 +16,9 @@ public interface MoimRepository extends JpaRepository { @Query("select m from Moim m join fetch m.owner where m.id = :id") Optional findById(final long id); + @Query("select m from Moim m") + List findAll(); + Boolean existsByNormalizedName(final String normalizedName); @Query("SELECT m FROM Post p JOIN p.topic t JOIN t.moim m WHERE m.isPublic = true AND p.isTemporary = false AND p.createdAt BETWEEN :startOfWeek AND :endOfWeek GROUP BY m ORDER BY COUNT(p)") diff --git a/module-domain/src/main/java/com/mile/moim/service/MoimRetriever.java b/module-domain/src/main/java/com/mile/moim/service/MoimRetriever.java index f3da8cd1..2e92ef1b 100644 --- a/module-domain/src/main/java/com/mile/moim/service/MoimRetriever.java +++ b/module-domain/src/main/java/com/mile/moim/service/MoimRetriever.java @@ -84,6 +84,10 @@ public boolean validateNormalizedName(final String normalizedName) { return !moimRepository.existsByNormalizedName(normalizedName); } + + public List findAll() { + return moimRepository.findAll(); + } public Optional findByOwner(final WriterName writerName) { return moimRepository.findByOwner(writerName); } diff --git a/module-domain/src/main/java/com/mile/moim/service/MoimService.java b/module-domain/src/main/java/com/mile/moim/service/MoimService.java index 630cfdb4..19bdbbb9 100644 --- a/module-domain/src/main/java/com/mile/moim/service/MoimService.java +++ b/module-domain/src/main/java/com/mile/moim/service/MoimService.java @@ -37,6 +37,7 @@ import com.mile.moim.service.popular.MoimPopularInfoService; import com.mile.post.domain.Post; import com.mile.post.service.PostRetriever; +import com.mile.post.service.dto.response.PostDataResponse; import com.mile.topic.service.TopicCreator; import com.mile.topic.service.TopicRemover; import com.mile.topic.service.TopicRetriever; @@ -365,4 +366,8 @@ public void deleteMoim( writerNameRemover.setWriterNameMoimNull(moim.getOwner()); moimRemover.deleteMoim(moim); } + + public PostDataResponse getAllPostMoimMap() { + return postRetriever.getAllPostDataByMoim(moimRetriever.findAll()); + } } diff --git a/module-domain/src/main/java/com/mile/post/repository/PostRepository.java b/module-domain/src/main/java/com/mile/post/repository/PostRepository.java index 93cea13b..44a4b086 100644 --- a/module-domain/src/main/java/com/mile/post/repository/PostRepository.java +++ b/module-domain/src/main/java/com/mile/post/repository/PostRepository.java @@ -1,5 +1,6 @@ package com.mile.post.repository; +import com.mile.moim.domain.Moim; import com.mile.post.domain.Post; import com.mile.topic.domain.Topic; @@ -15,6 +16,13 @@ public interface PostRepository extends JpaRepository, PostRepositoryCustom { boolean existsPostByIdAndWriterNameId(final Long postId, final Long userId); + @Query(""" + SELECT p + FROM Post p + WHERE p.topic.moim = :moim + AND p.isTemporary = false + """) + List findAllByMoim(final Moim moim); List findByTopic(final Topic topic); int countByWriterNameId(final Long writerNameId); diff --git a/module-domain/src/main/java/com/mile/post/service/PostRetriever.java b/module-domain/src/main/java/com/mile/post/service/PostRetriever.java index 3012fd74..6e51a0c8 100644 --- a/module-domain/src/main/java/com/mile/post/service/PostRetriever.java +++ b/module-domain/src/main/java/com/mile/post/service/PostRetriever.java @@ -10,6 +10,7 @@ import com.mile.moim.service.dto.response.MoimMostCuriousPostResponse; import com.mile.post.domain.Post; import com.mile.post.repository.PostRepository; +import com.mile.post.service.dto.response.PostDataResponse; import com.mile.topic.domain.Topic; import com.mile.writername.domain.WriterName; import lombok.RequiredArgsConstructor; @@ -102,7 +103,7 @@ public MoimCuriousPostListResponse getMostCuriousPostByMoim(final Moim moim) { List postList = getPostHaveCuriousCount(postRepository.findTop2ByMoimOrderByCuriousCountDesc(moim)); return MoimCuriousPostListResponse.of(postList .stream() - .map(p->MoimMostCuriousPostResponse.of(MoimCuriousPost.of(p)) + .map(p -> MoimMostCuriousPostResponse.of(MoimCuriousPost.of(p)) ).toList()); } @@ -120,4 +121,17 @@ public List findAllByTopics( .flatMap(topic -> postRepository.findByTopic(topic).stream()) .collect(Collectors.toList()); } + + public PostDataResponse getAllPostDataByMoim(final List moimList) { + return PostDataResponse.of( + moimList.stream().filter(moim -> { + List posts = postRepository.findAllByMoim(moim); + return posts != null && !posts.isEmpty(); + }) + .collect(Collectors.toMap( + moim -> moim, // 키: Moim 객체 그대로 사용 + postRepository::findAllByMoim // 값: findAllByMoim 결과 리스트 + )) + ); + } } diff --git a/module-domain/src/main/java/com/mile/post/service/PostService.java b/module-domain/src/main/java/com/mile/post/service/PostService.java index 85a66f2c..f7982df0 100644 --- a/module-domain/src/main/java/com/mile/post/service/PostService.java +++ b/module-domain/src/main/java/com/mile/post/service/PostService.java @@ -17,6 +17,7 @@ import com.mile.post.service.dto.response.ModifyPostGetResponse; import com.mile.post.service.dto.request.PostCreateRequest; import com.mile.post.service.dto.response.PostCuriousResponse; +import com.mile.post.service.dto.response.PostDataResponse; import com.mile.post.service.dto.response.PostGetResponse; import com.mile.post.service.dto.request.PostPutRequest; import com.mile.post.service.dto.request.TemporaryPostCreateRequest; @@ -39,6 +40,7 @@ import java.util.Base64; import java.util.HashMap; import java.util.List; +import java.util.stream.Collectors; @Service diff --git a/module-domain/src/main/java/com/mile/post/service/dto/response/PostDataResponse.java b/module-domain/src/main/java/com/mile/post/service/dto/response/PostDataResponse.java new file mode 100644 index 00000000..41c6f486 --- /dev/null +++ b/module-domain/src/main/java/com/mile/post/service/dto/response/PostDataResponse.java @@ -0,0 +1,24 @@ +package com.mile.post.service.dto.response; + +import com.mile.moim.domain.Moim; +import com.mile.post.domain.Post; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public record PostDataResponse( + Map> postListMoimMap +) { + public static PostDataResponse of(Map> post) { + return new PostDataResponse(post.entrySet() + .stream() + .collect(Collectors.toMap( + entry -> entry.getKey().getIdUrl(), + entry -> entry.getValue() + .stream() + .map(Post::getIdUrl) + .collect(Collectors.toList()) + ))); + } +}