diff --git a/src/main/java/com/TeamNull/LostArk/LostArk/controller/CommentController.java b/src/main/java/com/TeamNull/LostArk/LostArk/controller/CommentController.java index 06cd38d..ab15672 100644 --- a/src/main/java/com/TeamNull/LostArk/LostArk/controller/CommentController.java +++ b/src/main/java/com/TeamNull/LostArk/LostArk/controller/CommentController.java @@ -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.*; @@ -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 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 comments = commentRepository.findAll(pageRequest); - // commentRepository를 통해 추출된 데이터베이스의 자료를 만든 페이지에 적용 후 변수를 만듬 - - List 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 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 commentAdd(@PathVariable Integer abc, + @PageableDefault(size = 5, sort = "createdAt", direction = Sort.Direction.ASC) + Pageable pageable) { + return commentService.getComments(abc, pageable); } @PostMapping("/{userId}") diff --git a/src/main/java/com/TeamNull/LostArk/LostArk/service/CommentService.java b/src/main/java/com/TeamNull/LostArk/LostArk/service/CommentService.java index 582e101..e305567 100644 --- a/src/main/java/com/TeamNull/LostArk/LostArk/service/CommentService.java +++ b/src/main/java/com/TeamNull/LostArk/LostArk/service/CommentService.java @@ -1,6 +1,7 @@ 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; @@ -8,11 +9,20 @@ 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 @@ -22,9 +32,36 @@ public class CommentService { private final ResultRepository resultRepository; private final UserRepository userRepository; + public Map getComments(int page, Pageable pageable) { + Pageable pageRequest = PageRequest.of(page - 1, pageable.getPageSize(), pageable.getSort()); + Page comments = commentRepository.findAll(pageRequest); + + List 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 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