Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev backend #135

Merged
merged 10 commits into from
May 30, 2024
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()));
}
}
Loading