Skip to content

Commit

Permalink
Merge pull request #99 from Team-Recordy/feature/#98
Browse files Browse the repository at this point in the history
[fix] 북마크 페이지네이션 작동하도록 수정
  • Loading branch information
elive7 authored Jul 18, 2024
2 parents a13ef89 + 1ac30a4 commit ce72a1c
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.recordy.server.common.dto.response.CursorBasePaginatedResponse;
import org.recordy.server.auth.security.resolver.UserId;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.record.controller.dto.response.RecordInfoWithBookmark;
import org.springframework.data.domain.Slice;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

@Tag(name = "Bookmark Api")
public interface BookmarkApi {
Expand Down Expand Up @@ -53,4 +49,39 @@ public ResponseEntity<Boolean> bookmark(
@UserId Long userId,
@PathVariable Long recordId
);

@Operation(
summary = "사용자의 북마크 개수 조회 API",
description = "사용자가 북마크한 레코드의 개수를 조회합니다.",
responses = {
@ApiResponse(
responseCode = "200",
description = "요청이 성공적으로 처리되었습니다.",
content = @Content
),
@ApiResponse(
responseCode = "404",
description = "Not Found - 존재하지 않는 사용자 또는 레코드입니다.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
implementation = ErrorMessage.class
)
)
),
@ApiResponse(
responseCode = "500",
description = "Internal Server Error - 서버 내부 오류입니다.",
content = @Content(
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(
implementation = ErrorMessage.class
)
)
)
}
)
public ResponseEntity<Long> getBookmarkCount(
@UserId Long userId
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ public ResponseEntity<Boolean> bookmark(
.status(HttpStatus.OK)
.body(bookmarkService.bookmark(userId, recordId));
}

@Override
@GetMapping
public ResponseEntity<Long> getBookmarkCount(
@UserId Long userId
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(bookmarkService.countBookmarks(userId));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ public interface BookmarkRepository {
// query
Slice<Bookmark> findAllByBookmarksOrderByIdDesc(long userId, long cursor, Pageable pageable);
boolean existsByUserIdAndRecordId(Long userId, Long recordId);
long countByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface BookmarkJpaRepository extends JpaRepository<BookmarkEntity, Lon

// query
boolean existsByUserIdAndRecordId(Long userId, Long recordId);
long countAllByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ public Slice<BookmarkEntity> findAllByUserOrderByIdDesc(long userId, long cursor

return new SliceImpl<>(bookmarkEntities, pageable, QueryDslUtils.hasNext(pageable, bookmarkEntities));
}

public long countByUserId(long userId) {
return jpaQueryFactory
.select(bookmarkEntity.id.count())
.from(bookmarkEntity)
.join(bookmarkEntity.user, QUserEntity.userEntity)
.where(QUserEntity.userEntity.id.eq(userId))
.fetchOne();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public void delete(long userId, long recordId) {
bookmarkJpaRepository.deleteAllByUserIdAndRecordId(userId, recordId);
}

@Override
public void deleteByUserId(long userId) {
bookmarkJpaRepository.deleteAllByUserId(userId);
}

@Override
public Slice<Bookmark> findAllByBookmarksOrderByIdDesc(long userId, long cursor, Pageable pageable) {
return bookmarkQueryDslRepository.findAllByUserOrderByIdDesc(userId, cursor, pageable)
Expand All @@ -40,7 +45,7 @@ public boolean existsByUserIdAndRecordId(Long userId, Long recordId) {
}

@Override
public void deleteByUserId(long userId) {
bookmarkJpaRepository.deleteAllByUserId(userId);
public long countByUserId(Long userId) {
return bookmarkJpaRepository.countAllByUserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ public interface BookmarkService {

// query
List<Boolean> findBookmarks(long userId, List<Record> records);
Long countBookmarks(long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,10 @@ public List<Boolean> findBookmarks(long userId, List<Record> records) {
.map(record -> bookmarkRepository.existsByUserIdAndRecordId(userId, record.getId()))
.collect(Collectors.toList());
}

@Override
public Long countBookmarks(long userId) {
return bookmarkRepository.countByUserId(userId);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.recordy.server.auth.security.resolver.UserId;
import org.recordy.server.common.message.ErrorMessage;
import org.recordy.server.record.controller.dto.request.RecordCreateRequest;
import org.recordy.server.record.controller.dto.response.BookmarkedRecord;
import org.recordy.server.record.controller.dto.response.RecordInfoWithBookmark;
import org.springframework.data.domain.Slice;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -408,7 +409,7 @@ ResponseEntity<List<RecordInfoWithBookmark>> getTotalRecordInfosWithBookmarks(
)
}
)
public ResponseEntity<CursorBasePaginatedResponse<RecordInfoWithBookmark>> getBookmarkedRecords(
public ResponseEntity<CursorBasePaginatedResponse<BookmarkedRecord>> getBookmarkedRecords(
@UserId Long userId,
@RequestParam(required = false, defaultValue = "0L") long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.recordy.server.record.controller;

import lombok.RequiredArgsConstructor;
import org.recordy.server.bookmark.domain.Bookmark;
import org.recordy.server.bookmark.service.BookmarkService;
import org.recordy.server.common.dto.response.CursorBasePaginatedResponse;
import org.recordy.server.common.dto.response.PaginatedResponse;
import org.recordy.server.auth.security.resolver.UserId;
import org.recordy.server.record.controller.dto.request.RecordCreateRequest;
import org.recordy.server.record.controller.dto.response.BookmarkedRecord;
import org.recordy.server.record.controller.dto.response.RecordInfoWithBookmark;
import org.recordy.server.record.domain.File;
import org.recordy.server.record.domain.Record;
Expand Down Expand Up @@ -156,17 +158,15 @@ public ResponseEntity<List<RecordInfoWithBookmark>> getTotalRecordInfosWithBookm

@Override
@GetMapping("/bookmarks")
public ResponseEntity<CursorBasePaginatedResponse<RecordInfoWithBookmark>> getBookmarkedRecords(
public ResponseEntity<CursorBasePaginatedResponse<BookmarkedRecord>> getBookmarkedRecords(
@UserId Long userId,
@RequestParam(required = false, defaultValue = "0") long cursorId,
@RequestParam(required = false, defaultValue = "10") int size
) {
Slice<Record> records = recordService.getBookmarkedRecords(userId, cursorId, size);
List<Boolean> bookmarks = bookmarkService.findBookmarks(userId, records.getContent());
Slice<Bookmark> bookmarks = recordService.getBookmarkedRecords(userId, cursorId, size);

return ResponseEntity
.status(HttpStatus.OK)
.body(CursorBasePaginatedResponse.of(RecordInfoWithBookmark.of(records, bookmarks, userId), recordInfoWithBookmark -> recordInfoWithBookmark.recordInfo()
.id()));
.body(CursorBasePaginatedResponse.of(BookmarkedRecord.of(bookmarks, userId), bookmarkedRecord -> bookmarkedRecord.bookmarkId()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.recordy.server.record.controller.dto.response;

import java.util.List;
import java.util.stream.Collectors;
import org.recordy.server.bookmark.domain.Bookmark;
import org.recordy.server.record.domain.Record;
import org.springframework.data.domain.Slice;

public record BookmarkedRecord (
Long bookmarkId,
RecordInfo recordInfo,
Boolean isBookmark
){
public static Slice<BookmarkedRecord> of (Slice<Bookmark> bookmarks, Long currentUserId) {
return bookmarks.map(bookmark -> new BookmarkedRecord(bookmark.getId(), RecordInfo.from(bookmark.getRecord(), currentUserId), true));
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.recordy.server.record.service;

import org.recordy.server.bookmark.domain.Bookmark;
import org.recordy.server.record.domain.Record;
import org.recordy.server.record.domain.usecase.RecordCreate;
import org.springframework.data.domain.Slice;
Expand All @@ -18,6 +19,6 @@ public interface RecordService {
Slice<Record> getRecentRecords(String keywords, Long cursorId, int size);
Slice<Record> getRecentRecordsByUser(long userId, long cursorId, int size);
Slice<Record> getSubscribingRecords(long userId, long cursorId, int size);
Slice<Record> getBookmarkedRecords(long userId, long cursorId, int size);
Slice<Bookmark> getBookmarkedRecords(long userId, long cursorId, int size);
List<Record> getTotalRecords(int size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ private Slice<Record> getRecentRecordsWithKeywords(List<Keyword> keywords, long
return recordRepository.findAllByIdAfterAndKeywordsOrderByIdDesc(keywords, cursorId, PageRequest.ofSize(size));
}
@Override
public Slice<Record> getBookmarkedRecords(long userId, long cursorId, int size) {
return bookmarkRepository.findAllByBookmarksOrderByIdDesc(userId, cursorId, PageRequest.ofSize(size))
.map(Bookmark::getRecord);
public Slice<Bookmark> getBookmarkedRecords(long userId, long cursorId, int size) {
return bookmarkRepository.findAllByBookmarksOrderByIdDesc(userId, cursorId, PageRequest.ofSize(size));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ public boolean existsByUserIdAndRecordId(Long userId, Long recordId) {
bookmark.getUser().getId().equals(userId) && bookmark.getRecord().getId().equals(recordId)
);
}

@Override
public void deleteByUserId(long userId) {
bookmarks.values()
.removeIf(bookmark -> bookmark.getUser().getId() == userId);
}

@Override
public long countByUserId(Long userId) {
return bookmarks.values().stream()
.filter(bookmark -> bookmark.getUser().getId() == userId)
.count();
}
}

0 comments on commit ce72a1c

Please sign in to comment.