Skip to content

Commit

Permalink
Merge pull request #71 from NewsFit-jolp/feat/get-headline
Browse files Browse the repository at this point in the history
Feat/get headline
  • Loading branch information
k000927 authored Nov 4, 2024
2 parents f53b537 + 2fc9e35 commit 509da93
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,17 @@ public SuccessResponse<String> rateArticle(@PathVariable("articleId") String art
""")
@GetMapping("/recommend")
public String recommendArticles(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize) throws JsonProcessingException {
@RequestParam(value = "pageSize", required = false, defaultValue = "5") int pageSize) throws JsonProcessingException {
return articleService.recommendArticles(page, pageSize);
}

@Operation(summary = "헤드라인 조회",
description = """
헤드라인 기사들을 조회합니다.
헤드라인은 데이터베이스에 존재하는 기사들 중 좋아요가 가장 많은 상위 5개의 기사입니다.
""")
@GetMapping("/headLine")
public SuccessResponse<List<GetArticles>> getHeadLine() throws JsonProcessingException {
return SuccessResponse.success(articleService.getHeadLine());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.ColumnDefault;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class Article extends BaseEntity {

private String articleSource;
private LocalDateTime publishDate;
@Setter
private String headLine;

@OneToMany(mappedBy = "article", fetch = FetchType.EAGER, cascade = CascadeType.REMOVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import com.example.newsfit.domain.article.entity.Press;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
Expand Down Expand Up @@ -47,4 +45,7 @@ List<Article> findAllByTitleOrCategoryContaining(
@Query("SELECT a FROM Article a " +
"WHERE a.createdDate < :yesterday")
List<Article> findOldArticle(@Param("yesterday") LocalDateTime yesterday);

@Query("SELECT a FROM Article a ORDER BY a.likeCount DESC")
List<Article> findTopArticlesByLikeCount(Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ public GetArticles postArticle(String requestBody) throws ParseException {
JSONArray imageArray = (JSONArray) jsonObject.get("image");
List<String> images = new ArrayList<>();
String articleSource = (String) jsonObject.get("articleSource");
String headLine = (String) jsonObject.get("headLine");
LocalDateTime publishDate = LocalDateTime.parse((String) jsonObject.get("publishDate"));

if (imageArray != null) {
Expand All @@ -79,7 +78,6 @@ public GetArticles postArticle(String requestBody) throws ParseException {
.category(category)
.images(images)
.articleSource(articleSource)
.headLine(headLine)
.publishDate(publishDate)
.build();

Expand Down Expand Up @@ -147,14 +145,15 @@ public GetArticle getArticle(String articleId) throws JsonProcessingException {
.orElseThrow(() -> new CustomException(ErrorCode.ARTICLE_NOT_FOUND));

if (article.getContent() == null) {
article.summaryArticle(summaryArticle(article));
Map<String, Object> response = summaryArticle(article);
article.summaryArticle(response.get("content").toString());
}

Boolean isLikedArticle = articleLikesRepository.existsByMember_OAuthIdAndArticle(SecurityContextHolder.getContext().getAuthentication().getName(), article);
return GetArticle.of(article, isLikedArticle);
}

private String summaryArticle(Article article) throws JsonProcessingException {
private Map<String, Object> summaryArticle(Article article) throws JsonProcessingException {
String url = article.getArticleSource();
ArticleSource articleSource = articleSourceRepository.findByUrl(url);

Expand All @@ -175,7 +174,7 @@ private String summaryArticle(Article article) throws JsonProcessingException {
if (response.getStatusCode() == HttpStatus.OK) {
String responseBody = response.getBody();
Map<String, Object> responseMap = objectMapper.readValue(responseBody, Map.class);
return responseMap.get("content").toString();
return responseMap;
} else {
throw new RuntimeException("Failed to send POST request: " + response.getStatusCode());
}
Expand Down Expand Up @@ -236,45 +235,45 @@ public Boolean deleteArticleLikes(String articleId) {
return true;
}

public Boolean postCommentLikes(String articleId, String commentId) {
Comment comment = commentRepository.findById(Long.parseLong(commentId))
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));

Member member = memberRepository.findByOAuthId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

if (commentLikesRepository.findByMemberAndComment(member, comment).isPresent()) {
throw new CustomException(ErrorCode.DUPLICATED_COMMENT_LIKE);
}

CommentLike commentLike = CommentLike.builder()
.comment(comment)
.member(member)
.build();

commentLikesRepository.save(commentLike);
comment.addLikeCount();

return true;
}

public Boolean deleteCommentLikes(String articleId, String commentId) {
Comment comment = commentRepository.findById(Long.parseLong(commentId))
.orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));

Member member = memberRepository.findByOAuthId(SecurityContextHolder.getContext().getAuthentication().getName())
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

Optional<Integer> removeLike = commentLikesRepository.removeByMemberAndComment(member, comment);

if (removeLike.isPresent() && removeLike.get() == 0) {
throw new CustomException(ErrorCode.ARTICLE_LIKE_NOT_FOUND);
}

comment.subLikeCount();

return true;
}
// public Boolean postCommentLikes(String articleId, String commentId) {
// Comment comment = commentRepository.findById(Long.parseLong(commentId))
// .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
//
// Member member = memberRepository.findByOAuthId(SecurityContextHolder.getContext().getAuthentication().getName())
// .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
//
// if (commentLikesRepository.findByMemberAndComment(member, comment).isPresent()) {
// throw new CustomException(ErrorCode.DUPLICATED_COMMENT_LIKE);
// }
//
// CommentLike commentLike = CommentLike.builder()
// .comment(comment)
// .member(member)
// .build();
//
// commentLikesRepository.save(commentLike);
// comment.addLikeCount();
//
// return true;
// }
//
// public Boolean deleteCommentLikes(String articleId, String commentId) {
// Comment comment = commentRepository.findById(Long.parseLong(commentId))
// .orElseThrow(() -> new CustomException(ErrorCode.COMMENT_NOT_FOUND));
//
// Member member = memberRepository.findByOAuthId(SecurityContextHolder.getContext().getAuthentication().getName())
// .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
//
// Optional<Integer> removeLike = commentLikesRepository.removeByMemberAndComment(member, comment);
//
// if (removeLike.isPresent() && removeLike.get() == 0) {
// throw new CustomException(ErrorCode.ARTICLE_LIKE_NOT_FOUND);
// }
//
// comment.subLikeCount();
//
// return true;
// }

private List<Article> getArticlesByCursor(Category category, Long articleId, int size, List<Press> preferredPress) {
Pageable pageable = PageRequest.of(0, size);
Expand Down Expand Up @@ -323,5 +322,20 @@ public String recommendArticles(int page, int pageSize) throws JsonProcessingExc

return recommenderUtils.recommendArticles(member.getId(), page, pageSize);
}

public List<GetArticles> getHeadLine() throws JsonProcessingException {
Pageable pageable = PageRequest.of(0, 5);
List<Article> headLineArticles = articleRepository.findTopArticlesByLikeCount(pageable);

List<GetArticles> getArticles = new ArrayList<>();
for (Article headLineArticle : headLineArticles) {
if(headLineArticle.getHeadLine() == null) {
Map<String, Object> response = summaryArticle(headLineArticle);
headLineArticle.setHeadLine(response.get("subtitle").toString());
}
getArticles.add(GetArticles.of(headLineArticle));
}
return getArticles;
}
}

0 comments on commit 509da93

Please sign in to comment.