Skip to content
Closed
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
@@ -0,0 +1,10 @@
package in.koreatech.koin.domain.community.article.dto;

public record ArticleSummary(
Integer id,
String title
) {
public static ArticleSummary of(Integer id, String title) {
return new ArticleSummary(id, title);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;

import in.koreatech.koin.domain.community.article.dto.ArticleSummary;
import in.koreatech.koin.domain.community.article.exception.ArticleNotFoundException;
import in.koreatech.koin.domain.community.article.exception.BoardNotFoundException;
import in.koreatech.koin.domain.community.article.model.Article;
Expand All @@ -31,6 +32,13 @@ public interface ArticleRepository extends Repository<Article, Integer> {

List<Article> findAllByIdIn(Collection<Integer> articleIds);

@Query(value = """
SELECT new in.koreatech.koin.domain.community.article.dto.ArticleSummary(a.id, a.title)
FROM Article a
WHERE a.id IN :articleIds
""")
List<ArticleSummary> findAllSummaryByIdIn(@Param("articleIds") Collection<Integer> articleIds);

Page<Article> findAll(Pageable pageable);

Page<Article> findAllByBoardIdNot(Integer boardId, PageRequest pageRequest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import in.koreatech.koin.common.event.ArticleKeywordEvent;
import in.koreatech.koin.domain.community.article.dto.ArticleKeywordResult;
import in.koreatech.koin.domain.community.article.dto.ArticleSummary;
import in.koreatech.koin.domain.community.article.model.Article;
import in.koreatech.koin.domain.community.article.repository.ArticleRepository;
import in.koreatech.koin.domain.community.keyword.dto.ArticleKeywordCreateRequest;
Expand Down Expand Up @@ -150,8 +151,8 @@ public void sendKeywordNotification(KeywordNotificationRequest request) {
return;
}

List<Article> articles = articleRepository.findAllByIdIn(request.updateNotification());
List<ArticleKeywordEvent> keywordEvents = keywordExtractor.matchKeyword(articles, null);
List<ArticleSummary> articleSummaries = articleRepository.findAllSummaryByIdIn(request.updateNotification());
List<ArticleKeywordEvent> keywordEvents = keywordExtractor.matchKeyword(articleSummaries, null);
for (ArticleKeywordEvent event : keywordEvents) {
eventPublisher.publishEvent(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,42 @@

import org.springframework.stereotype.Component;

import in.koreatech.koin.domain.community.article.model.Article;
import in.koreatech.koin.domain.community.article.dto.ArticleSummary;
import in.koreatech.koin.domain.community.keyword.model.ArticleKeywordUserMap;
import in.koreatech.koin.domain.community.keyword.model.KeywordMatchResult;

@Component
public class KeywordExtractor {

public List<KeywordMatchResult> matchKeyword(
List<Article> articles, List<ArticleKeywordUserMap> articleKeywordUserMaps, Integer authorId
List<ArticleSummary> articleSummaries, List<ArticleKeywordUserMap> articleKeywordUserMaps, Integer authorId
) {
Set<KeywordMatchResult> keywordMatchResults = new HashSet<>();

for (Article article : articles) {
for (ArticleSummary articleSummary : articleSummaries) {
for (ArticleKeywordUserMap articleKeywordUserMap : articleKeywordUserMaps) {
if (isMatchable(article, articleKeywordUserMap, authorId)) {
addOrUpdateResult(keywordMatchResults, article, articleKeywordUserMap);
if (isMatchable(articleSummary, articleKeywordUserMap, authorId)) {
addOrUpdateResult(keywordMatchResults, articleSummary, articleKeywordUserMap);
}
}
}

return keywordMatchResults.stream().toList();
}

private boolean isMatchable(Article article, ArticleKeywordUserMap articleKeywordUserMap, Integer authorId) {
private boolean isMatchable(
ArticleSummary articleSummaries, ArticleKeywordUserMap articleKeywordUserMap, Integer authorId
) {
return !Objects.equals(articleKeywordUserMap.getUserId(), authorId)
&& article.getTitle().contains(articleKeywordUserMap.getKeyword());
&& articleSummaries.title().contains(articleKeywordUserMap.getKeyword());
}

private void addOrUpdateResult(
Set<KeywordMatchResult> keywordMatchResults, Article article, ArticleKeywordUserMap articleKeywordUserMap
Set<KeywordMatchResult> keywordMatchResults, ArticleSummary ArticleSummary,
ArticleKeywordUserMap articleKeywordUserMap
) {
KeywordMatchResult keywordMatchResult = KeywordMatchResult.of(
article.getId(), articleKeywordUserMap.getUserId(), articleKeywordUserMap.getKeyword()
ArticleSummary.id(), articleKeywordUserMap.getUserId(), articleKeywordUserMap.getKeyword()
);

keywordMatchResults.stream()
Expand Down
Loading