diff --git a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java index 912b0a22..21800f97 100644 --- a/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java +++ b/src/main/java/com/bbteam/budgetbuddies/common/BaseEntity.java @@ -22,6 +22,7 @@ @Getter @SuperBuilder @SoftDelete // boolean 타입의 deleted 필드가 추가 +@Getter public abstract class BaseEntity { @Id diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java index adc42780..36500a8b 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java @@ -1,89 +1,83 @@ package com.bbteam.budgetbuddies.domain.comment.controller; + import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.comment.service.CommentService; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.Parameters; import io.swagger.v3.oas.annotations.responses.ApiResponses; -import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequiredArgsConstructor -public class CommentController { - - private final CommentService commentService; - - // user, discountInfo 인증 어노테이션 추후 추가 예정 +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +public interface CommentController { @Operation(summary = "[User] 특정 할인 정보 게시글에 댓글달기", description = "특정 할인 정보 게시글에 댓글을 다는 API입니다.") @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), }) @Parameters({ - @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다."), - @Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다."), - @Parameter(name = "content", description = "댓글 내용입니다."), + @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"), + @Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다. requestBody"), + @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), }) - @PostMapping("/discounts/comments/{userId}/add") - public ResponseEntity saveDiscountInfoComment( - @RequestParam("userId") Long userId, - @RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){ - CommentResponseDto.DiscountInfoSuccessDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto); - return ResponseEntity.ok(dto); - } + ResponseEntity saveDiscountInfoComment( + Long userId, + CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto); + @Operation(summary = "[User] 특정 할인 정보 게시글의 댓글 조회하기", description = "특정 할인 정보 게시글의 댓글을 가져오는 API입니다.") @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), }) @Parameters({ - @Parameter(name = "discountInfoId", description = "댓글을 가져올 할인 정보 게시글 id입니다."), + @Parameter(name = "discountInfoId", description = "댓글을 가져올 할인 정보 게시글 id입니다. parameter"), + @Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"), + @Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter") }) - @GetMapping("/discounts/comments/get/{discountInfoId}") - public ResponseEntity> findAllByDiscountInfo( - @RequestParam("discountInfoId") Long discountInfoId){ - List result = commentService.findByDiscountInfo(discountInfoId); - return ResponseEntity.ok(result); - } + ResponseEntity> findAllByDiscountInfo( + Long discountInfoId, + Pageable pageable); @Operation(summary = "[User] 특정 지원 정보 게시글에 댓글달기", description = "특정 지원 정보 게시글에 댓글을 다는 API입니다.") @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), }) @Parameters({ - @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다."), - @Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다."), - @Parameter(name = "content", description = "댓글 내용입니다."), + @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"), + @Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다. requestBody"), + @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), }) - @PostMapping("/supports/comments/{userId}/add") - public ResponseEntity saveSupportInfoComment( - @RequestParam("userId") Long userId, - @RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){ - CommentResponseDto.SupportInfoSuccessDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto); - return ResponseEntity.ok(dto); - } + ResponseEntity saveSupportInfoComment( + Long userId, + CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto); @Operation(summary = "[User] 특정 지원 정보 게시글의 댓글 조회하기", description = "특정 지원 정보 게시글의 댓글을 가져오는 API입니다.") @ApiResponses({ @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), }) @Parameters({ - @Parameter(name = "supportInfoId", description = "댓글을 가져올 지원 정보 게시글 id입니다."), - }) - @GetMapping("/supports/comments/get/{supportInfoId}") - public ResponseEntity> findAllBySupportInfo( - @RequestParam("supportInfoId") Long supportInfoId){ - List result = commentService.findBySupportInfo(supportInfoId); - return ResponseEntity.ok(result); - } - + @Parameter(name = "supportInfoId", description = "댓글을 가져올 지원 정보 게시글 id입니다. parameter"), + @Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"), + @Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter") + }) + ResponseEntity> findAllBySupportInfo( + Long supportInfoId, + Pageable pageable); + @Operation(summary = "[User] 특정 댓글 삭제하기", description = "특정 댓글을 삭제하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "commentId", description = "삭제할 댓글 id 입니다. parameter") + }) + @GetMapping("/comments/delete") + ResponseEntity deleteComment(Long commentId); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerImpl.java new file mode 100644 index 00000000..8f9691d7 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerImpl.java @@ -0,0 +1,60 @@ +package com.bbteam.budgetbuddies.domain.comment.controller; + +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.service.CommentService; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +public class CommentControllerImpl implements CommentController{ + + private final CommentService commentService; + + @PostMapping("/discounts/comments") + public ResponseEntity saveDiscountInfoComment( + @RequestParam("userId") Long userId, + @RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){ + CommentResponseDto.DiscountInfoSuccessDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto); + return ResponseEntity.ok(dto); + } + + + @GetMapping("/discounts/comments") + public ResponseEntity> findAllByDiscountInfo( + @RequestParam("discountInfoId") Long discountInfoId, + @PageableDefault(size = 20, page = 0) Pageable pageable){ + Page result = commentService.findByDiscountInfoWithPaging(discountInfoId, pageable); + return ResponseEntity.ok(result); + } + + + @PostMapping("/supports/comments") + public ResponseEntity saveSupportInfoComment( + @RequestParam("userId") Long userId, + @RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){ + CommentResponseDto.SupportInfoSuccessDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto); + return ResponseEntity.ok(dto); + } + + + @GetMapping("/supports/comments") + public ResponseEntity> findAllBySupportInfo( + @RequestParam("supportInfoId") Long supportInfoId, + @PageableDefault(size = 20, page = 0)Pageable pageable){ + Page result = commentService.findBySupportInfoWithPaging(supportInfoId, pageable); + return ResponseEntity.ok(result); + } + + + public ResponseEntity deleteComment(@RequestParam("commentId") Long commentId) { + commentService.deleteComment(commentId); + return ResponseEntity.ok("ok"); + } + +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/converter/CommentConverter.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/converter/CommentConverter.java index a589bacf..626a1e6f 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/converter/CommentConverter.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/converter/CommentConverter.java @@ -7,50 +7,53 @@ import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; import com.bbteam.budgetbuddies.domain.user.entity.User; -import java.util.HashMap; - public class CommentConverter { - public static Comment toDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto, User user, DiscountInfo discountInfo) { + public static Comment toDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto, User user, DiscountInfo discountInfo, + Integer anonymousNumber) { return Comment.builder() .user(user) .discountInfo(discountInfo) .content(dto.getContent()) + .anonymousNumber(anonymousNumber) .build(); } - public static Comment toSupportComment(CommentRequestDto.SupportInfoCommentDto dto, User user, SupportInfo supportInfo) { + public static Comment toSupportComment(CommentRequestDto.SupportInfoCommentDto dto, User user, SupportInfo supportInfo, + Integer anonymousNumber) { return Comment.builder() .user(user) .supportInfo(supportInfo) .content(dto.getContent()) + .anonymousNumber(anonymousNumber) .build(); } - public static CommentResponseDto.DiscountInfoCommentDto toDiscountInfoCommentDto(Comment comment, - HashMap anonymousMapping){ + public static CommentResponseDto.DiscountInfoCommentDto toDiscountInfoCommentDto(Comment comment){ return CommentResponseDto.DiscountInfoCommentDto.builder() + .commentId(comment.getId()) .discountInfoId(comment.getDiscountInfo().getId()) .userId(comment.getUser().getId()) .content(comment.getContent()) - .anonymousNumber(anonymousMapping.get(comment.getUser().getId())) + .anonymousNumber(comment.getAnonymousNumber()) .build(); } - public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(Comment comment, - HashMap anonymousMapping){ + public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(Comment comment){ return CommentResponseDto.SupportInfoCommentDto.builder() + .commentId(comment.getId()) .supportInfoId(comment.getSupportInfo().getId()) .userId(comment.getUser().getId()) .content(comment.getContent()) - .anonymousNumber(anonymousMapping.get(comment.getUser().getId())) + .anonymousNumber(comment.getAnonymousNumber()) .build(); } public static CommentResponseDto.DiscountInfoSuccessDto toDiscountInfoSuccessDto(Comment comment){ return CommentResponseDto.DiscountInfoSuccessDto.builder() + .commentId(comment.getId()) .discountInfoId(comment.getDiscountInfo().getId()) .userId(comment.getUser().getId()) .content(comment.getContent()) @@ -59,6 +62,7 @@ public static CommentResponseDto.DiscountInfoSuccessDto toDiscountInfoSuccessDto public static CommentResponseDto.SupportInfoSuccessDto toSupportInfoSuccessDto(Comment comment){ return CommentResponseDto.SupportInfoSuccessDto.builder() + .commentId(comment.getId()) .supportInfoId(comment.getSupportInfo().getId()) .userId(comment.getUser().getId()) .content(comment.getContent()) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentResponseDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentResponseDto.java index 8cd110ba..bfc27405 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentResponseDto.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentResponseDto.java @@ -9,24 +9,27 @@ public class CommentResponseDto { @Getter @Builder public static class DiscountInfoCommentDto{ + private Long commentId; private Long userId; private Long discountInfoId; private String content; - private Long anonymousNumber; + private Integer anonymousNumber; } @Getter @Builder public static class SupportInfoCommentDto{ + private Long commentId; private Long userId; private Long supportInfoId; private String content; - private Long anonymousNumber; + private Integer anonymousNumber; } @Getter @Builder public static class DiscountInfoSuccessDto{ + private Long commentId; private Long userId; private Long discountInfoId; private String content; @@ -35,6 +38,7 @@ public static class DiscountInfoSuccessDto{ @Getter @Builder public static class SupportInfoSuccessDto{ + private Long commentId; private Long userId; private Long supportInfoId; private String content; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/entity/Comment.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/entity/Comment.java index 9ce02ba7..49dbf6dd 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/entity/Comment.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/entity/Comment.java @@ -11,6 +11,7 @@ import lombok.NoArgsConstructor; import lombok.experimental.SuperBuilder; import org.hibernate.annotations.Where; +import org.springframework.lang.Nullable; @Entity @Getter @@ -34,4 +35,7 @@ public class Comment extends BaseEntity { @JoinColumn(name = "support_info_id") private SupportInfo supportInfo; + @Column(nullable = false) + private Integer anonymousNumber; + } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepository.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepository.java index e197d82a..b734eca7 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepository.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepository.java @@ -3,18 +3,36 @@ import com.bbteam.budgetbuddies.domain.comment.entity.Comment; import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; +import com.bbteam.budgetbuddies.domain.user.entity.User; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; +import java.util.Optional; public interface CommentRepository extends JpaRepository { @Query("select c from Comment c where c.discountInfo.id = :discountInfoId" + - " order by c.createdAt asc") + " order by c.createdAt asc") // 익명번호 부여용 List findByDiscountInfo(@Param("discountInfoId")Long discountInfoId); + @Query("select c from Comment c where c.discountInfo.id = :discountInfoId" + + " order by c.createdAt asc") + Page findByDiscountInfoWithPaging(@Param("discountInfoId")Long discountInfoId, + Pageable pageable); + @Query("select c from Comment c where c.supportInfo.id = :supportInfoId" + " order by c.createdAt asc") List findBySupportInfo(@Param("supportInfoId")Long supportInfoId); + + @Query("select c from Comment c where c.supportInfo.id = :supportInfoId" + + " order by c.createdAt asc") + Page findBySupportInfoWithPaging(@Param("supportInfoId")Long supportInfoId, + Pageable pageable); + + Optional findTopByUserAndDiscountInfo(User user, DiscountInfo discountInfo); + Optional findTopByUserAndSupportInfo(User user, SupportInfo supportInfo); + } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentService.java index fdbb743f..ac49bc5a 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentService.java @@ -2,6 +2,8 @@ import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import java.util.List; @@ -26,6 +28,12 @@ public interface CommentService { */ List findBySupportInfo(Long supportInfoId); + Page findByDiscountInfoWithPaging(Long discountInfoId, Pageable pageable); + Page findBySupportInfoWithPaging(Long supportInfoId, Pageable pageable); + + void deleteComment(Long commentId); + + diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java index 4b394081..df8063fa 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java @@ -13,12 +13,15 @@ import com.bbteam.budgetbuddies.domain.user.entity.User; import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.HashMap; import java.util.List; import java.util.NoSuchElementException; +import java.util.Optional; import java.util.stream.Collectors; // 임시로 유저는 service에서 찾아서 처리하는 로직으로 작성함 @@ -37,13 +40,23 @@ public class CommentServiceImpl implements CommentService{ public CommentResponseDto.SupportInfoSuccessDto saveSupportComment(Long userId, CommentRequestDto.SupportInfoCommentDto dto) { User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); SupportInfo supportInfo = supportInfoRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException()); - - Comment comment = CommentConverter.toSupportComment(dto, user, supportInfo); + int anonymousNumber = getSupportAnonymousNumber(user, supportInfo); + Comment comment = CommentConverter.toSupportComment(dto, user, supportInfo, anonymousNumber); Comment savedComment = commentRepository.save(comment); return CommentConverter.toSupportInfoSuccessDto(savedComment); } + private int getSupportAnonymousNumber(User user, SupportInfo supportInfo) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndSupportInfo(user, supportInfo); + if(foundComment.isEmpty()){ + anonymousNumber = supportInfo.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } @Override @@ -51,20 +64,31 @@ public CommentResponseDto.SupportInfoSuccessDto saveSupportComment(Long userId, public CommentResponseDto.DiscountInfoSuccessDto saveDiscountComment(Long userId, CommentRequestDto.DiscountInfoCommentDto dto) { User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); DiscountInfo discountInfo = discountInfoRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException()); - - Comment comment = CommentConverter.toDiscountComment(dto, user, discountInfo); + int anonymousNumber = getDiscountAnonymousNumber(user, discountInfo); + Comment comment = CommentConverter.toDiscountComment(dto, user, discountInfo, anonymousNumber); Comment savedComment = commentRepository.save(comment); return CommentConverter.toDiscountInfoSuccessDto(savedComment); } + private int getDiscountAnonymousNumber(User user, DiscountInfo discountInfo) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndDiscountInfo(user, discountInfo); + if(foundComment.isEmpty()){ + anonymousNumber = discountInfo.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } + @Override public List findByDiscountInfo(Long discountInfoId) { List commentList = commentRepository.findByDiscountInfo(discountInfoId); HashMap anonymousMapping = countAnonymousNumber(commentList); List collect = commentList.stream() - .map(comment -> CommentConverter.toDiscountInfoCommentDto(comment, anonymousMapping)) + .map(CommentConverter::toDiscountInfoCommentDto) .collect(Collectors.toList()); return collect; @@ -75,7 +99,7 @@ public List findBySupportInfo(Long sup List commentList = commentRepository.findBySupportInfo(supportInfoId); HashMap anonymousMapping = countAnonymousNumber(commentList); List collect = commentList.stream() - .map(comment -> CommentConverter.toSupportInfoCommentDto(comment, anonymousMapping)) + .map(CommentConverter::toSupportInfoCommentDto) .collect(Collectors.toList()); return collect; } @@ -93,19 +117,24 @@ private static HashMap countAnonymousNumber(List commentLis return anonymousMapping; } - @Transactional - public void removeDiscountInfoComment(Long discountInfoId){ - DiscountInfo discountInfo = discountInfoRepository.findById(discountInfoId).orElseThrow(() -> new NoSuchElementException("No such Entity")); - discountInfoRepository.delete(discountInfo); - return; + @Override + public Page findByDiscountInfoWithPaging(Long discountInfoId, Pageable pageable) { + Page commentPage = commentRepository.findByDiscountInfoWithPaging(discountInfoId, pageable); + Page result = commentPage.map(CommentConverter::toDiscountInfoCommentDto); + return result; } - @Transactional - public void removeSupportInfoComment(Long supportInfoId){ - SupportInfo supportInfo = supportInfoRepository.findById(supportInfoId).orElseThrow(() -> new NoSuchElementException("No such Entity")); - supportInfoRepository.delete(supportInfo); - return; + @Override + public Page findBySupportInfoWithPaging(Long supportInfoId, Pageable pageable) { + Page commentPage = commentRepository.findBySupportInfoWithPaging(supportInfoId, pageable); + Page result = commentPage.map(CommentConverter::toSupportInfoCommentDto); + return result; } - + @Override + @Transactional + public void deleteComment(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); + commentRepository.delete(comment); + } } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/discountinfo/entity/DiscountInfo.java b/src/main/java/com/bbteam/budgetbuddies/domain/discountinfo/entity/DiscountInfo.java index 5caf45b3..072b2ac4 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/discountinfo/entity/DiscountInfo.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/discountinfo/entity/DiscountInfo.java @@ -42,4 +42,9 @@ public void subLikeCount() { this.likeCount--; } + public Integer addAndGetAnonymousNumber() { + this.anonymousNumber++; + return anonymousNumber; + } + } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/supportinfo/entity/SupportInfo.java b/src/main/java/com/bbteam/budgetbuddies/domain/supportinfo/entity/SupportInfo.java index aaa651e0..cc83973e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/supportinfo/entity/SupportInfo.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/supportinfo/entity/SupportInfo.java @@ -43,4 +43,9 @@ public void subLikeCount() { this.likeCount--; } + public Integer addAndGetAnonymousNumber() { + this.anonymousNumber++; + return anonymousNumber; + } + } diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepositoryTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepositoryTest.java index 67186f65..4aa430a7 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepositoryTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/comment/repository/CommentRepositoryTest.java @@ -7,7 +7,6 @@ import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; import jakarta.persistence.EntityManager; import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -15,8 +14,6 @@ import java.util.List; -import static org.junit.jupiter.api.Assertions.*; - @SpringBootTest @Transactional diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java index c3ade564..47636c82 100644 --- a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java +++ b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java @@ -13,11 +13,13 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.transaction.annotation.Transactional; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; /** * comment service는 다음과 같은 기능을 제공해야한다. @@ -31,6 +33,10 @@ * 4. 특정 게시글 id로 댓글 찾는 기능 */ + +/* + 테스트마다 테스트케이스가 다를 수 있어서 공통로직으로 처리하지 않아 매우 깁니다... + */ @SpringBootTest @Transactional class CommentServiceTest { @@ -56,7 +62,9 @@ public void saveDiscountInfoCommentTest(){ .build(); userRepository.save(user1); - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인").build(); + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); discountInfoRepository.save(sale1); CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() @@ -94,9 +102,13 @@ public void saveDiscountInfoCommentTest2(){ .build(); userRepository.save(user2); - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인").build(); + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); discountInfoRepository.save(sale1); - DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인").build(); + DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인") + .anonymousNumber(0) + .build(); discountInfoRepository.save(sale2); @@ -163,9 +175,9 @@ void DiscountAnonymousCommentTest(){ userRepository.save(user2); userRepository.save(user3); - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인").build(); + DiscountInfo sale1 = DiscountInfo.builder().anonymousNumber(0).title("무신사 할인").build(); discountInfoRepository.save(sale1); - DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인").build(); + DiscountInfo sale2 = DiscountInfo.builder().anonymousNumber(0).title("핫트랙스 할인").build(); discountInfoRepository.save(sale2); @@ -197,10 +209,10 @@ void DiscountAnonymousCommentTest(){ em.flush(); List result = commentService.findByDiscountInfo(sale1.getId()); - Long test1 = result.get(0).getAnonymousNumber(); - Long test2 = result.get(1).getAnonymousNumber(); - Long test3 = result.get(2).getAnonymousNumber(); - Long test4 = result.get(3).getAnonymousNumber(); + Integer test1 = result.get(0).getAnonymousNumber(); + Integer test2 = result.get(1).getAnonymousNumber(); + Integer test3 = result.get(2).getAnonymousNumber(); + Integer test4 = result.get(3).getAnonymousNumber(); Assertions.assertThat(test1).isEqualTo(1); Assertions.assertThat(test2).isEqualTo(2); @@ -228,9 +240,9 @@ public void saveSupportInfoCommentTest2(){ .build(); userRepository.save(user2); - SupportInfo info1 = SupportInfo.builder().title("국가장학금 신청").build(); + SupportInfo info1 = SupportInfo.builder().anonymousNumber(0).title("국가장학금 신청").build(); supportInfoRepository.save(info1); - SupportInfo info2 = SupportInfo.builder().title("봉사활동").build(); + SupportInfo info2 = SupportInfo.builder().anonymousNumber(0).title("봉사활동").build(); supportInfoRepository.save(info2); @@ -303,9 +315,9 @@ void supportAnonymousCommentTest(){ userRepository.save(user3); userRepository.save(user4); - SupportInfo info1 = SupportInfo.builder().title("국가장학금 신청").build(); + SupportInfo info1 = SupportInfo.builder().anonymousNumber(0).title("국가장학금 신청").build(); supportInfoRepository.save(info1); - SupportInfo info2 = SupportInfo.builder().title("봉사활동").build(); + SupportInfo info2 = SupportInfo.builder().anonymousNumber(0).title("봉사활동").build(); supportInfoRepository.save(info2); @@ -347,11 +359,11 @@ void supportAnonymousCommentTest(){ List returnDto = commentService.findBySupportInfo(info1.getId()); List returnDto2 = commentService.findBySupportInfo(info2.getId()); - Long test1 = returnDto.get(0).getAnonymousNumber(); - Long test2 = returnDto.get(1).getAnonymousNumber(); - Long test3 = returnDto.get(2).getAnonymousNumber(); - Long test4 = returnDto.get(3).getAnonymousNumber(); - Long test5 = returnDto.get(4).getAnonymousNumber(); + Integer test1 = returnDto.get(0).getAnonymousNumber(); + Integer test2 = returnDto.get(1).getAnonymousNumber(); + Integer test3 = returnDto.get(2).getAnonymousNumber(); + Integer test4 = returnDto.get(3).getAnonymousNumber(); + Integer test5 = returnDto.get(4).getAnonymousNumber(); Assertions.assertThat(test1).isEqualTo(1); Assertions.assertThat(test2).isEqualTo(2); @@ -360,6 +372,192 @@ void supportAnonymousCommentTest(){ Assertions.assertThat(test5).isEqualTo(1); } + @Test + void DiscountInfoCommentPagingTest() { + User user1 = User.builder() + .name("tester1") + .email("1234") + .age(5) + .phoneNumber("123456") + .build(); + userRepository.save(user1); + + User user2 = User.builder() + .name("tester2") + .email("12345") + .age(7) + .phoneNumber("1234567") + .build(); + + User user3 = User.builder() + .name("tester3") + .email("1234553") + .age(9) + .phoneNumber("1232134567") + .build(); + userRepository.save(user2); + userRepository.save(user3); + + DiscountInfo sale1 = DiscountInfo.builder().anonymousNumber(0).title("무신사 할인").build(); + discountInfoRepository.save(sale1); + DiscountInfo sale2 = DiscountInfo.builder().anonymousNumber(0).title("핫트랙스 할인").build(); + discountInfoRepository.save(sale2); + + + CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("굿") + .build(); + + CommentRequestDto.DiscountInfoCommentDto dto2 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("좋아요") + .build(); + CommentRequestDto.DiscountInfoCommentDto dto3 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale2.getId()) + .content("유용해요!") + .build(); + CommentRequestDto.DiscountInfoCommentDto dto4 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("구웃!") + .build(); + + commentService.saveDiscountComment(user1.getId(), dto1); + commentService.saveDiscountComment(user2.getId(), dto2); + commentService.saveDiscountComment(user1.getId(), dto3); + + commentService.saveDiscountComment(user1.getId(), dto4); + commentService.saveDiscountComment(user3.getId(), dto4); + commentService.saveDiscountComment(user2.getId(), dto4); + //sale1 = 5 + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + + Page result1 = commentService.findByDiscountInfoWithPaging(sale1.getId(), pageRequest1); + Assertions.assertThat(result1.getTotalElements()).isEqualTo(5); + Assertions.assertThat(result1.getTotalPages()).isEqualTo(3); + Assertions.assertThat(result1.hasNext()).isTrue(); + Assertions.assertThat(result1.hasPrevious()).isFalse(); + List list1 = result1.getContent(); + Assertions.assertThat(list1.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(list1.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(list1.get(0).getAnonymousNumber()).isEqualTo(1); + + PageRequest pageRequest2 = PageRequest.of(1, 3); + + Page result2 = commentService.findByDiscountInfoWithPaging(sale1.getId(), pageRequest2); + Assertions.assertThat(result2.getTotalElements()).isEqualTo(5); + Assertions.assertThat(result2.getTotalPages()).isEqualTo(2); + Assertions.assertThat(result2.hasNext()).isFalse(); + Assertions.assertThat(result2.hasPrevious()).isTrue(); + List list2 = result2.getContent(); + Assertions.assertThat(list2.get(0).getUserId()).isEqualTo(user3.getId()); + Assertions.assertThat(list2.get(0).getContent()).isEqualTo("구웃!"); + Assertions.assertThat(list2.get(0).getAnonymousNumber()).isEqualTo(3); + + + } + + @Test + void SupportInfoPagingTest() { + User user1 = User.builder() + .name("tester1") + .email("1234") + .age(5) + .phoneNumber("123456") + .build(); + userRepository.save(user1); + + User user2 = User.builder() + .name("tester2") + .email("12345") + .age(7) + .phoneNumber("1234567") + .build(); + User user3 = User.builder() + .name("tester432") + .email("123423445") + .age(7) + .phoneNumber("1423234567") + .build(); + User user4 = User.builder() + .name("test43er2") + .email("1232445") + .age(7) + .phoneNumber("123454267") + .build(); + userRepository.save(user2); + userRepository.save(user3); + userRepository.save(user4); + + SupportInfo info1 = SupportInfo.builder().anonymousNumber(0).title("국가장학금 신청").build(); + supportInfoRepository.save(info1); + SupportInfo info2 = SupportInfo.builder().anonymousNumber(0).title("봉사활동").build(); + supportInfoRepository.save(info2); + + + CommentRequestDto.SupportInfoCommentDto dto1 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info1.getId()) + .content("굿") + .build(); + + CommentRequestDto.SupportInfoCommentDto dto2 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info1.getId()) + .content("좋아요") + .build(); + CommentRequestDto.SupportInfoCommentDto dto3 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info2.getId()) + .content("유용해요!") + .build(); + CommentRequestDto.SupportInfoCommentDto dto6 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info1.getId()) + .content("굿") + .build(); + CommentRequestDto.SupportInfoCommentDto dto4 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info1.getId()) + .content("굿") + .build(); + CommentRequestDto.SupportInfoCommentDto dto5 = CommentRequestDto.SupportInfoCommentDto.builder() + .supportInfoId(info1.getId()) + .content("굿") + .build(); + + commentService.saveSupportComment(user1.getId(), dto1); + commentService.saveSupportComment(user2.getId(), dto2); + commentService.saveSupportComment(user1.getId(), dto3); // 얘만 info2 + commentService.saveSupportComment(user3.getId(), dto4); + commentService.saveSupportComment(user4.getId(), dto5); + commentService.saveSupportComment(user1.getId(), dto6); + commentService.saveSupportComment(user2.getId(), dto5); + commentService.saveSupportComment(user3.getId(), dto5); + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + Page result1 = commentService.findBySupportInfoWithPaging(info1.getId(), pageRequest1); + + Assertions.assertThat(result1.getTotalElements()).isEqualTo(7); + Assertions.assertThat(result1.getTotalPages()).isEqualTo(4); + Assertions.assertThat(result1.hasNext()).isTrue(); + Assertions.assertThat(result1.hasPrevious()).isFalse(); + List list1 = result1.getContent(); + Assertions.assertThat(list1.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(list1.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(list1.get(0).getAnonymousNumber()).isEqualTo(1); + + PageRequest pageRequest2 = PageRequest.of(1, 5); + Page result2 = commentService.findBySupportInfoWithPaging(info1.getId(), pageRequest2); + + Assertions.assertThat(result2.getTotalElements()).isEqualTo(7); + Assertions.assertThat(result2.getTotalPages()).isEqualTo(2); + Assertions.assertThat(result2.hasNext()).isFalse(); + Assertions.assertThat(result2.hasPrevious()).isTrue(); + List list2 = result2.getContent(); + Assertions.assertThat(list2.get(0).getUserId()).isEqualTo(user2.getId()); + Assertions.assertThat(list2.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(list2.get(0).getAnonymousNumber()).isEqualTo(2); + } + } \ No newline at end of file