Skip to content

Commit

Permalink
[DEV-46] 공지사항 전체 조회 API offset 기반 페이지네이션 적용 (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
wonjunYou authored Aug 29, 2024
1 parent e1b7b3f commit 7c757e7
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ddingdong.ddingdongBE.domain.notice.api;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

import ddingdong.ddingdongBE.domain.notice.controller.dto.request.GetAllNoticeByPageRequest;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.GetAllNoticeByPageResponse;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.NoticeResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Tag(name = "Notice", description = "Notice API")
@RequestMapping(value = "/server/notices", produces = APPLICATION_JSON_VALUE)
public interface NoticeApi {

@Operation(summary = "공지사항 전체 조회 API")
@GetMapping
GetAllNoticeByPageResponse getAllNotices(
GetAllNoticeByPageRequest request
);

@Operation(summary = "공지사항 상세 조회 API")
@GetMapping("/{noticeId}")
NoticeResponse getNoticeDetail(@PathVariable Long noticeId);
}
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package ddingdong.ddingdongBE.domain.notice.controller;

import ddingdong.ddingdongBE.domain.notice.controller.dto.response.DetailNoticeResponse;
import ddingdong.ddingdongBE.domain.notice.api.NoticeApi;
import ddingdong.ddingdongBE.domain.notice.controller.dto.request.GetAllNoticeByPageRequest;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.GetAllNoticeByPageResponse;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.NoticeResponse;
import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import ddingdong.ddingdongBE.domain.notice.service.NoticeService;

import java.util.List;
import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/server/notices")
@RequiredArgsConstructor
public class NoticeApiController {
public class NoticeApiController implements NoticeApi {

private final NoticeService noticeService;

@GetMapping
public List<NoticeResponse> getAllNotices() {
return noticeService.getAllNotices();
public GetAllNoticeByPageResponse getAllNotices(GetAllNoticeByPageRequest request) {
List<Notice> notices = noticeService.getAllNotices(
request.getPage(),
request.getLimit()
);

return GetAllNoticeByPageResponse.from(notices);
}

@GetMapping("/{noticeId}")
public DetailNoticeResponse getNotice(@PathVariable Long noticeId) {
public NoticeResponse getNoticeDetail(Long noticeId) {
return noticeService.getNotice(noticeId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.request;

import io.swagger.v3.oas.annotations.Parameter;
import lombok.Getter;
import org.springdoc.api.annotations.ParameterObject;

@Getter
@ParameterObject
public class GetAllNoticeByPageRequest {

@Parameter(description = "현재 페이지 수")
private int page;

@Parameter(description = "조회할 페이지 크기", example = "10")
private Integer limit;

public GetAllNoticeByPageRequest(int page, Integer limit) {
this.page = page;
this.limit = (limit == null) ? 10 : limit;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.response;

import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import java.time.LocalDateTime;
import java.util.List;
import lombok.Builder;

@Builder
public record GetAllNoticeByPageResponse(
List<NoticeResponseDto> notices
) {

public static GetAllNoticeByPageResponse from(List<Notice> notices) {
List<NoticeResponseDto> noticeResponses = notices.stream()
.map(NoticeResponseDto::from)
.toList();

return GetAllNoticeByPageResponse
.builder()
.notices(noticeResponses)
.build();
}

@Builder
public record NoticeResponseDto(
Long id,
String title,
LocalDateTime createdAt
) {

public static NoticeResponseDto from(Notice notice) {
return NoticeResponseDto
.builder()
.id(notice.getId())
.title(notice.getTitle())
.createdAt(notice.getCreatedAt())
.build();
}

}

}

Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package ddingdong.ddingdongBE.domain.notice.controller.dto.response;

import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import ddingdong.ddingdongBE.file.dto.FileResponse;
import java.time.LocalDateTime;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,20 +12,30 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class NoticeResponse {

private Long id;

private String title;

private String content;

private LocalDateTime createdAt;

private NoticeResponse(Long id, String title, LocalDateTime createdAt) {
this.id = id;
private List<String> imageUrls;

private List<FileResponse> fileUrls;

public NoticeResponse(String title, String content, LocalDateTime createdAt,
List<String> imageUrls,
List<FileResponse> fileUrls) {
this.title = title;
this.content = content;
this.createdAt = createdAt;
this.imageUrls = imageUrls;
this.fileUrls = fileUrls;
}

public static NoticeResponse from(Notice notice) {
return new NoticeResponse(notice.getId(), notice.getTitle(), notice.getCreatedAt());
public static NoticeResponse of(Notice notice, List<String> imageUrls,
List<FileResponse> fileUrls) {
return new NoticeResponse(notice.getTitle(), notice.getContent(), notice.getCreatedAt(),
imageUrls, fileUrls);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
package ddingdong.ddingdongBE.domain.notice.repository;

import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface NoticeRepository extends JpaRepository<Notice, Long> {

@Query(
value = """
SELECT *
FROM notice AS n
ORDER BY n.id DESC
LIMIT :limit
OFFSET :offsetValue
""",
nativeQuery = true
)
List<Notice> findAllByPage(int limit, int offsetValue);

}
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package ddingdong.ddingdongBE.domain.notice.service;

import static ddingdong.ddingdongBE.common.exception.ErrorMessage.*;
import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileTypeCategory.*;
import static ddingdong.ddingdongBE.common.exception.ErrorMessage.NO_SUCH_NOTICE;
import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileDomainCategory.NOTICE;
import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileTypeCategory.FILE;
import static ddingdong.ddingdongBE.domain.fileinformation.entity.FileTypeCategory.IMAGE;

import ddingdong.ddingdongBE.domain.fileinformation.entity.FileInformation;
import ddingdong.ddingdongBE.domain.fileinformation.repository.FileInformationRepository;
import ddingdong.ddingdongBE.domain.fileinformation.service.FileInformationService;
import ddingdong.ddingdongBE.domain.notice.controller.dto.request.RegisterNoticeRequest;
import ddingdong.ddingdongBE.domain.notice.controller.dto.request.UpdateNoticeRequest;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.DetailNoticeResponse;
import ddingdong.ddingdongBE.domain.notice.controller.dto.response.NoticeResponse;
import ddingdong.ddingdongBE.domain.notice.entity.Notice;
import ddingdong.ddingdongBE.domain.notice.repository.NoticeRepository;
import ddingdong.ddingdongBE.domain.user.entity.User;
import ddingdong.ddingdongBE.file.FileStore;
import ddingdong.ddingdongBE.file.dto.FileResponse;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -44,21 +40,20 @@ public Long register(User user, RegisterNoticeRequest request) {
}

@Transactional(readOnly = true)
public List<NoticeResponse> getAllNotices() {
return noticeRepository.findAll().stream()
.map(NoticeResponse::from)
.collect(Collectors.toList());
public List<Notice> getAllNotices(int page, int limit) {
int offset = (page - 1) * limit;
return noticeRepository.findAllByPage(limit, offset);
}

@Transactional(readOnly = true)
public DetailNoticeResponse getNotice(Long noticeId) {
public NoticeResponse getNotice(Long noticeId) {
Notice notice = noticeRepository.findById(noticeId)
.orElseThrow(() -> new NoSuchElementException(NO_SUCH_NOTICE.getText()));

List<String> imageUrls = fileInformationService.getImageUrls(IMAGE.getFileType() + NOTICE.getFileDomain() + noticeId);
List<FileResponse> fileUrls = fileInformationService.getFileUrls(FILE.getFileType() + NOTICE.getFileDomain() + noticeId);

return DetailNoticeResponse.of(notice, imageUrls, fileUrls);
return NoticeResponse.of(notice, imageUrls, fileUrls);
}

public void update(Long noticeId, UpdateNoticeRequest request) {
Expand Down

0 comments on commit 7c757e7

Please sign in to comment.