Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#592 [feat] 글/ 글모임 메타데이터용 get 구현 #593

Merged
merged 2 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public interface MoimRepository extends JpaRepository<Moim, Long> {
@Query("select m from Moim m join fetch m.owner where m.id = :id")
Optional<Moim> findById(final long id);

@Query("select m from Moim m")
List<Moim> 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)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ public boolean validateNormalizedName(final String normalizedName) {
return !moimRepository.existsByNormalizedName(normalizedName);
}


public List<Moim> findAll() {
return moimRepository.findAll();
}
public Optional<Moim> findByOwner(final WriterName writerName) {
return moimRepository.findByOwner(writerName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -365,4 +366,8 @@ public void deleteMoim(
writerNameRemover.setWriterNameMoimNull(moim.getOwner());
moimRemover.deleteMoim(moim);
}

public PostDataResponse getAllPostMoimMap() {
return postRetriever.getAllPostDataByMoim(moimRetriever.findAll());
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,6 +16,13 @@
public interface PostRepository extends JpaRepository<Post, Long>, 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<Post> findAllByMoim(final Moim moim);
List<Post> findByTopic(final Topic topic);

int countByWriterNameId(final Long writerNameId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +103,7 @@ public MoimCuriousPostListResponse getMostCuriousPostByMoim(final Moim moim) {
List<Post> 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());
}

Expand All @@ -120,4 +121,17 @@ public List<Post> findAllByTopics(
.flatMap(topic -> postRepository.findByTopic(topic).stream())
.collect(Collectors.toList());
}

public PostDataResponse getAllPostDataByMoim(final List<Moim> moimList) {
return PostDataResponse.of(
moimList.stream().filter(moim -> {
List<Post> posts = postRepository.findAllByMoim(moim);
return posts != null && !posts.isEmpty();
})
.collect(Collectors.toMap(
moim -> moim, // 키: Moim 객체 그대로 사용
postRepository::findAllByMoim // 값: findAllByMoim 결과 리스트
))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,6 +40,7 @@
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, List<String>> postListMoimMap
) {
public static PostDataResponse of(Map<Moim, List<Post>> post) {
return new PostDataResponse(post.entrySet()
.stream()
.collect(Collectors.toMap(
entry -> entry.getKey().getIdUrl(),
entry -> entry.getValue()
.stream()
.map(Post::getIdUrl)
.collect(Collectors.toList())
)));
}
}
Loading