Skip to content

Commit

Permalink
Merge pull request #135 from tukcomCD2024/Dev-backend
Browse files Browse the repository at this point in the history
Dev backend
  • Loading branch information
yeonjy authored May 30, 2024
2 parents 93688fa + 30ec494 commit 63a880b
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.rollthedice.backend.domain.news.repository;
package com.rollthedice.backend.domain.debate.repository;

import com.rollthedice.backend.domain.debate.entity.DebateMessage;
import org.springframework.data.jpa.repository.JpaRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

import com.rollthedice.backend.domain.debate.dto.request.DebateMessageRequest;
import com.rollthedice.backend.domain.debate.dto.response.DebateMessageResponse;
import com.rollthedice.backend.domain.debate.dto.response.DebateSummaryResponse;
import com.rollthedice.backend.domain.debate.entity.DebateRoom;
import com.rollthedice.backend.domain.debate.exception.DebateRoomNotFoundException;
import com.rollthedice.backend.domain.debate.mapper.DebateMessageMapper;
import com.rollthedice.backend.domain.debate.repository.DebateRoomRepository;
import com.rollthedice.backend.domain.news.repository.DebateMessageRepository;
import com.rollthedice.backend.domain.debate.repository.DebateMessageRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rollthedice.backend.domain.news.dto.response.NewsDetailResponse;
import com.rollthedice.backend.domain.news.dto.response.NewsResponse;
import com.rollthedice.backend.domain.news.dto.response.ReadNewsResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
Expand Down Expand Up @@ -46,4 +47,17 @@ NewsDetailResponse getDetailNews(
Long newsId
);

@Operation(
summary = "최근 읽은 뉴스 조회",
description = "가장 최근에 읽은 3개의 뉴스를 조회합니다.",
security = {@SecurityRequirement(name = "access_token")},
tags = {"news"}
)
@ApiResponse(
responseCode = "200",
description = "요청에 성공하였습니다."
)
List<ReadNewsResponse> getReadNews();


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.rollthedice.backend.domain.news.api;

import com.rollthedice.backend.domain.crawling.NewsCrawlingService;
import com.rollthedice.backend.domain.news.dto.response.NewsDetailResponse;
import com.rollthedice.backend.domain.news.dto.response.NewsResponse;
import com.rollthedice.backend.domain.news.dto.response.ReadNewsResponse;
import com.rollthedice.backend.domain.news.service.NewsService;
import com.rollthedice.backend.domain.news.service.ReadNewsService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand All @@ -15,6 +18,8 @@
@RequestMapping("news")
public class NewsController implements NewsApi {
private final NewsService newsService;
private final ReadNewsService readNewsService;
private final NewsCrawlingService crawlingService;

@ResponseStatus(HttpStatus.OK)
@GetMapping("")
Expand All @@ -28,4 +33,10 @@ public List<NewsResponse> getNews(final Pageable pageable) {
public NewsDetailResponse getDetailNews(final @PathVariable Long newsId) {
return newsService.getDetailNews(newsId);
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("/viewed-history")
public List<ReadNewsResponse> getReadNews() {
return readNewsService.getReadNews();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.rollthedice.backend.domain.news.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReadNewsResponse {
private Long id;
private String title;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.rollthedice.backend.global.config.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

Expand All @@ -23,4 +24,10 @@ public class ReadNews extends BaseTimeEntity {
@JoinColumn(name = "news_id")
private News news;

@Builder
public ReadNews(Member member, News news) {
this.member = member;
this.news = news;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rollthedice.backend.domain.news.dto.response.NewsDetailResponse;
import com.rollthedice.backend.domain.news.dto.response.NewsResponse;
import com.rollthedice.backend.domain.news.dto.response.ReadNewsResponse;
import com.rollthedice.backend.domain.news.entity.News;
import org.mapstruct.Mapper;
import org.mapstruct.MappingConstants.ComponentModel;
Expand All @@ -11,5 +12,7 @@ public interface NewsMapper {

NewsResponse toResponse(final News news, boolean isBookmarked);

ReadNewsResponse toReadNewsResponse(final News news);

NewsDetailResponse toDetailResponse(final News news);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
@Repository
public interface NewsRepository extends JpaRepository<News, Long> {
List<News> findAllByContentIsNull();
List<News> findAllByOrderByViewsDescCreatedAtDesc(
final Pageable pageable);
List<News> findAllByOrderByViewsDescCreatedAtDesc(final Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rollthedice.backend.domain.news.repository;

import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.news.entity.ReadNews;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ReadNewsRepository extends JpaRepository<ReadNews, Long> {
List<ReadNews> getTop3ByMemberOrderByCreatedAtDesc(Member member);

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.rollthedice.backend.domain.bookmark.service.BookmarkService;
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.news.dto.response.NewsDetailResponse;
import com.rollthedice.backend.domain.news.dto.response.ReadNewsResponse;
import com.rollthedice.backend.domain.news.entity.ReadNews;
import com.rollthedice.backend.domain.news.exception.NewsNotFoundException;
import com.rollthedice.backend.domain.news.repository.ReadNewsRepository;
import com.rollthedice.backend.global.oauth2.service.AuthService;
import com.rollthedice.backend.domain.news.contentqueue.ContentProducer;
import com.rollthedice.backend.domain.news.dto.ContentMessageDto;
Expand All @@ -29,6 +32,7 @@ public class NewsService {
private final AuthService authService;
private final ContentProducer contentProducer;
private final NewsRepository newsRepository;
private final ReadNewsRepository readNewsRepository;
private final NewsMapper newsMapper;
private final BookmarkService bookmarkService;

Expand Down Expand Up @@ -69,6 +73,13 @@ public List<NewsResponse> getNews(final Pageable pageable) {

public NewsDetailResponse getDetailNews(Long newsId) {
final News news = newsRepository.findById(newsId).orElseThrow(NewsNotFoundException::new);
readNewsRepository.save(ReadNews.builder()
.member(authService.getMember()).news(news).build());
return newsMapper.toDetailResponse(news);
}

public List<ReadNewsResponse> getNewsByReadNews(final List<News> news) {
return news.stream().map(newsMapper::toReadNewsResponse).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.rollthedice.backend.domain.news.service;

import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.news.dto.response.ReadNewsResponse;
import com.rollthedice.backend.domain.news.entity.ReadNews;
import com.rollthedice.backend.domain.news.repository.ReadNewsRepository;
import com.rollthedice.backend.global.oauth2.service.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ReadNewsService {
private final AuthService authService;
private final ReadNewsRepository readNewsReository;
private final NewsService newsService;

public List<ReadNewsResponse> getReadNews() {
Member member = authService.getMember();
List<ReadNews> readNews = readNewsReository.getTop3ByMemberOrderByCreatedAtDesc(member);
return newsService.getNewsByReadNews(readNews.stream().map(r -> r.getNews()).collect(Collectors.toList()));
}
}

0 comments on commit 63a880b

Please sign in to comment.