Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #125 from News-Sseuk/FIX#45
Browse files Browse the repository at this point in the history
[FIX] 이슈가 저장되어 있지 않은 기사의 경우, 예외처리
  • Loading branch information
gahyun02 authored Nov 17, 2024
2 parents 40ed680 + 0e0e14e commit 3eed94f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import backend.newssseuk.domain.article.Article;
import backend.newssseuk.domain.article.repository.JpaArticleRepository;
import backend.newssseuk.domain.articleHashTag.ArticleHashTag;
import backend.newssseuk.domain.articleHashTag.service.ArticleHashTagService;
import backend.newssseuk.domain.hashTag.HashTag;
import backend.newssseuk.domain.recommendedArticle.dto.RecommendedArticleUpdateDto;
import backend.newssseuk.domain.relatedArticle.RelatedArticleRepository;
import backend.newssseuk.domain.user.User;
Expand All @@ -21,7 +19,6 @@
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package backend.newssseuk.domain.relatedArticle;

import backend.newssseuk.domain.article.Article;
import backend.newssseuk.domain.article.ArticleHelper;
import backend.newssseuk.domain.article.repository.JpaArticleRepository;
import backend.newssseuk.domain.relatedArticle.redis.RelatedArticleRedisCachingService;
import backend.newssseuk.domain.relatedArticle.redis.RelatedArticleRedisEntity;
import backend.newssseuk.domain.relatedArticle.redis.RelatedArticleRedisRepository;
import backend.newssseuk.springbootmongodb.ArticleService;
import backend.newssseuk.springbootmongodb.dto.ArticleResponseDto;
import backend.newssseuk.springbootmongodb.dto.ArticleThumbnailDTO;
import backend.newssseuk.springbootmongodb.redis.ArticleRedisEntity;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -20,24 +14,27 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class RelatedArticleService {
private final JpaArticleRepository jpaArticleRepository;
private final RelatedArticleRepository relatedArticleRepository;
private final RelatedArticleRedisRepository relatedArticleRedisRepository;
private final ArticleService articleService;

@Transactional
public List<ArticleThumbnailDTO> collectingRelatedArticles(String nosql_article_id) throws Exception {
Article article = jpaArticleRepository.findByNosqlId(nosql_article_id).orElse(null);
String fullUrl = "http://52.78.251.30:80/article/each?nosql_id=" + URLEncoder.encode(String.valueOf(nosql_article_id), "UTF-8");
if (saveRelatedArticleId(fullUrl) == null){
return null;
}
List<Long> mysqlIdList = saveRelatedArticleId(fullUrl)
.stream()
.map(nosql_id -> jpaArticleRepository.findByNosqlId(nosql_id).get().getId()).toList();

try {
RelatedArticle relatedArticle = relatedArticleRepository.findByArticle(article);
relatedArticle.setArticleList(mysqlIdList);
Expand All @@ -51,19 +48,6 @@ public List<ArticleThumbnailDTO> collectingRelatedArticles(String nosql_article_
.map(mysqlId -> jpaArticleRepository.findById(mysqlId).orElseThrow())
.toList());
}
// RelatedArticleRedisEntity relatedArticleRedisEntity = relatedArticleRedisRepository.findByArticleId(article.getId());
// // 레디스에 저장된 값이 있을 때
// if (relatedArticleRedisEntity != null){
// return relatedArticleRedisEntity.getArticleList();
// } else { // 레디스에 저장된 값이 없을 때
// List<Article> articleResponseList = relatedArticle.getArticleList().stream()
// .map(articleId -> jpaArticleRepository.findById(articleId)
// .orElseThrow()) // 예외 발생 시, 기본적으로 NoSuchElementException 던짐.
// .toList();
// List<ArticleRedisEntity> articleList = articleResponseList.stream()
// .map(mysqlArticle -> mysqlArticle.getMongoEntity(articleHelper))
// .collect(Collectors.toList());
// return relatedArticleRedisCachingService.cashingRelatedArticle(article, articleList).getArticleList();

@Transactional
public void addRelatedArticle(Long articleId, List<Long> articleList){
Expand Down Expand Up @@ -104,6 +88,11 @@ public List<String> saveRelatedArticleId(String fullUrl) throws Exception {
}
in.close();

// 응답이 비어 있거나 null일 경우 빈 리스트 반환
if (response.length() == 0) {
return new ArrayList<>(); // 빈 리스트 반환
}

// JSON 응답을 List<String>으로 변환
ObjectMapper objectMapper = new ObjectMapper();
List<String> relatedArticleIds = objectMapper.readValue(response.toString(), List.class);
Expand Down

0 comments on commit 3eed94f

Please sign in to comment.