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

✨ feat: 북마크된 공고 목록 저장 많은 순 조회 기능 개발 #123

Merged
merged 4 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions http/test.http
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,15 @@ Authorization: Bearer {{masterToken}}
Content-Type: application/json

{
"announcementId": 2
"announcementId": 1
}

### 북마크 최근 저장순 조회
GET http://localhost:8080/api/v1/me/announcements/bookmarked/sort?sort=RECENT
GET http://localhost:8080/api/v1/me/announcements/bookmarked?sort=RECENT
Authorization: Bearer {{masterToken}}

### 북마크 저장 많은순 조회
GET http://localhost:8080/api/v1/me/announcements/bookmarked?sort=SAVED
Authorization: Bearer {{masterToken}}

### 보고서 작성
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,17 @@ public ApiResponse<BookmarkToggleResponse> bookmarkToggle(
return ApiResponse.onSuccess(bookmarkService.bookmarkToggle(authOrganization, request));
}

@GetMapping("/bookmarked/sort")
public ApiResponse<List<BookmarkGetResponse>> getBookmark(
@GetMapping("/bookmarked")
public ApiResponse<List<BookmarkGetResponse>> getRecentBookmark(
@AuthOrganization Organization authOrganization,
@RequestParam("sort") BookmarkStatus sortStatus
@RequestParam(name = "sort") BookmarkStatus sortStatus
) {
if (sortStatus == BookmarkStatus.RECENT) {
return ApiResponse.onSuccess(bookmarkService.getRecentBookmark(authOrganization));
}
if (sortStatus == BookmarkStatus.SAVED) {
return ApiResponse.onSuccess(bookmarkService.getSavedBookmark(authOrganization));
}
return ApiResponse.onSuccess(Collections.emptyList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public record BookmarkGetResponse(
Long id,
Long announcementId,
String announcementTitle,
LocalDateTime createdAt
LocalDateTime createdAt,
Long savedCount
) {

public static BookmarkGetResponse from(Bookmark bookmark) {
Expand All @@ -21,6 +22,7 @@ public static BookmarkGetResponse from(Bookmark bookmark) {
.announcementId(bookmark.getAnnouncement().getId())
.announcementTitle(bookmark.getAnnouncement().getTitle())
.createdAt(bookmark.getCreatedAt())
.savedCount(bookmark.getSavedCount())
.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,20 @@ public class Bookmark extends BaseEntity {
@Column(name = "bookmark_id")
private Long id;

@Column(name = "saved_count", nullable = false)
private long savedCount;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "organization_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Organization organization;

@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "announcement_id", foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
private Announcement announcement;

public void increaseSavedCount() { this.savedCount++; }
public void decreaseSavedCount() {
if (this.savedCount > 0) {
this.savedCount--;
} }
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ public interface BookmarkRepository extends JpaRepository<Bookmark, Long> {

Optional<Bookmark> findByOrganizationAndAnnouncement(Organization organization, Announcement announcement);
List<BookmarkGetResponse> findByOrganizationOrderByCreatedAtDesc(Organization organization);
List<BookmarkGetResponse> findByOrganizationOrderBySavedCountDesc(Organization organization);

}

Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,20 @@ public BookmarkToggleResponse bookmarkToggle(Organization organization, Bookmark

if (existingBookmark != null) {
bookmarkRepository.delete(existingBookmark);
existingBookmark.decreaseSavedCount();
return BookmarkToggleResponse.from(existingBookmark, false); // 이미 북마크가 되어있는 경우 취소
} else {
final Bookmark bookmark = bookmarkRepository.save(request.toEntity(organization, announcement));
bookmark.increaseSavedCount();
return BookmarkToggleResponse.from(bookmark, true); // 북마크가 안되어있는 경우 등록
}
}

public List<BookmarkGetResponse> getRecentBookmark(Organization organization) {
return bookmarkRepository.findByOrganizationOrderByCreatedAtDesc(organization);
}

public List<BookmarkGetResponse> getSavedBookmark(Organization organization) {
return bookmarkRepository.findByOrganizationOrderBySavedCountDesc(organization);
}
}
Loading