Skip to content

Commit

Permalink
feat: bookmark announcements fetch api
Browse files Browse the repository at this point in the history
  • Loading branch information
jcw1031 committed May 5, 2024
1 parent 9353c4a commit 7ca1b82
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public PageResponseDto<List<AnnouncementListResponse>> searchAnnouncement(
@RequestParam("campus") Campus campus,
@RequestParam(name = "page", defaultValue = "1") int page,
@RequestParam(name = "keyword", required = false, defaultValue = "") String keyword
// @RequestHeader(name = "Device-Id", required = false) String deviceId
) {
PageDto pageDto = PageDto.of(page);
List<AnnouncementListResponse> announcements =
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/ac/knu/likeknu/controller/BookmarkController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package ac.knu.likeknu.controller;

import ac.knu.likeknu.controller.dto.announcement.AnnouncementListResponse;
import ac.knu.likeknu.controller.dto.base.ResponseDto;
import ac.knu.likeknu.controller.dto.device.request.BookmarkRequest;
import ac.knu.likeknu.service.BookmarkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/api/bookmarks")
@RestController
public class BookmarkController {
Expand All @@ -36,4 +42,12 @@ public ResponseDto<String> removeBookmark(
bookmarkService.removeAnnouncementBookmark(deviceId, announcementId);
return ResponseDto.of("The bookmark has been removed successfully.");
}

@GetMapping("/{deviceId}")
public ResponseDto<List<AnnouncementListResponse>> bookmarkAnnouncements(
@PathVariable("deviceId") String deviceId
) {
List<AnnouncementListResponse> bookmarkAnnouncements = bookmarkService.getBookmarkAnnouncements(deviceId);
return ResponseDto.of(bookmarkAnnouncements);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,37 @@
public record AnnouncementListResponse(String announcementId, String announcementTitle, String announcementDate,
String announcementUrl, String announcementTag, boolean isBookmarked) {

public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy.MM.dd");

public static AnnouncementListResponse of(Announcement announcement) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
return AnnouncementListResponse.builder()
.announcementId(announcement.getId())
.announcementTitle(announcement.getAnnouncementTitle())
.announcementDate(announcement.getAnnouncementDate().format(dateTimeFormatter))
.announcementDate(announcement.getAnnouncementDate().format(DATE_TIME_FORMATTER))
.announcementUrl(announcement.getAnnouncementUrl())
.announcementTag(announcement.getTag().getTagName())
.build();
}

public static AnnouncementListResponse of(Announcement announcement, Set<Announcement> bookmarks) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
return AnnouncementListResponse.builder()
.announcementId(announcement.getId())
.announcementTitle(announcement.getAnnouncementTitle())
.announcementDate(announcement.getAnnouncementDate().format(dateTimeFormatter))
.announcementDate(announcement.getAnnouncementDate().format(DATE_TIME_FORMATTER))
.announcementUrl(announcement.getAnnouncementUrl())
.announcementTag(announcement.getTag().getTagName())
.isBookmarked(bookmarks.contains(announcement))
.build();
}

public static AnnouncementListResponse bookmarks(Announcement announcement) {
return AnnouncementListResponse.builder()
.announcementId(announcement.getId())
.announcementTitle(announcement.getAnnouncementTitle())
.announcementDate(announcement.getAnnouncementDate().format(DATE_TIME_FORMATTER))
.announcementUrl(announcement.getAnnouncementUrl())
.announcementTag(announcement.getTag().getTagName())
.isBookmarked(true)
.build();
}
}
16 changes: 16 additions & 0 deletions src/main/java/ac/knu/likeknu/service/BookmarkService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ac.knu.likeknu.service;

import ac.knu.likeknu.controller.dto.announcement.AnnouncementListResponse;
import ac.knu.likeknu.domain.Announcement;
import ac.knu.likeknu.domain.Device;
import ac.knu.likeknu.exception.BusinessException;
Expand All @@ -8,12 +9,16 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Set;

@Transactional
@Service
public class BookmarkService {

private static final int DEFAULT_PAGE_SIZE = 15;

private final DeviceRepository deviceRepository;
private final AnnouncementRepository announcementRepository;

Expand Down Expand Up @@ -44,4 +49,15 @@ public void removeAnnouncementBookmark(String deviceId, String announcementId) {
Set<Announcement> bookmarks = device.getBookmarks();
bookmarks.remove(announcement);
}

public List<AnnouncementListResponse> getBookmarkAnnouncements(String deviceId) {
Device device = deviceRepository.findById(deviceId)
.orElseThrow(() -> new BusinessException(String.format("Device not found! [%s]", deviceId)));

return device.getBookmarks()
.stream()
.sorted(Comparator.comparing(Announcement::getAnnouncementDate).reversed())
.map(AnnouncementListResponse::bookmarks)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void fetchStudentNewsApiSuccess() throws Exception {
Announcement announcement3 = TestInstanceFactory.createAnnouncement("Test C", "https://testc.com", Tag.LIBRARY);

// when
when(announcementService.getAnnouncements(eq(Campus.CHEONAN), eq(Category.STUDENT_NEWS), any(PageDto.class), any(), nativeDeviceId))
when(announcementService.getAnnouncements(eq(Campus.CHEONAN), eq(Category.STUDENT_NEWS), any(PageDto.class), any(), any()))
.thenReturn(List.of(
AnnouncementListResponse.of(announcement1),
AnnouncementListResponse.of(announcement2),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import ac.knu.likeknu.controller.dto.announcement.AnnouncementListResponse;
import ac.knu.likeknu.controller.dto.base.PageDto;
import ac.knu.likeknu.domain.Announcement;
import ac.knu.likeknu.domain.Device;
import ac.knu.likeknu.domain.constants.Campus;
import ac.knu.likeknu.domain.constants.Category;
import ac.knu.likeknu.domain.constants.Tag;
import ac.knu.likeknu.repository.AnnouncementRepository;
import ac.knu.likeknu.repository.DeviceRepository;
import ac.knu.likeknu.utils.TestInstanceFactory;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -19,7 +21,9 @@
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -39,6 +43,8 @@ class AnnouncementServiceTest {

@Mock
private AnnouncementRepository announcementRepository;
@Mock
private DeviceRepository deviceRepository;

@DisplayName("ํ•™์ƒ์†Œ์‹ ์กฐํšŒ์— ์„ฑ๊ณตํ•œ๋‹ค.")
@Test
Expand All @@ -53,9 +59,11 @@ void getStudentNewsSuccess() throws Exception {
when(announcementRepository.findByCampusInAndCategory(
eq(Set.of(Campus.ALL, Campus.CHEONAN)), eq(Category.STUDENT_NEWS), any(Pageable.class)
)).thenReturn(new PageImpl<>(List.of(announcement1, announcement2, announcement3), PageRequest.of(0, 10), 1));
when(deviceRepository.findById(any(String.class)))
.thenReturn(Optional.of(new Device("deviceA", null, Campus.SINGWAN, LocalDateTime.now(), null, null)));

List<AnnouncementListResponse> announcementList =
announcementService.getAnnouncements(Campus.CHEONAN, Category.STUDENT_NEWS, pageDto, "", nativeDeviceId);
announcementService.getAnnouncements(Campus.CHEONAN, Category.STUDENT_NEWS, pageDto, "", "");

// then
AnnouncementListResponse announcementResponse = announcementList.get(0);
Expand Down

0 comments on commit 7ca1b82

Please sign in to comment.