Skip to content

Commit

Permalink
Merge branch 'dev' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
gouyeonch committed Oct 15, 2024
2 parents 4bbfc7e + 5a1a226 commit 870cc63
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kkokkomu.short_news.comment.repository;

import com.kkokkomu.short_news.comment.domain.Comment;
import com.kkokkomu.short_news.news.domain.NewsReaction;
import com.kkokkomu.short_news.user.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -228,4 +229,16 @@ List<Comment> findByUserAndCommentLike(
@Param("user") User user
);

// 최신순 댓글 단 댓글 조회
@Query("SELECT c FROM Comment c " +
"WHERE c.user.id = :userId " +
"ORDER BY c.id")
Page<Comment> findAllByUserAndCorsorFirst(Long userId, Pageable pageable);

// 최신순 댓글 단 댓글 조회 초기화
@Query("SELECT c FROM Comment c " +
"WHERE c.user.id = :userId " +
"AND c.id > :cursorId " +
"ORDER BY c.id")
Page<Comment> findAllByUserAndCorsor(@Param("userId") Long userId, @Param("cursorId") Long cursorId, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.kkokkomu.short_news.comment.dto.comment.response.*;
import com.kkokkomu.short_news.core.config.service.RedisService;
import com.kkokkomu.short_news.news.domain.News;
import com.kkokkomu.short_news.news.domain.NewsReaction;
import com.kkokkomu.short_news.news.service.NewsLookupService;
import com.kkokkomu.short_news.user.domain.User;
import com.kkokkomu.short_news.comment.dto.comment.request.CreateCommentDto;
Expand Down Expand Up @@ -470,5 +471,26 @@ public CursorResponseDto<List<ReplyListDto>> guestReadOldestReply(Long parentId,
return CursorResponseDto.fromEntityAndPageInfo(replyListDtos, cursorInfoDto);
} // 비로그인 오래된순 대댓글 조회

/* 관리자 */
/* 유틸 */
@Transactional(readOnly = true)
public Page<Comment> getNewsCommentByCursor(Long userId, Long cursorId, int size) {
log.info("getNewsCommentByCursor service");

PageRequest pageRequest = PageRequest.of(0, size);

Page<Comment> results;
if (cursorId == null) {
// 최초
results = commentRepository.findAllByUserAndCorsorFirst(userId, pageRequest);
} else {
// 그 이후
if (!commentRepository.existsById(cursorId)) {
throw new CommonException(ErrorCode.NOT_FOUND_CURSOR);
}

results = commentRepository.findAllByUserAndCorsor(userId, cursorId, pageRequest);
}

return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.kkokkomu.short_news.core.dto.ResponseDto;
import com.kkokkomu.short_news.news.dto.news.response.NewsInfoDto;
import com.kkokkomu.short_news.news.dto.news.response.SearchNewsDto;
import com.kkokkomu.short_news.news.dto.newsHist.response.CommentHistInfoDto;
import com.kkokkomu.short_news.news.dto.newsHist.response.NewsHistInfoDto;
import com.kkokkomu.short_news.news.service.NewsLogService;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -26,7 +27,7 @@ public class NewsLogController {

@Operation(summary = "댓글 달았던 뉴스 조회")
@GetMapping("/commented")
public ResponseDto<CursorResponseDto<List<SearchNewsDto>>> readCommentedNews(
public ResponseDto<CursorResponseDto<List<CommentHistInfoDto>>> readCommentedNews(
@UserId Long userId,
@RequestParam(value = "cursorId", required = false) Long cursorId,
@RequestParam("size") int size
Expand All @@ -37,7 +38,7 @@ public ResponseDto<CursorResponseDto<List<SearchNewsDto>>> readCommentedNews(

@Operation(summary = "감정표현한 뉴스 조회")
@GetMapping("/reaction")
public ResponseDto<CursorResponseDto<List<NewsHistInfoDto>>> readReactionNews(
public ResponseDto<CursorResponseDto<List<NewsInfoDto>>> readReactionNews(
@UserId Long userId,
@RequestParam(value = "cursorId", required = false) Long cursorId,
@RequestParam("size") int size
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.kkokkomu.short_news.news.dto.newsHist.response;

import com.kkokkomu.short_news.comment.dto.comment.response.CommentDto;
import com.kkokkomu.short_news.news.dto.news.response.NewsInfoDto;
import lombok.Builder;

@Builder
public record CommentHistInfoDto(
CommentDto comment,
NewsInfoDto news
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import com.kkokkomu.short_news.news.domain.News;
import com.kkokkomu.short_news.news.domain.NewsReaction;
import com.kkokkomu.short_news.news.domain.NewsViewHist;
import com.kkokkomu.short_news.user.domain.User;
import com.kkokkomu.short_news.core.type.ENewsReaction;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;
Expand All @@ -19,5 +24,16 @@ public interface NewsReactionRepository extends JpaRepository<NewsReaction, Long

Optional<NewsReaction> findByNewsAndUser(News news, User user);

@Query("SELECT nr FROM NewsReaction nr " +
"WHERE nr.user.id = :userId " +
"ORDER BY nr.id DESC")
Page<NewsReaction> findAllByUserAndCorsorFirst(Long userId, Pageable pageable);

@Query("SELECT nr FROM NewsReaction nr " +
"WHERE nr.user.id = :userId " +
"AND nr.id > :cursorId " +
"ORDER BY nr.id DESC")
Page<NewsReaction> findAllByUserAndCorsor(@Param("userId") Long userId, @Param("cursorId") Long cursorId, Pageable pageable);

void deleteByNewsAndUserAndReaction(News news, User user, ENewsReaction reaction);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ public interface NewsViewHistRepository extends JpaRepository<NewsViewHist, Long

void deleteAllByUser(User user);

// 뉴스 시청기록 최신순 조회
@Query("SELECT nh FROM NewsViewHist nh " +
"WHERE nh.user.id = :userId " +
"ORDER BY nh.id DESC")
"ORDER BY nh.id ")
Page<NewsViewHist> findAllByUserAndCorsorFirst(Long userId, Pageable pageable);

// 뉴스 시청기록 최신순 조회 초기화
@Query("SELECT nh FROM NewsViewHist nh " +
"WHERE nh.user.id = :userId " +
"AND nh.id < :cursorId " +
"ORDER BY nh.id DESC")
"AND nh.id > :cursorId " +
"ORDER BY nh.id ")
Page<NewsViewHist> findAllByUserAndCorsor(@Param("userId") Long userId, @Param("cursorId") Long cursorId, Pageable pageable);

// 중복 체크를 위한 쿼리
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public List<GenerateNewsDto> generateNewsList(CreateGenerateNewsDto createGenera

List<News> newsListAll = newsRepository.findAll();
for (News news : newsListAll) {
news.addScore(-1 * topScore);
news.addScore(topScore);
}
newsRepository.saveAll(newsListAll);

Expand Down Expand Up @@ -256,11 +256,12 @@ public List<GenerateNewsDto> generateNews(CreateGenerateNewsDto createGenerateNe
// 랭킹 초기화
log.info("news ranking rese");
News topNews = newsRepository.findTopByOrderByScoreDesc();
log.info("top news {}", topNews.getId());
Double topScore = topNews.getScore() * -1;

List<News> newsListAll = newsRepository.findAll();
for (News news : newsListAll) {
news.addScore(-1 * topScore);
news.addScore(topScore);
}
newsRepository.saveAll(newsListAll);

Expand Down Expand Up @@ -333,7 +334,7 @@ public List<GenerateNewsDto> generateNews(CreateGenerateNewsDto createGenerateNe
for (GenerateNewsDto generateNewsDto : generateNewsDtos) {
if (generateNewsDto.newsDto() != null) {
content.append("<p> Title: ").append(generateNewsDto.newsDto().title()).append("</p>");
content.append("<p> URL: ").append(generateNewsDto.newsDto().shortformUrl()).append("</p>");
content.append("<p> URL: ").append(generateNewsDto.newsDto().shortformUrl().replace("kkm-shortform", "kkm-shortform-withad")).append("</p>");
content.append("<p> origin: ").append(generateNewsDto.newsDto().relatedUrl()).append("</p>");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.kkokkomu.short_news.news.service;

import com.kkokkomu.short_news.comment.domain.Comment;
import com.kkokkomu.short_news.comment.dto.comment.response.CommentDto;
import com.kkokkomu.short_news.comment.service.CommentService;
import com.kkokkomu.short_news.core.dto.CursorInfoDto;
import com.kkokkomu.short_news.core.dto.CursorResponseDto;
import com.kkokkomu.short_news.core.exception.CommonException;
import com.kkokkomu.short_news.core.exception.ErrorCode;
import com.kkokkomu.short_news.news.domain.News;
import com.kkokkomu.short_news.news.domain.NewsReaction;
import com.kkokkomu.short_news.news.domain.NewsViewHist;
import com.kkokkomu.short_news.news.dto.news.response.NewsInfoDto;
import com.kkokkomu.short_news.news.dto.news.response.SearchNewsDto;
import com.kkokkomu.short_news.news.dto.newsHist.response.CommentHistInfoDto;
import com.kkokkomu.short_news.news.dto.newsHist.response.NewsHistInfoDto;
import com.kkokkomu.short_news.news.repository.NewsRepository;
import com.kkokkomu.short_news.news.repository.NewsViewHistRepository;
Expand All @@ -28,88 +33,68 @@
@RequiredArgsConstructor
@Slf4j
public class NewsLogService {
private final NewsRepository newsRepository;
private final NewsViewHistRepository newsViewHistRepository;

private final NewsViewHistService newsViewHistService;
private final UserLookupService userLookupService;
private final SearchNewsService searchNewsService;
private final NewsViewHistRepository newsViewHistRepository;
private final NewsReactionService newsReactionService;
private final NewsLookupService newsLookupService;
private final CommentService commentService;

public CursorResponseDto<List<SearchNewsDto>> getNewsWithComment(Long userId, Long cursorId, int size) {
@Transactional(readOnly = true)
public CursorResponseDto<List<CommentHistInfoDto>> getNewsWithComment(Long userId, Long cursorId, int size) {
log.info("getNewsWithComment service");

User user = userLookupService.findUserById(userId);

// 커서 아이디에 해당하는 뉴스가 있는지 검사
if (cursorId != null && !newsRepository.existsById(cursorId)) {
throw new CommonException(ErrorCode.NOT_FOUND_CURSOR);
}

PageRequest pageRequest = PageRequest.of(0, size);

// 캐싱 히스토리 db 동기화
newsViewHistService.updateNewsHist(userId);

List<News> news;
Page<News> results;
if (cursorId == null) {
// 최초
results = newsRepository.findFirstPageNewsByUserCommentsOrderByIdDesc(user, pageRequest);
} else {
// 그 이후
results = newsRepository.findNewsByUserCommentsAndIdLessThanOrderByIdDesc(user, cursorId, pageRequest);
}
news = results.getContent();
// 유저가 감정표현한 뉴스들 조회 by cursot
Page<Comment> reactionsByCursor = commentService.getNewsCommentByCursor(userId, cursorId, size);
List<Comment> comments = reactionsByCursor.getContent();

List<SearchNewsDto> searchNewsDtos = SearchNewsDto.of(news);
List<CommentHistInfoDto> searchNewsDtos = getCommentHistInfo(userId, comments);

CursorInfoDto cursorInfoDto = CursorInfoDto.fromPageInfo(results);
CursorInfoDto cursorInfoDto = CursorInfoDto.fromPageInfo(reactionsByCursor);

return CursorResponseDto.fromEntityAndPageInfo(searchNewsDtos, cursorInfoDto);
} // 댓글 달았던 뉴스 조회
} // 댓글 뉴스 조회

@Transactional(readOnly = true)
public CursorResponseDto<List<NewsHistInfoDto>> getNewsWithReaction(Long userId, Long cursorId, int size) {
public CursorResponseDto<List<NewsInfoDto>> getNewsWithReaction(Long userId, Long cursorId, int size) {
log.info("getNewsWithReaction service");

User user = userLookupService.findUserById(userId);

// 커서 아이디에 해당하는 뉴스가 있는지 검사
if (cursorId != null && !newsRepository.existsById(cursorId)) {
throw new CommonException(ErrorCode.NOT_FOUND_CURSOR);
}

PageRequest pageRequest = PageRequest.of(0, size);

// 캐싱 히스토리 db 동기화
newsViewHistService.updateNewsHist(userId);

List<NewsViewHist> news;
Page<NewsViewHist> results;
if (cursorId == null) {
// 최초
results = newsViewHistRepository.findAllByUserAndCorsorFirst(userId, pageRequest);
} else {
// 그 이후
results = newsViewHistRepository.findAllByUserAndCorsor(userId, cursorId, pageRequest);
}
news = results.getContent();
// 유저가 감정표현한 뉴스들 조회 by cursot
Page<NewsReaction> reactionsByCursor = newsReactionService.getNewsReactionsByCursor(userId, cursorId, size);

List<NewsHistInfoDto> searchNewsDtos = getNewsHistInfo(news);
List<News> newsList = reactionsByCursor.stream()
.map(r -> r.getNews())
.toList();

CursorInfoDto cursorInfoDto = CursorInfoDto.fromPageInfo(results);
// 뉴스들 기반 시청기록 조회
List<NewsInfoDto> newsHistList = searchNewsService.getNewsInfo(newsList, userId);

return CursorResponseDto.fromEntityAndPageInfo(searchNewsDtos, cursorInfoDto);
CursorInfoDto cursorInfoDto = CursorInfoDto.fromPageInfo(reactionsByCursor);

return CursorResponseDto.fromEntityAndPageInfo(newsHistList, cursorInfoDto);
} // 감정표현한 뉴스 조회

@Transactional(readOnly = true)
public CursorResponseDto<List<NewsHistInfoDto>> getNewsWithHist(Long userId, Long cursorId, int size) {
log.info("getNewsWithHist service");
log.info("getNewsWithReaction service");

User user = userLookupService.findUserById(userId);

// 커서 아이디에 해당하는 뉴스가 있는지 검사
if (cursorId != null && !newsRepository.existsById(cursorId)) {
if (cursorId != null && !newsLookupService.existNewsById(cursorId)) {
throw new CommonException(ErrorCode.NOT_FOUND_CURSOR);
}

Expand All @@ -118,7 +103,7 @@ public CursorResponseDto<List<NewsHistInfoDto>> getNewsWithHist(Long userId, Lon
// 캐싱 히스토리 db 동기화
newsViewHistService.updateNewsHist(userId);

List<NewsViewHist> news;
List<NewsViewHist> hist;
Page<NewsViewHist> results;
if (cursorId == null) {
// 최초
Expand All @@ -127,14 +112,14 @@ public CursorResponseDto<List<NewsHistInfoDto>> getNewsWithHist(Long userId, Lon
// 그 이후
results = newsViewHistRepository.findAllByUserAndCorsor(userId, cursorId, pageRequest);
}
news = results.getContent();
hist = results.getContent();

List<NewsHistInfoDto> searchNewsDtos = getNewsHistInfo(news);
List<NewsHistInfoDto> searchNewsDtos = getNewsHistInfo(hist);

CursorInfoDto cursorInfoDto = CursorInfoDto.fromPageInfo(results);

return CursorResponseDto.fromEntityAndPageInfo(searchNewsDtos, cursorInfoDto);
} // 감정표현한 뉴스 조회
} // 최신순 시청기록 조회

@Transactional
public String deleteNewsHist(String newsHistIdList) {
Expand Down Expand Up @@ -177,4 +162,19 @@ private List<NewsHistInfoDto> getNewsHistInfo(List<NewsViewHist> newsViewHists)
}
return newsHistInfoDtos;
}

private CommentHistInfoDto getCommentHistInfo(Long userId, Comment comment) {
return CommentHistInfoDto.builder()
.news(searchNewsService.getNewsInfo(comment.getNews(), userId))
.comment(CommentDto.of(comment))
.build();
}

private List<CommentHistInfoDto> getCommentHistInfo(Long userId, List<Comment> comments) {
List<CommentHistInfoDto> newsHistInfoDtos = new ArrayList<>();
for (Comment comment : comments) {
newsHistInfoDtos.add(getCommentHistInfo(userId, comment));
}
return newsHistInfoDtos;
}
}
Loading

0 comments on commit 870cc63

Please sign in to comment.