Skip to content

Commit

Permalink
페이지네이션 controller와 service 분리 후 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
dkwooje committed Oct 2, 2024
1 parent 8405ef5 commit 9614dd3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package com.TeamNull.LostArk.LostArk.controller;

import com.TeamNull.LostArk.LostArk.dto.CommentDto;
import com.TeamNull.LostArk.LostArk.entity.Comment;
import com.TeamNull.LostArk.LostArk.entity.User;
import com.TeamNull.LostArk.LostArk.repository.CommentRepository;
import com.TeamNull.LostArk.LostArk.repository.UserRepository;
import com.TeamNull.LostArk.LostArk.service.CommentService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -26,53 +19,13 @@
public class CommentController {

private final CommentService commentService;
private final CommentRepository commentRepository;
private final UserRepository userRepository;

@GetMapping("/{abc}") //abc라는 정수를 URL에서 추출합니다.
public Map<String, Object> commentList(
@PathVariable Integer abc, //추출한 정수를 저장할 변수 만듬
@PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.DESC) //페이지의 규칙을 만듬
Pageable pageable //만든 규칙을 저장할 변수 생성
) {

Pageable pageRequest = PageRequest.of(abc - 1, pageable.getPageSize(), pageable.getSort()); //추출된 정수와 규칙으로 페이지 변수를 만듬
Page<Comment> comments = commentRepository.findAll(pageRequest);
// commentRepository를 통해 추출된 데이터베이스의 자료를 만든 페이지에 적용 후 변수를 만듬

List<CommentDto.CommentResponseDto> responseDtoList = comments.getContent().stream()//데이터베이스에서 가져온 자료 중에서 각 페이지에 포함된 댓글 목록을 추출
//더 정확히는 댓글 content가 아닌 새로만들 댓글 목록이다.
.map(comment -> { // DTO를 통해 원하는 자료(댓글 목록의 자료)를 가진 리스트를 만듬
CommentDto.CommentResponseDto dto = new CommentDto.CommentResponseDto(
comment.getId(),
comment.getCreatedAt(),
comment.getContent(),
comment.getUser().getId(),
comment.getTopFactorResult(),
comment.getNickName()
);

return dto;
})
.toList();


int totalPages = comments.getTotalPages(); //데이터베이스 페이지에 있는 페이지를 이용해 규칙성 정수를 만듬
int currentPage = comments.getNumber() + 1;
int startPage = Math.max(1, currentPage - 2);
int endPage = Math.min(startPage + 4, totalPages);

// JSON 응답 구성
Map<String, Object> response = new HashMap<>(); //맵을 사용해 규칙성 정수와 원하는 자료를 가진 리스트를 추력함
response.put("comments", responseDtoList);
response.put("currentPage", currentPage);
response.put("startPage", startPage);
response.put("endPage", endPage);
response.put("totalPages", totalPages);

return response;


@GetMapping("/{abc}")
public Map<String, Object> commentAdd(@PathVariable Integer abc,
@PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.ASC)
Pageable pageable) {
return commentService.getComments(abc, pageable);
}

@PostMapping("/{userId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
package com.TeamNull.LostArk.LostArk.service;


import com.TeamNull.LostArk.LostArk.dto.CommentDto;
import com.TeamNull.LostArk.LostArk.entity.Comment;
import com.TeamNull.LostArk.LostArk.entity.User;
import com.TeamNull.LostArk.LostArk.repository.CommentRepository;
import com.TeamNull.LostArk.LostArk.repository.ResultRepository;
import com.TeamNull.LostArk.LostArk.repository.UserRepository;
import lombok.RequiredArgsConstructor;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;



import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand All @@ -22,9 +32,36 @@ public class CommentService {
private final ResultRepository resultRepository;
private final UserRepository userRepository;

public Map<String, Object> getComments(int page, Pageable pageable) {
Pageable pageRequest = PageRequest.of(page - 1, pageable.getPageSize(), pageable.getSort());
Page<Comment> comments = commentRepository.findAll(pageRequest);

List<CommentDto.CommentResponseDto> responseDtoList = comments.getContent().stream()
.map(comment -> new CommentDto.CommentResponseDto(
comment.getId(),
comment.getCreatedAt(),
comment.getPassword(),
comment.getUser().getId(),
comment.getTopFactorResult(),
comment.getNickName()
)).toList();

int totalPage = comments.getTotalPages();
int currentPage = comments.getNumber() + 1;
int startPage = Math.max(1, currentPage - 2);
int endPage = Math.min(totalPage, startPage + 4);

Map<String, Object> response = new HashMap<>();
response.put("comments", responseDtoList);
response.put("TOTALPAGE", totalPage);
response.put("CURRENTPAGE", currentPage);
response.put("STARTPAGE", startPage);
response.put("ENDPAGE", endPage);

return response;
}


public void commentAdd( String content,
public void creation( String content,
String password,
String nickname,
UUID userId
Expand Down

0 comments on commit 9614dd3

Please sign in to comment.