Skip to content

Commit

Permalink
[feat] #23 Page객체 List 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
최경환 authored and 최경환 committed Jan 22, 2025
1 parent 2f3c6c0 commit f75b4e3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ application-dev.yaml
application-local.yaml
application-jwt.yaml
application-oauth.yaml
Application-bucket.yaml
application-bucket.yaml

### macOS ###
.DS_Store
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.wedit.weditapp.domain.comments.controller;

import com.wedit.weditapp.domain.comments.dto.response.CommentResponseDTO;
import com.wedit.weditapp.domain.comments.dto.response.PagedCommentResponseDTO;
import com.wedit.weditapp.domain.comments.service.CommentService;
import com.wedit.weditapp.global.response.GlobalResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -28,11 +30,11 @@ public class CommentController {
@ApiResponse(responseCode = "500", description = "서버 에러")
})
@GetMapping("/{invitationId}")
public ResponseEntity<GlobalResponseDto<Page<CommentResponseDTO>>> findAllComments(
public ResponseEntity<GlobalResponseDto<PagedCommentResponseDTO>> findAllComments(
@PathVariable Long invitationId,
@RequestParam(defaultValue = "0") int page){
@RequestParam(defaultValue = "1") int page){

Page<CommentResponseDTO> comments = commentService.findAllCommentsByInvitationId(invitationId, page);
return ResponseEntity.ok(GlobalResponseDto.success(comments));
PagedCommentResponseDTO response = commentService.findAllCommentsByInvitationId(invitationId, page);
return ResponseEntity.status(HttpStatus.OK).body(GlobalResponseDto.success(response));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public static CommentResponseDTO from (Comments comment){
.build();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.wedit.weditapp.domain.comments.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class PagedCommentResponseDTO {

private List<CommentResponseDTO> comments;
private Boolean isLast;
private Integer currentPage;

@Builder
private PagedCommentResponseDTO(List<CommentResponseDTO> comments, Boolean isLast, Integer currentPage){
this.comments = comments;
this.isLast = isLast;
this.currentPage = currentPage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.wedit.weditapp.domain.comments.domain.Comments;
import com.wedit.weditapp.domain.comments.domain.repository.CommentRepository;
import com.wedit.weditapp.domain.comments.dto.response.CommentResponseDTO;
import com.wedit.weditapp.domain.comments.dto.response.PagedCommentResponseDTO;
import com.wedit.weditapp.global.error.ErrorCode;
import com.wedit.weditapp.global.error.exception.CommonException;
import lombok.RequiredArgsConstructor;
Expand All @@ -25,16 +26,30 @@ public class CommentService {

private final CommentRepository commentRepository;

public Page<CommentResponseDTO> findAllCommentsByInvitationId(Long invitationId, int page){
public PagedCommentResponseDTO findAllCommentsByInvitationId(Long invitationId, int page){

// boolean existsInvitation = invitationRepository.existById(invitationId);
// if(!existsInvitation){
// throw new CommonException(ErrorCode.INVITATION_NOT_FOUND);
// }

Pageable pageable = PageRequest.of(page, PAGE_SIZE, Sort.by("createdAt").descending());
if(page < 1){
throw new CommonException(ErrorCode.INVALID_PAGE_NUMBER);
}

Pageable pageable = PageRequest.of(page - 1, PAGE_SIZE, Sort.by("createdAt").descending());
Page<Comments> comments = commentRepository.findByInvitationId(invitationId, pageable);

return comments.map(CommentResponseDTO::from);
// Comments 엔티티 -> DTO 리스트로 변환
List<CommentResponseDTO> commentList = comments.getContent().stream()
.map(CommentResponseDTO::from)
.collect(Collectors.toList());

// 필요한 정보만으로 DTO 생성
return PagedCommentResponseDTO.builder()
.comments(commentList)
.isLast(comments.isLast())
.currentPage(page)
.build();
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/wedit/weditapp/global/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ public enum ErrorCode {
DUPLICATE_NICKNAME(409, "NICKNAME_ALREADY_EXISTS", "이미 존재하는 닉네임입니다."),

//작업
DELETE_FORBIDDEN(403, "DELETE_FORBIDDEN", "삭제 권한이 없습니다.");
DELETE_FORBIDDEN(403, "DELETE_FORBIDDEN", "삭제 권한이 없습니다."),

// 방명록
INVALID_PAGE_NUMBER(400, "INVALID_PAGE_NUMBER", "페이지 번호는 1 이상이어야 합니다.");

private final int status;
private final String code;
Expand Down

0 comments on commit f75b4e3

Please sign in to comment.