mirror of
https://github.com/headporter81/specialsource-homepage-backend.git
synced 2026-10-07 15:21:09 +09:00
This commit is contained in:
@@ -36,6 +36,21 @@ public class ScrapedPostController {
|
|||||||
return ResponseEntity.ok(posts);
|
return ResponseEntity.ok(posts);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/viewed")
|
||||||
|
public ResponseEntity<?> getViewedPosts(
|
||||||
|
@RequestParam(required = false) String board,
|
||||||
|
@RequestParam(defaultValue = "0") int page,
|
||||||
|
@RequestParam(defaultValue = "20") int size
|
||||||
|
) {
|
||||||
|
if (board != null && !isValidBoardKey(board)) {
|
||||||
|
return ResponseEntity.badRequest().body("존재하지 않는 게시판 키입니다: " + board);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pageable pageable = PageRequest.of(page, Math.min(size, 100));
|
||||||
|
Page<ScrapedPostResponse> posts = scrapedPostQueryService.getViewedPosts(board, pageable);
|
||||||
|
return ResponseEntity.ok(posts);
|
||||||
|
}
|
||||||
|
|
||||||
@GetMapping("/boards")
|
@GetMapping("/boards")
|
||||||
public ResponseEntity<List<BoardSummaryResponse>> getBoards() {
|
public ResponseEntity<List<BoardSummaryResponse>> getBoards() {
|
||||||
List<BoardSummaryResponse> boards = Arrays.stream(BoardSource.values())
|
List<BoardSummaryResponse> boards = Arrays.stream(BoardSource.values())
|
||||||
|
|||||||
@@ -15,6 +15,10 @@ public interface ScrapedPostRepository extends JpaRepository<ScrapedPost, Long>
|
|||||||
|
|
||||||
Page<ScrapedPost> findByViewedAtIsNullOrderByScrapedAtDesc(Pageable pageable);
|
Page<ScrapedPost> findByViewedAtIsNullOrderByScrapedAtDesc(Pageable pageable);
|
||||||
|
|
||||||
|
Page<ScrapedPost> findByBoardKeyAndViewedAtIsNotNullOrderByViewedAtDesc(String boardKey, Pageable pageable);
|
||||||
|
|
||||||
|
Page<ScrapedPost> findByViewedAtIsNotNullOrderByViewedAtDesc(Pageable pageable);
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
long deleteByScrapedAtBefore(LocalDateTime cutoff);
|
long deleteByScrapedAtBefore(LocalDateTime cutoff);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,15 @@ public class ScrapedPostQueryService {
|
|||||||
return page.map(ScrapedPostResponse::from);
|
return page.map(ScrapedPostResponse::from);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Page<ScrapedPostResponse> getViewedPosts(String boardKey, Pageable pageable) {
|
||||||
|
// 조회한(viewedAt != null) 글만 최근 읽은 순으로 보여준다.
|
||||||
|
Page<ScrapedPost> page = (boardKey == null || boardKey.isBlank())
|
||||||
|
? scrapedPostRepository.findByViewedAtIsNotNullOrderByViewedAtDesc(pageable)
|
||||||
|
: scrapedPostRepository.findByBoardKeyAndViewedAtIsNotNullOrderByViewedAtDesc(boardKey, pageable);
|
||||||
|
|
||||||
|
return page.map(ScrapedPostResponse::from);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public void markViewed(Long id) {
|
public void markViewed(Long id) {
|
||||||
ScrapedPost post = scrapedPostRepository.findById(id)
|
ScrapedPost post = scrapedPostRepository.findById(id)
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ public record ScrapedPostResponse(
|
|||||||
Integer likeCount,
|
Integer likeCount,
|
||||||
String content,
|
String content,
|
||||||
List<String> imageUrls,
|
List<String> imageUrls,
|
||||||
LocalDateTime scrapedAt
|
LocalDateTime scrapedAt,
|
||||||
|
LocalDateTime viewedAt
|
||||||
) {
|
) {
|
||||||
public static ScrapedPostResponse from(ScrapedPost post) {
|
public static ScrapedPostResponse from(ScrapedPost post) {
|
||||||
List<String> imageUrls = (post.getImageUrls() == null || post.getImageUrls().isBlank())
|
List<String> imageUrls = (post.getImageUrls() == null || post.getImageUrls().isBlank())
|
||||||
@@ -38,7 +39,8 @@ public record ScrapedPostResponse(
|
|||||||
post.getLikeCount(),
|
post.getLikeCount(),
|
||||||
post.getContent(),
|
post.getContent(),
|
||||||
imageUrls,
|
imageUrls,
|
||||||
post.getScrapedAt()
|
post.getScrapedAt(),
|
||||||
|
post.getViewedAt()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user