22
33import UMC .news .newsIntelligent .domain .news .converter .NewsConverter ;
44import UMC .news .newsIntelligent .domain .news .dto .NewsResponseDTO ;
5+ //import UMC.news.newsIntelligent.domain.news.entity.News;
56import UMC .news .newsIntelligent .domain .news .entity .News ;
67import UMC .news .newsIntelligent .domain .news .entity .PressLogo ;
78import UMC .news .newsIntelligent .domain .news .repository .NewsRepository ;
89import UMC .news .newsIntelligent .domain .news .repository .PressLogoRepository ;
10+ import UMC .news .newsIntelligent .domain .topic .entity .Topic ;
11+ import UMC .news .newsIntelligent .domain .topic .repository .TopicRepository ;
912import UMC .news .newsIntelligent .global .apiPayload .code .error .ErrorCode ;
1013import UMC .news .newsIntelligent .global .apiPayload .exception .CustomException ;
1114import lombok .RequiredArgsConstructor ;
@@ -25,6 +28,7 @@ public class NewsQueryServiceImpl implements NewsQueryService{
2528 private static final int MAX_PAGE_SIZE = 20 ;
2629 private static final String DEFAULT_LOGO = null ; // 기본 로고
2730
31+ private final TopicRepository topicRepository ;
2832 private final NewsRepository newsRepository ;
2933 private final PressLogoRepository pressLogoRepository ;
3034
@@ -51,55 +55,32 @@ public NewsResponseDTO.NewsResDTO getRelatedNews(Long topicId, Long lastId, int
5155 return new NewsResponseDTO .NewsResDTO (content , totalCount , newLastId , pageSize , hasNext );
5256 }
5357
54- // 최신 수정 보도 (조건 만족 + 같은 topic_id로 3개 이상 중 '기사 수가 가장많은' 토픽 1개)
58+ // 최신 수정 보도 (조건 만족 + 같은 topic_id로 1개 이상인 토픽 1개)
5559 @ Override
5660 public NewsResponseDTO .TopicQualifiedItemResDTO getLatestTopicNews () throws CustomException {
57- List <News > latestNews = newsRepository .findArticlesOfTopTopicByCount ();
58-
59- if (latestNews .isEmpty ()) {
60- throw new CustomException (ErrorCode .LATEST_NEWS_NOT_FOUND );
61+ Long qualifiedTopic = newsRepository .pickTopTopicIdByQualifiedNews ();
62+ if (qualifiedTopic == null ) {
63+ throw new CustomException (ErrorCode .TOPIC_NOT_FOUND );
6164 }
6265
63- // 최신순 정렬
64- latestNews .sort (
65- Comparator .comparing (News ::getPublishDate )
66- .thenComparing (News ::getId )
67- .reversed ()
68- );
69-
70- Long topicId = latestNews .get (0 ).getTopic ().getId ();
71-
72- // 대표 기사: 정렬 결과의 첫 번째(= 가장 최신 기사)
73- News main = latestNews .get (0 );
66+ Topic topic = topicRepository .findById (qualifiedTopic )
67+ .orElseThrow (() -> new CustomException (ErrorCode .TOPIC_NOT_FOUND ));
7468
75- List <NewsResponseDTO .NewsRelatedArticleDto > related = NewsConverter .toRelatedDtos (latestNews );
69+ List <News > relatedNewsEntity = newsRepository .findTop3QualifiedNewsByTopicId (qualifiedTopic );
70+ if (relatedNewsEntity .isEmpty ()) {
71+ throw new CustomException (ErrorCode .LATEST_NEWS_NOT_FOUND );
72+ }
73+ List <NewsResponseDTO .NewsRelatedArticleDto > relatedNews =
74+ NewsConverter .toRelatedDtos (relatedNewsEntity );
7675
7776 return new NewsResponseDTO .TopicQualifiedItemResDTO (
78- topicId ,
79- main . getTitle (),
80- main . getNewsSummary (),
81- main . getTopic () .getImageUrl (),
82- main . getPublishDate (),
83- related
77+ topic . getId () ,
78+ topic . getTopicName (),
79+ topic . getAiSummary (),
80+ topic .getImageUrl (),
81+ topic . getSummaryTime (),
82+ relatedNews
8483 );
85-
86- // // topic_id 단위로 그룹핑
87- // Map<Long, List<News>> byTopic = latestNews.stream()
88- // .collect(Collectors.groupingBy(n -> n.getTopic().getId(), LinkedHashMap::new, Collectors.toList()));
89- //
90- // // 개수가 가장 많은 토픽 그룹 선택 (동률이면 첫 그룹)
91- // Map.Entry<Long, List<News>> mostCountGroup = byTopic.entrySet().stream()
92- // .max(Comparator.comparingInt(e -> e.getValue().size()))
93- // .orElseThrow(() -> new CustomException(GeneralErrorCode.LATEST_NEWS_NOT_FOUND));
94- //
95- // Long topicId = mostCountGroup.getKey();
96- // List<News> items = mostCountGroup.getValue();
97- //
98- // // 대표 기사: id가 가장 큰 기사
99- // News main = items.stream()
100- // .max(Comparator.comparing(News::getId))
101- // .orElse(items.get(0));
102-
10384 }
10485
10586 private static int normalizeSize (int size ) {
@@ -135,4 +116,4 @@ private static String normPress(String s) {
135116 if (t .isEmpty ()) return null ;
136117 return Normalizer .normalize (t , Normalizer .Form .NFC );
137118 }
138- }
119+ }
0 commit comments