From 1f6e3073a1f8951351abfd85cc8a9b7421a78582 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 15:58:34 +0900 Subject: [PATCH 01/18] =?UTF-8?q?[refactor]=20Controller=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EB=B0=8F=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=EC=B2=B4=20=EC=9D=B4=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 109 +++++++----------- .../controller/CommentControllerApi.java | 80 +++++++++++++ 2 files changed, 123 insertions(+), 66 deletions(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java 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 36500a8b..6fd4a094 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,83 +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 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 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.GetMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequiredArgsConstructor +public class CommentController implements CommentControllerApi { + + 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); + } + -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"), - @Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다. requestBody"), - @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), - }) - ResponseEntity saveDiscountInfoComment( - Long userId, - CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto); + @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); + } - @Operation(summary = "[User] 특정 할인 정보 게시글의 댓글 조회하기", description = "특정 할인 정보 게시글의 댓글을 가져오는 API입니다.") - @ApiResponses({ - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), - }) - @Parameters({ - @Parameter(name = "discountInfoId", description = "댓글을 가져올 할인 정보 게시글 id입니다. parameter"), - @Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"), - @Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter") - }) - ResponseEntity> findAllByDiscountInfo( - Long discountInfoId, - Pageable pageable); + @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); + } - @Operation(summary = "[User] 특정 지원 정보 게시글에 댓글달기", description = "특정 지원 정보 게시글에 댓글을 다는 API입니다.") - @ApiResponses({ - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), - }) - @Parameters({ - @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"), - @Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다. requestBody"), - @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), - }) - 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입니다. parameter"), - @Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"), - @Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter") + @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); + } - }) - ResponseEntity> findAllBySupportInfo( - Long supportInfoId, - Pageable pageable); + public ResponseEntity deleteComment(@RequestParam("commentId") Long commentId) { + commentService.deleteComment(commentId); + return ResponseEntity.ok("ok"); + } - @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/CommentControllerApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java new file mode 100644 index 00000000..363412ad --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java @@ -0,0 +1,80 @@ +package com.bbteam.budgetbuddies.domain.comment.controller; + + +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +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 org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; + +public interface CommentControllerApi { + @Operation(summary = "[User] 특정 할인 정보 게시글에 댓글달기", description = "특정 할인 정보 게시글에 댓글을 다는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "userId", description = "현재 댓글을 다는 유저 id입니다. parameter"), + @Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다. requestBody"), + @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), + }) + 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"), + @Parameter(name = "page", description = "페이징을 위한 페이지 번호입니다. 0부터 시작합니다. parameter"), + @Parameter(name = "size", description = "페이징을 위한 페이지 사이즈입니다. default는 20입니다. parameter") + }) + 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"), + @Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다. requestBody"), + @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), + }) + 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입니다. 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); +} From 9277ed49d0d5cd38b355520ba5f528ed8d095d02 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 20:38:32 +0900 Subject: [PATCH 02/18] =?UTF-8?q?[refactor]=20Comment=20=EB=B0=98=ED=99=98?= =?UTF-8?q?=EC=8B=9C=20=EC=9E=91=EC=84=B1=20=EC=8B=9C=EA=B0=84=EB=8F=84=20?= =?UTF-8?q?=EA=B0=99=EC=9D=B4=20=EB=B0=98=ED=99=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../budgetbuddies/domain/comment/dto/CommentResponseDto.java | 5 +++++ 1 file changed, 5 insertions(+) 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 bfc27405..1e7bcb9e 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 @@ -4,6 +4,9 @@ import lombok.Builder; import lombok.Getter; +import java.time.LocalDate; +import java.time.LocalDateTime; + public class CommentResponseDto { @Getter @@ -14,6 +17,7 @@ public static class DiscountInfoCommentDto{ private Long discountInfoId; private String content; private Integer anonymousNumber; + private LocalDateTime createdAt; } @Getter @@ -24,6 +28,7 @@ public static class SupportInfoCommentDto{ private Long supportInfoId; private String content; private Integer anonymousNumber; + private LocalDateTime createdAt; } @Getter From 25ac8ee94e3079adf836c2895758b38383d27e5c Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 20:38:54 +0900 Subject: [PATCH 03/18] =?UTF-8?q?[feat]=20CommentService=20=EB=8C=93?= =?UTF-8?q?=EA=B8=80=20=EC=88=98=EC=A0=95=20=EB=A1=9C=EC=A7=81=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentService.java | 6 ++- .../comment/service/CommentServiceImpl.java | 48 +++++++++++++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) 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 ac49bc5a..19aaa266 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 @@ -33,8 +33,12 @@ public interface CommentService { void deleteComment(Long commentId); + CommentResponseDto.DiscountInfoCommentDto findDiscountCommentOne(Long commentId); + CommentResponseDto.SupportInfoCommentDto findSupportCommentOne(Long commentId); - + //dirty checking 사용해서 변경 + CommentResponseDto.DiscountInfoCommentDto modifyDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto); + CommentResponseDto.SupportInfoCommentDto modifySupportComment(CommentRequestDto.SupportInfoCommentDto dto); } 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 df8063fa..97ee1ea3 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 @@ -18,10 +18,7 @@ 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.*; import java.util.stream.Collectors; // 임시로 유저는 service에서 찾아서 처리하는 로직으로 작성함 @@ -137,4 +134,47 @@ public void deleteComment(Long commentId) { Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); commentRepository.delete(comment); } + + @Override + public CommentResponseDto.DiscountInfoCommentDto findDiscountCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getDiscountInfo() == null){ + throw new RuntimeException("discountInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toDiscountInfoCommentDto(comment); + + } + + @Override + public CommentResponseDto.SupportInfoCommentDto findSupportCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getSupportInfo() == null){ + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toSupportInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.DiscountInfoCommentDto modifyDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto) { + Comment comment = commentRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if(comment.getDiscountInfo() == null) { + throw new RuntimeException("discountInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toDiscountInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.SupportInfoCommentDto modifySupportComment(CommentRequestDto.SupportInfoCommentDto dto) { + Comment comment = commentRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if (comment.getSupportInfo() == null) { + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toSupportInfoCommentDto(comment); + } } From 1e95bf89190d362d568b5443a244b3d39db95bb4 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 20:39:36 +0900 Subject: [PATCH 04/18] =?UTF-8?q?[refactor]=20CommentConverter=20ResponseD?= =?UTF-8?q?to=EC=97=90=20createadAt=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/converter/CommentConverter.java | 2 ++ 1 file changed, 2 insertions(+) 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 626a1e6f..6a5a9f8d 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 @@ -36,6 +36,7 @@ public static CommentResponseDto.DiscountInfoCommentDto toDiscountInfoCommentDto .userId(comment.getUser().getId()) .content(comment.getContent()) .anonymousNumber(comment.getAnonymousNumber()) + .createdAt(comment.getCreatedAt()) .build(); } @@ -47,6 +48,7 @@ public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(C .userId(comment.getUser().getId()) .content(comment.getContent()) .anonymousNumber(comment.getAnonymousNumber()) + .createdAt(comment.getCreatedAt()) .build(); } From 537fe49c323acf6d5f7bb78a36643425490b036a Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 20:39:57 +0900 Subject: [PATCH 05/18] =?UTF-8?q?[feat]=20Comment=20content=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EC=9C=84=ED=95=9C=20=EB=A7=A4=EC=84=9C=EB=93=9C=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bbteam/budgetbuddies/domain/comment/entity/Comment.java | 5 +++++ 1 file changed, 5 insertions(+) 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 ce3ecccc..81827240 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 @@ -41,4 +41,9 @@ public class Comment extends BaseEntity { @Column(nullable = false) private Integer anonymousNumber; + public void modifyComment(String newComment) { + this.content = newComment; + return; + } + } From 29d431de3b5fc2562c17ed639ddd0668839a1f6f Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 22:12:06 +0900 Subject: [PATCH 06/18] =?UTF-8?q?[test]=20Generic=20Interface=EB=A5=BC=20?= =?UTF-8?q?=ED=86=B5=ED=95=9C=20Service=20=ED=86=B5=ED=95=A9=20=EC=8B=9C?= =?UTF-8?q?=EB=8F=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/DiscountCommentService.java | 98 +++ .../service/SupportCommentService.java | 100 +++ .../comment/service/CommentServiceTestV2.java | 569 ++++++++++++++++++ 3 files changed, 767 insertions(+) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java create mode 100644 src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java new file mode 100644 index 00000000..d1a16e0f --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java @@ -0,0 +1,98 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; +import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; +import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; +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.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service("discountCommentService") +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class DiscountCommentService implements CommentService{ + private final CommentRepository commentRepository; + private final UserRepository userRepository; + private final DiscountInfoRepository discountInfoRepository; + + @Override + @Transactional + public CommentResponseDto.DiscountInfoCommentDto saveComment(Long userId, CommentRequestDto.DiscountInfoCommentDto dto) { + User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); + DiscountInfo info = discountInfoRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 + int anonymousNumber = getAnonymousNumber(user, info); + Comment comment = CommentConverter.toDiscountComment(dto, user, info, anonymousNumber); + Comment savedComment = commentRepository.save(comment); + + return CommentConverter.toDiscountInfoCommentDto(savedComment); + } + + private int getAnonymousNumber(User user, DiscountInfo info) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndDiscountInfo(user, info); + if (foundComment.isEmpty()) { + anonymousNumber = info.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } + + @Override + public List findByInfo(Long infoId) { + List commentList = commentRepository.findByDiscountInfo(infoId); + List collect = commentList.stream() + .map(CommentConverter::toDiscountInfoCommentDto) + .collect(Collectors.toList()); + return collect; + } + + @Override + public Page findByInfoWithPaging(Long infoId, Pageable pageable) { + Page commentPage = commentRepository.findByDiscountInfoWithPaging(infoId, pageable); + Page result = commentPage.map(CommentConverter::toDiscountInfoCommentDto); + return result; + } + + @Override + @Transactional + public void deleteComment(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); + commentRepository.delete(comment); + } + + @Override + public CommentResponseDto.DiscountInfoCommentDto findCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getDiscountInfo() == null){ + throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toDiscountInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.DiscountInfoCommentDto modifyComment(CommentRequestDto.DiscountInfoCommentDto dto) { + Comment comment = commentRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if (comment.getDiscountInfo() == null) { + throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toDiscountInfoCommentDto(comment); + } +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java new file mode 100644 index 00000000..d6c4ec1a --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java @@ -0,0 +1,100 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; +import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; +import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; +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("supportCommentService") +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class SupportCommentService implements CommentService { + + private final CommentRepository commentRepository; + private final UserRepository userRepository; + private final SupportInfoRepository supportInfoRepository; + + @Override + @Transactional + public CommentResponseDto.SupportInfoCommentDto saveComment(Long userId, CommentRequestDto.SupportInfoCommentDto dto) { + User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); + SupportInfo info = supportInfoRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 + int anonymousNumber = getAnonymousNumber(user, info); + Comment comment = CommentConverter.toSupportComment(dto, user, info, anonymousNumber); + Comment savedComment = commentRepository.save(comment); + + return CommentConverter.toSupportInfoCommentDto(savedComment); + } + + private int getAnonymousNumber(User user, SupportInfo info) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndSupportInfo(user, info); + if (foundComment.isEmpty()) { + anonymousNumber = info.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } + + @Override + public List findByInfo(Long infoId) { + List commentList = commentRepository.findBySupportInfo(infoId); + List collect = commentList.stream() + .map(CommentConverter::toSupportInfoCommentDto) + .collect(Collectors.toList()); + return collect; + } + + @Override + public Page findByInfoWithPaging(Long infoId, Pageable pageable) { + Page commentPage = commentRepository.findBySupportInfoWithPaging(infoId, 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); + } + + @Override + public CommentResponseDto.SupportInfoCommentDto findCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getSupportInfo() == null){ + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toSupportInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.SupportInfoCommentDto modifyComment(CommentRequestDto.SupportInfoCommentDto dto) { + Comment comment = commentRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if (comment.getSupportInfo() == null) { + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toSupportInfoCommentDto(comment); + } +} diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java new file mode 100644 index 00000000..aafa0422 --- /dev/null +++ b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java @@ -0,0 +1,569 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; +import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; +import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; +import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; +import com.bbteam.budgetbuddies.domain.user.entity.User; +import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; +import jakarta.persistence.EntityManager; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + + +/** + * comment service는 다음과 같은 기능을 제공해야한다. + * 1. comment 저장 기능 + * 2. 특정 게시글에 따른 comment return + * 현재 게시글의 종류는 2가지로 각각 할인정보, 지원정보이다. + * 즉, 할인정보, 지원정보 ID가 들어오면 해당 게시글에 대한 댓글 정보를 다 가지고 올 수 있어야한다. + * 아마 관리 측면에선 댓글 삭제 기능도 필요할 것이다. + * 3. 특정 userid로 댓글 찾는 기능 + * 얘는 게시글 ID랑 제목 정도 같이??? + * 4. 특정 게시글 id로 댓글 찾는 기능 + */ + + +/* + 테스트마다 테스트케이스가 다를 수 있어서 공통로직으로 처리하지 않아 매우 깁니다... + */ +@SpringBootTest +@Transactional +class CommentServiceTestV2 { + + @Qualifier("discountCommentService") + @Autowired + CommentService discountCommentService; + + @Qualifier("supportCommentService") + @Autowired + CommentService supportCommentService; + + @Autowired + UserRepository userRepository; + @Autowired + DiscountInfoRepository discountInfoRepository; + @Autowired + SupportInfoRepository supportInfoRepository; + @Autowired + EntityManager em; + + @Test + public void saveDiscountInfoCommentTest(){ + User user1 = User.builder() + .name("tester1") + .email("1234") + .age(5) + .phoneNumber("123456") + .build(); + userRepository.save(user1); + + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); + discountInfoRepository.save(sale1); + + CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("굿") + .build(); + + discountCommentService.saveComment(user1.getId(), dto1); + em.flush(); + + List returnDto = discountCommentService.findByInfo(sale1.getId()); + + Assertions.assertThat(returnDto.size()).isEqualTo(1); + Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + + } + + @Test + public void saveDiscountInfoCommentTest2(){ + 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(); + userRepository.save(user2); + + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); + discountInfoRepository.save(sale1); + DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인") + .anonymousNumber(0) + .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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + em.flush(); + + List returnDto = discountCommentService.findByInfo(sale1.getId()); + List returnDto2 = discountCommentService.findByInfo(sale2.getId()); + Assertions.assertThat(returnDto.size()).isEqualTo(2); + Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(returnDto.get(1).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); + Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); + + Assertions.assertThat(returnDto2.size()).isEqualTo(1); + Assertions.assertThat(returnDto2.get(0).getDiscountInfoId()).isEqualTo(sale2.getId()); + Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); + + } + + @Test + void DiscountAnonymousCommentTest(){ + 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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + discountCommentService.saveComment(user1.getId(), dto4); + discountCommentService.saveComment(user3.getId(), dto4); + + em.flush(); + + List result = discountCommentService.findByInfo(sale1.getId()); + 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); + Assertions.assertThat(test3).isEqualTo(1); + Assertions.assertThat(test4).isEqualTo(3); + + + } + + @Test + public void saveSupportInfoCommentTest2(){ + 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(); + userRepository.save(user2); + + 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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); + + em.flush(); + + List returnDto = supportCommentService.findByInfo(info1.getId()); + List returnDto2 = supportCommentService.findByInfo(info2.getId()); + Assertions.assertThat(returnDto.size()).isEqualTo(2); + Assertions.assertThat(returnDto.get(0).getSupportInfoId()).isEqualTo(info1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(returnDto.get(1).getSupportInfoId()).isEqualTo(info1.getId()); + Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); + Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); + + Assertions.assertThat(returnDto2.size()).isEqualTo(1); + Assertions.assertThat(returnDto2.get(0).getSupportInfoId()).isEqualTo(info2.getId()); + Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); + + } + + @Test + void supportAnonymousCommentTest(){ + 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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); + supportCommentService.saveComment(user3.getId(), dto4); + supportCommentService.saveComment(user4.getId(), dto5); + supportCommentService.saveComment(user1.getId(), dto6); + + em.flush(); + + List returnDto = supportCommentService.findByInfo(info1.getId()); + List returnDto2 = supportCommentService.findByInfo(info2.getId()); + + 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); + Assertions.assertThat(test3).isEqualTo(3); + Assertions.assertThat(test4).isEqualTo(4); + 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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + discountCommentService.saveComment(user1.getId(), dto4); + discountCommentService.saveComment(user3.getId(), dto4); + discountCommentService.saveComment(user2.getId(), dto4); + //sale1 = 5 + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + + Page result1 = discountCommentService.findByInfoWithPaging(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 = discountCommentService.findByInfoWithPaging(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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); // 얘만 info2 + supportCommentService.saveComment(user3.getId(), dto4); + supportCommentService.saveComment(user4.getId(), dto5); + supportCommentService.saveComment(user1.getId(), dto6); + supportCommentService.saveComment(user2.getId(), dto5); + supportCommentService.saveComment(user3.getId(), dto5); + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + Page result1 = supportCommentService.findByInfoWithPaging(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 = supportCommentService.findByInfoWithPaging(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 From 87551a52e8dc940727adb78099f53242d3ece06d Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Tue, 30 Jul 2024 22:12:54 +0900 Subject: [PATCH 07/18] =?UTF-8?q?[remove]=20=ED=86=B5=ED=95=A9=20=EC=8B=9C?= =?UTF-8?q?=EB=8F=84=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/DiscountCommentService.java | 98 --- .../service/SupportCommentService.java | 100 --- .../comment/service/CommentServiceTestV2.java | 569 ------------------ 3 files changed, 767 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java delete mode 100644 src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java deleted file mode 100644 index d1a16e0f..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.bbteam.budgetbuddies.domain.comment.service; - -import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.comment.entity.Comment; -import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; -import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; -import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; -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.List; -import java.util.NoSuchElementException; -import java.util.Optional; -import java.util.stream.Collectors; - -@Service("discountCommentService") -@Transactional(readOnly = true) -@RequiredArgsConstructor -public class DiscountCommentService implements CommentService{ - private final CommentRepository commentRepository; - private final UserRepository userRepository; - private final DiscountInfoRepository discountInfoRepository; - - @Override - @Transactional - public CommentResponseDto.DiscountInfoCommentDto saveComment(Long userId, CommentRequestDto.DiscountInfoCommentDto dto) { - User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); - DiscountInfo info = discountInfoRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 - int anonymousNumber = getAnonymousNumber(user, info); - Comment comment = CommentConverter.toDiscountComment(dto, user, info, anonymousNumber); - Comment savedComment = commentRepository.save(comment); - - return CommentConverter.toDiscountInfoCommentDto(savedComment); - } - - private int getAnonymousNumber(User user, DiscountInfo info) { - int anonymousNumber; - Optional foundComment = commentRepository.findTopByUserAndDiscountInfo(user, info); - if (foundComment.isEmpty()) { - anonymousNumber = info.addAndGetAnonymousNumber(); - } else { - anonymousNumber = foundComment.get().getAnonymousNumber(); - } - return anonymousNumber; - } - - @Override - public List findByInfo(Long infoId) { - List commentList = commentRepository.findByDiscountInfo(infoId); - List collect = commentList.stream() - .map(CommentConverter::toDiscountInfoCommentDto) - .collect(Collectors.toList()); - return collect; - } - - @Override - public Page findByInfoWithPaging(Long infoId, Pageable pageable) { - Page commentPage = commentRepository.findByDiscountInfoWithPaging(infoId, pageable); - Page result = commentPage.map(CommentConverter::toDiscountInfoCommentDto); - return result; - } - - @Override - @Transactional - public void deleteComment(Long commentId) { - Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); - commentRepository.delete(comment); - } - - @Override - public CommentResponseDto.DiscountInfoCommentDto findCommentOne(Long commentId) { - Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); - if(comment.getDiscountInfo() == null){ - throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); - } - return CommentConverter.toDiscountInfoCommentDto(comment); - } - - @Override - @Transactional - public CommentResponseDto.DiscountInfoCommentDto modifyComment(CommentRequestDto.DiscountInfoCommentDto dto) { - Comment comment = commentRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); - if (comment.getDiscountInfo() == null) { - throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); - } - comment.modifyComment(dto.getContent()); - - return CommentConverter.toDiscountInfoCommentDto(comment); - } -} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java deleted file mode 100644 index d6c4ec1a..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java +++ /dev/null @@ -1,100 +0,0 @@ -package com.bbteam.budgetbuddies.domain.comment.service; - -import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.comment.entity.Comment; -import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; -import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; -import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; -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("supportCommentService") -@Transactional(readOnly = true) -@RequiredArgsConstructor -public class SupportCommentService implements CommentService { - - private final CommentRepository commentRepository; - private final UserRepository userRepository; - private final SupportInfoRepository supportInfoRepository; - - @Override - @Transactional - public CommentResponseDto.SupportInfoCommentDto saveComment(Long userId, CommentRequestDto.SupportInfoCommentDto dto) { - User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); - SupportInfo info = supportInfoRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 - int anonymousNumber = getAnonymousNumber(user, info); - Comment comment = CommentConverter.toSupportComment(dto, user, info, anonymousNumber); - Comment savedComment = commentRepository.save(comment); - - return CommentConverter.toSupportInfoCommentDto(savedComment); - } - - private int getAnonymousNumber(User user, SupportInfo info) { - int anonymousNumber; - Optional foundComment = commentRepository.findTopByUserAndSupportInfo(user, info); - if (foundComment.isEmpty()) { - anonymousNumber = info.addAndGetAnonymousNumber(); - } else { - anonymousNumber = foundComment.get().getAnonymousNumber(); - } - return anonymousNumber; - } - - @Override - public List findByInfo(Long infoId) { - List commentList = commentRepository.findBySupportInfo(infoId); - List collect = commentList.stream() - .map(CommentConverter::toSupportInfoCommentDto) - .collect(Collectors.toList()); - return collect; - } - - @Override - public Page findByInfoWithPaging(Long infoId, Pageable pageable) { - Page commentPage = commentRepository.findBySupportInfoWithPaging(infoId, 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); - } - - @Override - public CommentResponseDto.SupportInfoCommentDto findCommentOne(Long commentId) { - Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); - if(comment.getSupportInfo() == null){ - throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); - } - return CommentConverter.toSupportInfoCommentDto(comment); - } - - @Override - @Transactional - public CommentResponseDto.SupportInfoCommentDto modifyComment(CommentRequestDto.SupportInfoCommentDto dto) { - Comment comment = commentRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); - if (comment.getSupportInfo() == null) { - throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); - } - comment.modifyComment(dto.getContent()); - - return CommentConverter.toSupportInfoCommentDto(comment); - } -} diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java deleted file mode 100644 index aafa0422..00000000 --- a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java +++ /dev/null @@ -1,569 +0,0 @@ -package com.bbteam.budgetbuddies.domain.comment.service; - -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; -import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; -import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; -import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; -import com.bbteam.budgetbuddies.domain.user.entity.User; -import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; -import jakarta.persistence.EntityManager; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - - -/** - * comment service는 다음과 같은 기능을 제공해야한다. - * 1. comment 저장 기능 - * 2. 특정 게시글에 따른 comment return - * 현재 게시글의 종류는 2가지로 각각 할인정보, 지원정보이다. - * 즉, 할인정보, 지원정보 ID가 들어오면 해당 게시글에 대한 댓글 정보를 다 가지고 올 수 있어야한다. - * 아마 관리 측면에선 댓글 삭제 기능도 필요할 것이다. - * 3. 특정 userid로 댓글 찾는 기능 - * 얘는 게시글 ID랑 제목 정도 같이??? - * 4. 특정 게시글 id로 댓글 찾는 기능 - */ - - -/* - 테스트마다 테스트케이스가 다를 수 있어서 공통로직으로 처리하지 않아 매우 깁니다... - */ -@SpringBootTest -@Transactional -class CommentServiceTestV2 { - - @Qualifier("discountCommentService") - @Autowired - CommentService discountCommentService; - - @Qualifier("supportCommentService") - @Autowired - CommentService supportCommentService; - - @Autowired - UserRepository userRepository; - @Autowired - DiscountInfoRepository discountInfoRepository; - @Autowired - SupportInfoRepository supportInfoRepository; - @Autowired - EntityManager em; - - @Test - public void saveDiscountInfoCommentTest(){ - User user1 = User.builder() - .name("tester1") - .email("1234") - .age(5) - .phoneNumber("123456") - .build(); - userRepository.save(user1); - - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") - .anonymousNumber(0) - .build(); - discountInfoRepository.save(sale1); - - CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() - .discountInfoId(sale1.getId()) - .content("굿") - .build(); - - discountCommentService.saveComment(user1.getId(), dto1); - em.flush(); - - List returnDto = discountCommentService.findByInfo(sale1.getId()); - - Assertions.assertThat(returnDto.size()).isEqualTo(1); - Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - - } - - @Test - public void saveDiscountInfoCommentTest2(){ - 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(); - userRepository.save(user2); - - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") - .anonymousNumber(0) - .build(); - discountInfoRepository.save(sale1); - DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인") - .anonymousNumber(0) - .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(); - - discountCommentService.saveComment(user1.getId(), dto1); - discountCommentService.saveComment(user2.getId(), dto2); - discountCommentService.saveComment(user1.getId(), dto3); - - em.flush(); - - List returnDto = discountCommentService.findByInfo(sale1.getId()); - List returnDto2 = discountCommentService.findByInfo(sale2.getId()); - Assertions.assertThat(returnDto.size()).isEqualTo(2); - Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - Assertions.assertThat(returnDto.get(1).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); - Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); - - Assertions.assertThat(returnDto2.size()).isEqualTo(1); - Assertions.assertThat(returnDto2.get(0).getDiscountInfoId()).isEqualTo(sale2.getId()); - Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); - - } - - @Test - void DiscountAnonymousCommentTest(){ - 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(); - - discountCommentService.saveComment(user1.getId(), dto1); - discountCommentService.saveComment(user2.getId(), dto2); - discountCommentService.saveComment(user1.getId(), dto3); - - discountCommentService.saveComment(user1.getId(), dto4); - discountCommentService.saveComment(user3.getId(), dto4); - - em.flush(); - - List result = discountCommentService.findByInfo(sale1.getId()); - 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); - Assertions.assertThat(test3).isEqualTo(1); - Assertions.assertThat(test4).isEqualTo(3); - - - } - - @Test - public void saveSupportInfoCommentTest2(){ - 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(); - userRepository.save(user2); - - 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(); - - supportCommentService.saveComment(user1.getId(), dto1); - supportCommentService.saveComment(user2.getId(), dto2); - supportCommentService.saveComment(user1.getId(), dto3); - - em.flush(); - - List returnDto = supportCommentService.findByInfo(info1.getId()); - List returnDto2 = supportCommentService.findByInfo(info2.getId()); - Assertions.assertThat(returnDto.size()).isEqualTo(2); - Assertions.assertThat(returnDto.get(0).getSupportInfoId()).isEqualTo(info1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - Assertions.assertThat(returnDto.get(1).getSupportInfoId()).isEqualTo(info1.getId()); - Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); - Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); - - Assertions.assertThat(returnDto2.size()).isEqualTo(1); - Assertions.assertThat(returnDto2.get(0).getSupportInfoId()).isEqualTo(info2.getId()); - Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); - - } - - @Test - void supportAnonymousCommentTest(){ - 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(); - - supportCommentService.saveComment(user1.getId(), dto1); - supportCommentService.saveComment(user2.getId(), dto2); - supportCommentService.saveComment(user1.getId(), dto3); - supportCommentService.saveComment(user3.getId(), dto4); - supportCommentService.saveComment(user4.getId(), dto5); - supportCommentService.saveComment(user1.getId(), dto6); - - em.flush(); - - List returnDto = supportCommentService.findByInfo(info1.getId()); - List returnDto2 = supportCommentService.findByInfo(info2.getId()); - - 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); - Assertions.assertThat(test3).isEqualTo(3); - Assertions.assertThat(test4).isEqualTo(4); - 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(); - - discountCommentService.saveComment(user1.getId(), dto1); - discountCommentService.saveComment(user2.getId(), dto2); - discountCommentService.saveComment(user1.getId(), dto3); - - discountCommentService.saveComment(user1.getId(), dto4); - discountCommentService.saveComment(user3.getId(), dto4); - discountCommentService.saveComment(user2.getId(), dto4); - //sale1 = 5 - em.flush(); - - PageRequest pageRequest1 = PageRequest.of(0, 2); - - Page result1 = discountCommentService.findByInfoWithPaging(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 = discountCommentService.findByInfoWithPaging(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(); - - supportCommentService.saveComment(user1.getId(), dto1); - supportCommentService.saveComment(user2.getId(), dto2); - supportCommentService.saveComment(user1.getId(), dto3); // 얘만 info2 - supportCommentService.saveComment(user3.getId(), dto4); - supportCommentService.saveComment(user4.getId(), dto5); - supportCommentService.saveComment(user1.getId(), dto6); - supportCommentService.saveComment(user2.getId(), dto5); - supportCommentService.saveComment(user3.getId(), dto5); - em.flush(); - - PageRequest pageRequest1 = PageRequest.of(0, 2); - Page result1 = supportCommentService.findByInfoWithPaging(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 = supportCommentService.findByInfoWithPaging(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 From 59f1d3ad768a0be4ca6ceb3f498ac9098214bdcb Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:51:04 +0900 Subject: [PATCH 08/18] =?UTF-8?q?[feat]=20CommentService=20=EC=9E=AC?= =?UTF-8?q?=ED=86=B5=ED=95=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/AbstractCommentService.java | 29 +++++ .../comment/service/CommentService.java | 43 ++++---- .../service/DiscountCommentService.java | 103 +++++++++++++++++ .../service/SupportCommentService.java | 104 ++++++++++++++++++ 4 files changed, 255 insertions(+), 24 deletions(-) create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java new file mode 100644 index 00000000..fa5f87f9 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java @@ -0,0 +1,29 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; + +import java.util.NoSuchElementException; +import java.util.Optional; + +public abstract class AbstractCommentService implements CommentService { + + protected final CommentRepository commentRepository; + + public AbstractCommentService(CommentRepository commentRepository) { + this.commentRepository = commentRepository; + } + + @Override + public void deleteComment(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); + commentRepository.delete(comment); + } + + @Override + public Optional findById(Long commentId) { + return commentRepository.findById(commentId); + } +} 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 19aaa266..29661e29 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 @@ -1,44 +1,39 @@ package com.bbteam.budgetbuddies.domain.comment.service; +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; +import com.bbteam.budgetbuddies.domain.user.entity.User; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; +import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; -public interface CommentService { - CommentResponseDto.SupportInfoSuccessDto saveSupportComment(Long userId, CommentRequestDto.SupportInfoCommentDto dto); - CommentResponseDto.DiscountInfoSuccessDto saveDiscountComment(Long userId, CommentRequestDto.DiscountInfoCommentDto dto); +public interface CommentService { - /** - * - * @param discountInfoId - * @return List - * 해당 로직은 익명 구분을 위한 익명 구분 숫자도 같이 return 합니다. - */ - List findByDiscountInfo(Long discountInfoId); + T saveComment(Long userId, R dto); - /** - * - * @param supportInfoId - * @return List - * 해당 로직은 익명 구분을 위한 익명 구분 숫자도 같이 return 합니다. - */ - List findBySupportInfo(Long supportInfoId); - Page findByDiscountInfoWithPaging(Long discountInfoId, Pageable pageable); - Page findBySupportInfoWithPaging(Long supportInfoId, Pageable pageable); + List findByInfo(Long infoId); + + + Page findByInfoWithPaging(Long infoId, Pageable pageable); + void deleteComment(Long commentId); - CommentResponseDto.DiscountInfoCommentDto findDiscountCommentOne(Long commentId); - CommentResponseDto.SupportInfoCommentDto findSupportCommentOne(Long commentId); + T findCommentOne(Long commentId); + - //dirty checking 사용해서 변경 - CommentResponseDto.DiscountInfoCommentDto modifyDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto); - CommentResponseDto.SupportInfoCommentDto modifySupportComment(CommentRequestDto.SupportInfoCommentDto dto); + T modifyComment(CommentRequestDto.CommentModifyDto dto); + Optional findById(Long commentId); } diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java new file mode 100644 index 00000000..b5a146a4 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/DiscountCommentService.java @@ -0,0 +1,103 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; +import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; +import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; +import com.bbteam.budgetbuddies.domain.user.entity.User; +import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; +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.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service("discountCommentService") +@Transactional(readOnly = true) +public class DiscountCommentService extends AbstractCommentService { + private final UserRepository userRepository; + private final DiscountInfoRepository discountInfoRepository; + + public DiscountCommentService(CommentRepository commentRepository, UserRepository userRepository, DiscountInfoRepository discountInfoRepository) { + super(commentRepository); + this.userRepository = userRepository; + this.discountInfoRepository = discountInfoRepository; + } + + @Override + @Transactional + public CommentResponseDto.DiscountInfoCommentDto saveComment(Long userId, CommentRequestDto.DiscountInfoCommentDto dto) { + User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); + DiscountInfo info = discountInfoRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 + int anonymousNumber = getAnonymousNumber(user, info); + Comment comment = CommentConverter.toDiscountComment(dto, user, info, anonymousNumber); + Comment savedComment = commentRepository.save(comment); + + return CommentConverter.toDiscountInfoCommentDto(savedComment); + } + + + + private int getAnonymousNumber(User user, DiscountInfo info) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndDiscountInfo(user, info); + if (foundComment.isEmpty()) { + anonymousNumber = info.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } + + @Override + public List findByInfo(Long infoId) { + List commentList = commentRepository.findByDiscountInfo(infoId); + List collect = commentList.stream() + .map(CommentConverter::toDiscountInfoCommentDto) + .collect(Collectors.toList()); + return collect; + } + + @Override + public Page findByInfoWithPaging(Long infoId, Pageable pageable) { + Page commentPage = commentRepository.findByDiscountInfoWithPaging(infoId, pageable); + Page result = commentPage.map(CommentConverter::toDiscountInfoCommentDto); + return result; + } + + @Override + @Transactional + public void deleteComment(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such id")); + commentRepository.delete(comment); + } + + @Override + public CommentResponseDto.DiscountInfoCommentDto findCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getDiscountInfo() == null){ + throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toDiscountInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.DiscountInfoCommentDto modifyComment(CommentRequestDto.CommentModifyDto dto) { + Comment comment = commentRepository.findById(dto.getCommentId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if (comment.getDiscountInfo() == null) { + throw new RuntimeException("DiscountInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toDiscountInfoCommentDto(comment); + } +} diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java new file mode 100644 index 00000000..123483d5 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java @@ -0,0 +1,104 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; +import com.bbteam.budgetbuddies.domain.comment.service.before.CommentService; +import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; +import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; +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.List; +import java.util.NoSuchElementException; +import java.util.Optional; +import java.util.stream.Collectors; + +@Service("supportCommentService") +@Transactional(readOnly = true) +public class SupportCommentService extends AbstractCommentService { + + private final UserRepository userRepository; + private final SupportInfoRepository supportInfoRepository; + + public SupportCommentService(CommentRepository commentRepository, UserRepository userRepository, SupportInfoRepository supportInfoRepository) { + super(commentRepository); + this.userRepository = userRepository; + this.supportInfoRepository = supportInfoRepository; + } + + @Override + @Transactional + public CommentResponseDto.SupportInfoCommentDto saveComment(Long userId, CommentRequestDto.SupportInfoCommentDto dto) { + User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("유저 존재 x")); + SupportInfo info = supportInfoRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("정보 존재 x")); // dto에서 infoId를 추출하여 찾는 메서드 + int anonymousNumber = getAnonymousNumber(user, info); + Comment comment = CommentConverter.toSupportComment(dto, user, info, anonymousNumber); + Comment savedComment = commentRepository.save(comment); + + return CommentConverter.toSupportInfoCommentDto(savedComment); + } + + private int getAnonymousNumber(User user, SupportInfo info) { + int anonymousNumber; + Optional foundComment = commentRepository.findTopByUserAndSupportInfo(user, info); + if (foundComment.isEmpty()) { + anonymousNumber = info.addAndGetAnonymousNumber(); + } else { + anonymousNumber = foundComment.get().getAnonymousNumber(); + } + return anonymousNumber; + } + + @Override + public List findByInfo(Long infoId) { + List commentList = commentRepository.findBySupportInfo(infoId); + List collect = commentList.stream() + .map(CommentConverter::toSupportInfoCommentDto) + .collect(Collectors.toList()); + return collect; + } + + @Override + public Page findByInfoWithPaging(Long infoId, Pageable pageable) { + Page commentPage = commentRepository.findBySupportInfoWithPaging(infoId, 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); + } + + @Override + public CommentResponseDto.SupportInfoCommentDto findCommentOne(Long commentId) { + Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); + if(comment.getSupportInfo() == null){ + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + return CommentConverter.toSupportInfoCommentDto(comment); + } + + @Override + @Transactional + public CommentResponseDto.SupportInfoCommentDto modifyComment(CommentRequestDto.CommentModifyDto dto) { + Comment comment = commentRepository.findById(dto.getCommentId()).orElseThrow(() -> new NoSuchElementException("xxx")); + if (comment.getSupportInfo() == null) { + throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); + } + comment.modifyComment(dto.getContent()); + + return CommentConverter.toSupportInfoCommentDto(comment); + } +} From 00882731a9c87a0429a88f3376bd5453eac2fb07 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:51:27 +0900 Subject: [PATCH 09/18] =?UTF-8?q?[test]=20CommentService=20=ED=86=B5?= =?UTF-8?q?=ED=95=A9=EC=97=90=20=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentServiceTest.java | 31 +- .../comment/service/CommentServiceTestV2.java | 569 ++++++++++++++++++ 2 files changed, 599 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java 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 47636c82..9dfacb8c 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 @@ -2,6 +2,8 @@ import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.comment.entity.Comment; +import com.bbteam.budgetbuddies.domain.comment.service.before.CommentService; import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; @@ -15,7 +17,6 @@ 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; @@ -558,6 +559,34 @@ void SupportInfoPagingTest() { Assertions.assertThat(list2.get(0).getAnonymousNumber()).isEqualTo(2); } + @Test + void discountInfoCommentModifyTest() { + User user1 = User.builder() + .name("tester1") + .email("1234") + .age(5) + .phoneNumber("123456") + .build(); + userRepository.save(user1); + DiscountInfo sale1 = DiscountInfo.builder().anonymousNumber(0).title("무신사 할인").build(); + discountInfoRepository.save(sale1); + + CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("굿") + .build(); + commentService.saveDiscountComment(user1.getId(), dto1); + CommentResponseDto.DiscountInfoCommentDto test = commentService.findByDiscountInfo(sale1.getId()).get(0); + CommentRequestDto.CommentModifyDto content = CommentRequestDto.CommentModifyDto.builder() + .commentId(test.getCommentId()) + .content("잘했어요!") + .build(); + em.clear(); + commentService.modifyDiscountComment(content); + Comment byId = commentService.findById(1L); + Assertions.assertThat(byId.getContent()).isEqualTo("잘했어요!"); + } + } \ No newline at end of file diff --git a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java new file mode 100644 index 00000000..ff919466 --- /dev/null +++ b/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTestV2.java @@ -0,0 +1,569 @@ +package com.bbteam.budgetbuddies.domain.comment.service; + +import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; +import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; +import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; +import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; +import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; +import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; +import com.bbteam.budgetbuddies.domain.user.entity.User; +import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; +import jakarta.persistence.EntityManager; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + + +/** + * comment service는 다음과 같은 기능을 제공해야한다. + * 1. comment 저장 기능 + * 2. 특정 게시글에 따른 comment return + * 현재 게시글의 종류는 2가지로 각각 할인정보, 지원정보이다. + * 즉, 할인정보, 지원정보 ID가 들어오면 해당 게시글에 대한 댓글 정보를 다 가지고 올 수 있어야한다. + * 아마 관리 측면에선 댓글 삭제 기능도 필요할 것이다. + * 3. 특정 userid로 댓글 찾는 기능 + * 얘는 게시글 ID랑 제목 정도 같이??? + * 4. 특정 게시글 id로 댓글 찾는 기능 + */ + + +/* + 테스트마다 테스트케이스가 다를 수 있어서 공통로직으로 처리하지 않아 매우 깁니다... + */ +@SpringBootTest +@Transactional +class CommentServiceTestV2 { + + @Qualifier("discountCommentService") + @Autowired + CommentService discountCommentService; + + @Qualifier("supportCommentService") + @Autowired + com.bbteam.budgetbuddies.domain.comment.service.CommentService supportCommentService; + + @Autowired + UserRepository userRepository; + @Autowired + DiscountInfoRepository discountInfoRepository; + @Autowired + SupportInfoRepository supportInfoRepository; + @Autowired + EntityManager em; + + @Test + public void saveDiscountInfoCommentTest(){ + User user1 = User.builder() + .name("tester1") + .email("1234") + .age(5) + .phoneNumber("123456") + .build(); + userRepository.save(user1); + + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); + discountInfoRepository.save(sale1); + + CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() + .discountInfoId(sale1.getId()) + .content("굿") + .build(); + + discountCommentService.saveComment(user1.getId(), dto1); + em.flush(); + + List returnDto = discountCommentService.findByInfo(sale1.getId()); + + Assertions.assertThat(returnDto.size()).isEqualTo(1); + Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + + } + + @Test + public void saveDiscountInfoCommentTest2(){ + 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(); + userRepository.save(user2); + + DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") + .anonymousNumber(0) + .build(); + discountInfoRepository.save(sale1); + DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인") + .anonymousNumber(0) + .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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + em.flush(); + + List returnDto = discountCommentService.findByInfo(sale1.getId()); + List returnDto2 = discountCommentService.findByInfo(sale2.getId()); + Assertions.assertThat(returnDto.size()).isEqualTo(2); + Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(returnDto.get(1).getDiscountInfoId()).isEqualTo(sale1.getId()); + Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); + Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); + + Assertions.assertThat(returnDto2.size()).isEqualTo(1); + Assertions.assertThat(returnDto2.get(0).getDiscountInfoId()).isEqualTo(sale2.getId()); + Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); + + } + + @Test + void DiscountAnonymousCommentTest(){ + 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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + discountCommentService.saveComment(user1.getId(), dto4); + discountCommentService.saveComment(user3.getId(), dto4); + + em.flush(); + + List result = discountCommentService.findByInfo(sale1.getId()); + 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); + Assertions.assertThat(test3).isEqualTo(1); + Assertions.assertThat(test4).isEqualTo(3); + + + } + + @Test + public void saveSupportInfoCommentTest2(){ + 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(); + userRepository.save(user2); + + 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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); + + em.flush(); + + List returnDto = supportCommentService.findByInfo(info1.getId()); + List returnDto2 = supportCommentService.findByInfo(info2.getId()); + Assertions.assertThat(returnDto.size()).isEqualTo(2); + Assertions.assertThat(returnDto.get(0).getSupportInfoId()).isEqualTo(info1.getId()); + Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); + Assertions.assertThat(returnDto.get(1).getSupportInfoId()).isEqualTo(info1.getId()); + Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); + Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); + + Assertions.assertThat(returnDto2.size()).isEqualTo(1); + Assertions.assertThat(returnDto2.get(0).getSupportInfoId()).isEqualTo(info2.getId()); + Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); + Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); + + } + + @Test + void supportAnonymousCommentTest(){ + 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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); + supportCommentService.saveComment(user3.getId(), dto4); + supportCommentService.saveComment(user4.getId(), dto5); + supportCommentService.saveComment(user1.getId(), dto6); + + em.flush(); + + List returnDto = supportCommentService.findByInfo(info1.getId()); + List returnDto2 = supportCommentService.findByInfo(info2.getId()); + + 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); + Assertions.assertThat(test3).isEqualTo(3); + Assertions.assertThat(test4).isEqualTo(4); + 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(); + + discountCommentService.saveComment(user1.getId(), dto1); + discountCommentService.saveComment(user2.getId(), dto2); + discountCommentService.saveComment(user1.getId(), dto3); + + discountCommentService.saveComment(user1.getId(), dto4); + discountCommentService.saveComment(user3.getId(), dto4); + discountCommentService.saveComment(user2.getId(), dto4); + //sale1 = 5 + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + + Page result1 = discountCommentService.findByInfoWithPaging(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 = discountCommentService.findByInfoWithPaging(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(); + + supportCommentService.saveComment(user1.getId(), dto1); + supportCommentService.saveComment(user2.getId(), dto2); + supportCommentService.saveComment(user1.getId(), dto3); // 얘만 info2 + supportCommentService.saveComment(user3.getId(), dto4); + supportCommentService.saveComment(user4.getId(), dto5); + supportCommentService.saveComment(user1.getId(), dto6); + supportCommentService.saveComment(user2.getId(), dto5); + supportCommentService.saveComment(user3.getId(), dto5); + em.flush(); + + PageRequest pageRequest1 = PageRequest.of(0, 2); + Page result1 = supportCommentService.findByInfoWithPaging(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 = supportCommentService.findByInfoWithPaging(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 From 3eb685ccf19a7a567725bb4105fd54d5e9a77e25 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:55:50 +0900 Subject: [PATCH 10/18] =?UTF-8?q?[refactor]=20Controller=20=EC=9D=B4?= =?UTF-8?q?=EA=B4=80=EC=97=90=20=EB=94=B0=EB=A5=B8=20annotation=20?= =?UTF-8?q?=EB=B9=84=ED=99=9C=EC=84=B1=ED=99=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) 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 6fd4a094..7ebab75e 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 @@ -2,7 +2,7 @@ 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 com.bbteam.budgetbuddies.domain.comment.service.before.CommentService; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -10,17 +10,17 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -@RestController +//@RestController @RequiredArgsConstructor public class CommentController implements CommentControllerApi { private final CommentService commentService; @PostMapping("/discounts/comments") - public ResponseEntity saveDiscountInfoComment( + public ResponseEntity saveDiscountInfoComment( @RequestParam("userId") Long userId, @RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){ - CommentResponseDto.DiscountInfoSuccessDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto); + CommentResponseDto.DiscountInfoCommentDto dto = commentService.saveDiscountComment(userId, discountInfoCommentDto); return ResponseEntity.ok(dto); } @@ -35,10 +35,10 @@ public ResponseEntity> findAllBy @PostMapping("/supports/comments") - public ResponseEntity saveSupportInfoComment( + public ResponseEntity saveSupportInfoComment( @RequestParam("userId") Long userId, @RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){ - CommentResponseDto.SupportInfoSuccessDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto); + CommentResponseDto.SupportInfoCommentDto dto = commentService.saveSupportComment(userId, supportInfoCommentDto); return ResponseEntity.ok(dto); } @@ -51,10 +51,12 @@ public ResponseEntity> findAllByS return ResponseEntity.ok(result); } - + @PostMapping("/comments/delete") public ResponseEntity deleteComment(@RequestParam("commentId") Long commentId) { commentService.deleteComment(commentId); return ResponseEntity.ok("ok"); } + + } From e4d9b6db2f1084fa6e17bd5478676ee59bdc4171 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:56:35 +0900 Subject: [PATCH 11/18] =?UTF-8?q?[refactor]=20CommentControllerApi=20?= =?UTF-8?q?=EB=B0=98=ED=99=98=20API=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/controller/CommentControllerApi.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java index 363412ad..730f6b6e 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java @@ -22,7 +22,7 @@ public interface CommentControllerApi { @Parameter(name = "discountInfoId", description = "댓글을 다는 할인 정보 게시글 id입니다. requestBody"), @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), }) - ResponseEntity saveDiscountInfoComment( + ResponseEntity saveDiscountInfoComment( Long userId, CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto); @@ -49,7 +49,7 @@ ResponseEntity> findAllByDiscoun @Parameter(name = "supportInfoId", description = "댓글을 다는 지원 정보 게시글 id입니다. requestBody"), @Parameter(name = "content", description = "댓글 내용입니다. requestBody"), }) - ResponseEntity saveSupportInfoComment( + ResponseEntity saveSupportInfoComment( Long userId, CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto); From fcdb71090f63ec4014e53de170626d174162bd4c Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:57:05 +0900 Subject: [PATCH 12/18] =?UTF-8?q?[remove]=20SuccessDto=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/converter/CommentConverter.java | 17 ----------------- .../domain/comment/dto/CommentResponseDto.java | 16 ---------------- 2 files changed, 33 deletions(-) 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 6a5a9f8d..8e424e50 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 @@ -53,22 +53,5 @@ public static CommentResponseDto.SupportInfoCommentDto toSupportInfoCommentDto(C } - 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()) - .build(); - } - - 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()) - .build(); - } } 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 1e7bcb9e..932cad45 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 @@ -31,22 +31,6 @@ public static class SupportInfoCommentDto{ private LocalDateTime createdAt; } - @Getter - @Builder - public static class DiscountInfoSuccessDto{ - private Long commentId; - private Long userId; - private Long discountInfoId; - private String content; - } - @Getter - @Builder - public static class SupportInfoSuccessDto{ - private Long commentId; - private Long userId; - private Long supportInfoId; - private String content; - } } From 5aa7531f971744beac504e8e78818f585e9f6006 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:57:33 +0900 Subject: [PATCH 13/18] =?UTF-8?q?[feat]=20CommentModifyDto=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EC=97=90=20=EC=82=AC=EC=9A=A9=ED=95=A0=20Dto=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/dto/CommentRequestDto.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentRequestDto.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentRequestDto.java index 45b91e8e..9b516614 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentRequestDto.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/dto/CommentRequestDto.java @@ -2,7 +2,6 @@ import lombok.Builder; import lombok.Getter; -import lombok.Setter; public class CommentRequestDto { @@ -19,4 +18,11 @@ public static class SupportInfoCommentDto { private String content; private Long supportInfoId; } + + @Getter + @Builder + public static class CommentModifyDto { + private String content; + private Long commentId; + } } From d58fcb41e7047f756cbe182b2b3a7b4e842f5318 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 00:59:37 +0900 Subject: [PATCH 14/18] =?UTF-8?q?[remove]=20=EA=B5=AC=20CommentService=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/service/CommentServiceImpl.java | 180 ------ .../comment/service/CommentServiceTest.java | 592 ------------------ 2 files changed, 772 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java delete mode 100644 src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java 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 deleted file mode 100644 index 97ee1ea3..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceImpl.java +++ /dev/null @@ -1,180 +0,0 @@ -package com.bbteam.budgetbuddies.domain.comment.service; - - -import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.comment.entity.Comment; -import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; -import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; -import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; -import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; -import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; -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.*; -import java.util.stream.Collectors; - -// 임시로 유저는 service에서 찾아서 처리하는 로직으로 작성함 -@Service -@Transactional(readOnly = true) -@RequiredArgsConstructor -public class CommentServiceImpl implements CommentService{ - - private final CommentRepository commentRepository; - private final UserRepository userRepository; - private final DiscountInfoRepository discountInfoRepository; - private final SupportInfoRepository supportInfoRepository; - - @Override - @Transactional - 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()); - 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 - @Transactional - 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()); - 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(CommentConverter::toDiscountInfoCommentDto) - .collect(Collectors.toList()); - return collect; - - } - - @Override - public List findBySupportInfo(Long supportInfoId) { - List commentList = commentRepository.findBySupportInfo(supportInfoId); - HashMap anonymousMapping = countAnonymousNumber(commentList); - List collect = commentList.stream() - .map(CommentConverter::toSupportInfoCommentDto) - .collect(Collectors.toList()); - return collect; - } - - private static HashMap countAnonymousNumber(List commentList) { - HashMap anonymousMapping = new HashMap<>(); - Long count = 1L; - for (Comment comment : commentList) { - Long id = comment.getUser().getId(); - if(!anonymousMapping.containsKey(id)){ - anonymousMapping.put(id, count); - count++; - } - } - return anonymousMapping; - } - - @Override - public Page findByDiscountInfoWithPaging(Long discountInfoId, Pageable pageable) { - Page commentPage = commentRepository.findByDiscountInfoWithPaging(discountInfoId, pageable); - Page result = commentPage.map(CommentConverter::toDiscountInfoCommentDto); - return result; - } - - @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); - } - - @Override - public CommentResponseDto.DiscountInfoCommentDto findDiscountCommentOne(Long commentId) { - Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); - if(comment.getDiscountInfo() == null){ - throw new RuntimeException("discountInfo comment에 대한 요청이 아닙니다."); - } - return CommentConverter.toDiscountInfoCommentDto(comment); - - } - - @Override - public CommentResponseDto.SupportInfoCommentDto findSupportCommentOne(Long commentId) { - Comment comment = commentRepository.findById(commentId).orElseThrow(() -> new NoSuchElementException("No such comment")); - if(comment.getSupportInfo() == null){ - throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); - } - return CommentConverter.toSupportInfoCommentDto(comment); - } - - @Override - @Transactional - public CommentResponseDto.DiscountInfoCommentDto modifyDiscountComment(CommentRequestDto.DiscountInfoCommentDto dto) { - Comment comment = commentRepository.findById(dto.getDiscountInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); - if(comment.getDiscountInfo() == null) { - throw new RuntimeException("discountInfo comment에 대한 요청이 아닙니다."); - } - comment.modifyComment(dto.getContent()); - - return CommentConverter.toDiscountInfoCommentDto(comment); - } - - @Override - @Transactional - public CommentResponseDto.SupportInfoCommentDto modifySupportComment(CommentRequestDto.SupportInfoCommentDto dto) { - Comment comment = commentRepository.findById(dto.getSupportInfoId()).orElseThrow(() -> new NoSuchElementException("xxx")); - if (comment.getSupportInfo() == null) { - throw new RuntimeException("supportInfo comment에 대한 요청이 아닙니다."); - } - comment.modifyComment(dto.getContent()); - - return CommentConverter.toSupportInfoCommentDto(comment); - } -} 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 deleted file mode 100644 index 9dfacb8c..00000000 --- a/src/test/java/com/bbteam/budgetbuddies/domain/comment/service/CommentServiceTest.java +++ /dev/null @@ -1,592 +0,0 @@ -package com.bbteam.budgetbuddies.domain.comment.service; - -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; -import com.bbteam.budgetbuddies.domain.comment.entity.Comment; -import com.bbteam.budgetbuddies.domain.comment.service.before.CommentService; -import com.bbteam.budgetbuddies.domain.discountinfo.entity.DiscountInfo; -import com.bbteam.budgetbuddies.domain.discountinfo.repository.DiscountInfoRepository; -import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; -import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; -import com.bbteam.budgetbuddies.domain.user.entity.User; -import com.bbteam.budgetbuddies.domain.user.repository.UserRepository; -import jakarta.persistence.EntityManager; -import org.assertj.core.api.Assertions; -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.transaction.annotation.Transactional; - -import java.util.List; - - -/** - * comment service는 다음과 같은 기능을 제공해야한다. - * 1. comment 저장 기능 - * 2. 특정 게시글에 따른 comment return - * 현재 게시글의 종류는 2가지로 각각 할인정보, 지원정보이다. - * 즉, 할인정보, 지원정보 ID가 들어오면 해당 게시글에 대한 댓글 정보를 다 가지고 올 수 있어야한다. - * 아마 관리 측면에선 댓글 삭제 기능도 필요할 것이다. - * 3. 특정 userid로 댓글 찾는 기능 - * 얘는 게시글 ID랑 제목 정도 같이??? - * 4. 특정 게시글 id로 댓글 찾는 기능 - */ - - -/* - 테스트마다 테스트케이스가 다를 수 있어서 공통로직으로 처리하지 않아 매우 깁니다... - */ -@SpringBootTest -@Transactional -class CommentServiceTest { - @Autowired - CommentService commentService; - - @Autowired - UserRepository userRepository; - @Autowired - DiscountInfoRepository discountInfoRepository; - @Autowired - SupportInfoRepository supportInfoRepository; - @Autowired - EntityManager em; - - @Test - public void saveDiscountInfoCommentTest(){ - User user1 = User.builder() - .name("tester1") - .email("1234") - .age(5) - .phoneNumber("123456") - .build(); - userRepository.save(user1); - - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") - .anonymousNumber(0) - .build(); - discountInfoRepository.save(sale1); - - CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() - .discountInfoId(sale1.getId()) - .content("굿") - .build(); - - commentService.saveDiscountComment(user1.getId(), dto1); - em.flush(); - - List returnDto = commentService.findByDiscountInfo(sale1.getId()); - - Assertions.assertThat(returnDto.size()).isEqualTo(1); - Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - - } - - @Test - public void saveDiscountInfoCommentTest2(){ - 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(); - userRepository.save(user2); - - DiscountInfo sale1 = DiscountInfo.builder().title("무신사 할인") - .anonymousNumber(0) - .build(); - discountInfoRepository.save(sale1); - DiscountInfo sale2 = DiscountInfo.builder().title("핫트랙스 할인") - .anonymousNumber(0) - .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(); - - commentService.saveDiscountComment(user1.getId(), dto1); - commentService.saveDiscountComment(user2.getId(), dto2); - commentService.saveDiscountComment(user1.getId(), dto3); - - em.flush(); - - List returnDto = commentService.findByDiscountInfo(sale1.getId()); - List returnDto2 = commentService.findByDiscountInfo(sale2.getId()); - Assertions.assertThat(returnDto.size()).isEqualTo(2); - Assertions.assertThat(returnDto.get(0).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - Assertions.assertThat(returnDto.get(1).getDiscountInfoId()).isEqualTo(sale1.getId()); - Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); - Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); - - Assertions.assertThat(returnDto2.size()).isEqualTo(1); - Assertions.assertThat(returnDto2.get(0).getDiscountInfoId()).isEqualTo(sale2.getId()); - Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); - - } - - @Test - void DiscountAnonymousCommentTest(){ - 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); - - em.flush(); - - List result = commentService.findByDiscountInfo(sale1.getId()); - 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); - Assertions.assertThat(test3).isEqualTo(1); - Assertions.assertThat(test4).isEqualTo(3); - - - } - - @Test - public void saveSupportInfoCommentTest2(){ - 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(); - userRepository.save(user2); - - 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(); - - commentService.saveSupportComment(user1.getId(), dto1); - commentService.saveSupportComment(user2.getId(), dto2); - commentService.saveSupportComment(user1.getId(), dto3); - - em.flush(); - - List returnDto = commentService.findBySupportInfo(info1.getId()); - List returnDto2 = commentService.findBySupportInfo(info2.getId()); - Assertions.assertThat(returnDto.size()).isEqualTo(2); - Assertions.assertThat(returnDto.get(0).getSupportInfoId()).isEqualTo(info1.getId()); - Assertions.assertThat(returnDto.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto.get(0).getContent()).isEqualTo("굿"); - Assertions.assertThat(returnDto.get(1).getSupportInfoId()).isEqualTo(info1.getId()); - Assertions.assertThat(returnDto.get(1).getUserId()).isEqualTo(user2.getId()); - Assertions.assertThat(returnDto.get(1).getContent()).isEqualTo("좋아요"); - - Assertions.assertThat(returnDto2.size()).isEqualTo(1); - Assertions.assertThat(returnDto2.get(0).getSupportInfoId()).isEqualTo(info2.getId()); - Assertions.assertThat(returnDto2.get(0).getUserId()).isEqualTo(user1.getId()); - Assertions.assertThat(returnDto2.get(0).getContent()).isEqualTo("유용해요!"); - - } - - @Test - void supportAnonymousCommentTest(){ - 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); - commentService.saveSupportComment(user3.getId(), dto4); - commentService.saveSupportComment(user4.getId(), dto5); - commentService.saveSupportComment(user1.getId(), dto6); - - em.flush(); - - List returnDto = commentService.findBySupportInfo(info1.getId()); - List returnDto2 = commentService.findBySupportInfo(info2.getId()); - - 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); - Assertions.assertThat(test3).isEqualTo(3); - Assertions.assertThat(test4).isEqualTo(4); - 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); - } - - @Test - void discountInfoCommentModifyTest() { - User user1 = User.builder() - .name("tester1") - .email("1234") - .age(5) - .phoneNumber("123456") - .build(); - userRepository.save(user1); - DiscountInfo sale1 = DiscountInfo.builder().anonymousNumber(0).title("무신사 할인").build(); - discountInfoRepository.save(sale1); - - CommentRequestDto.DiscountInfoCommentDto dto1 = CommentRequestDto.DiscountInfoCommentDto.builder() - .discountInfoId(sale1.getId()) - .content("굿") - .build(); - commentService.saveDiscountComment(user1.getId(), dto1); - CommentResponseDto.DiscountInfoCommentDto test = commentService.findByDiscountInfo(sale1.getId()).get(0); - CommentRequestDto.CommentModifyDto content = CommentRequestDto.CommentModifyDto.builder() - .commentId(test.getCommentId()) - .content("잘했어요!") - .build(); - em.clear(); - commentService.modifyDiscountComment(content); - Comment byId = commentService.findById(1L); - Assertions.assertThat(byId.getContent()).isEqualTo("잘했어요!"); - } - - - -} \ No newline at end of file From c3b1aadf197af4217ec113672c32fefed89122ea Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 01:00:03 +0900 Subject: [PATCH 15/18] =?UTF-8?q?[refactor]=20CommentController=20?= =?UTF-8?q?=EA=B5=90=EC=B2=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CommentControllerImpl.java | 60 ----------- .../controller/CommentControllerV2.java | 102 ++++++++++++++++++ 2 files changed, 102 insertions(+), 60 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerImpl.java create mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java 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 deleted file mode 100644 index 8f9691d7..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerImpl.java +++ /dev/null @@ -1,60 +0,0 @@ -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/controller/CommentControllerV2.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java new file mode 100644 index 00000000..9a4c70a5 --- /dev/null +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java @@ -0,0 +1,102 @@ +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.beans.factory.annotation.Qualifier; +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 +public class CommentControllerV2 implements CommentControllerApi { + + @Qualifier("discountCommentService") + private final CommentService discountCommentService; + + @Qualifier("supportCommentService") + private final CommentService supportCommentService; + + public CommentControllerV2(CommentService discountCommentService, + CommentService supportCommentService) { + this.discountCommentService = discountCommentService; + this.supportCommentService = supportCommentService; + } + + @PostMapping("/discounts/comments") + public ResponseEntity saveDiscountInfoComment( + @RequestParam("userId") Long userId, + @RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){ + CommentResponseDto.DiscountInfoCommentDto dto = discountCommentService.saveComment(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 = discountCommentService.findByInfoWithPaging(discountInfoId, pageable); + return ResponseEntity.ok(result); + } + + + @PostMapping("/supports/comments") + public ResponseEntity saveSupportInfoComment( + @RequestParam("userId") Long userId, + @RequestBody CommentRequestDto.SupportInfoCommentDto supportInfoCommentDto){ + CommentResponseDto.SupportInfoCommentDto dto = supportCommentService.saveComment(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 = supportCommentService.findByInfoWithPaging(supportInfoId, pageable); + return ResponseEntity.ok(result); + } + + @PostMapping("/comments/delete") + public ResponseEntity deleteComment(@RequestParam("commentId") Long commentId) { + discountCommentService.deleteComment(commentId); + return ResponseEntity.ok("ok"); + } + + @GetMapping("/supports/comments/modify") + public ResponseEntity findSupportOne(@RequestParam("commentId")Long commentId) { + CommentResponseDto.SupportInfoCommentDto result = supportCommentService.findCommentOne(commentId); + return ResponseEntity.ok(result); + } + + @PostMapping("/supports/comments/modify") + public ResponseEntity modifySupportOne( + @RequestBody CommentRequestDto.CommentModifyDto dto) { + CommentResponseDto.SupportInfoCommentDto result = supportCommentService.modifyComment(dto); + return ResponseEntity.ok(result); + } + + @GetMapping("/discounts/comments/modify") + public ResponseEntity findDiscountOne(@RequestParam("commentId")Long commentId) { + CommentResponseDto.DiscountInfoCommentDto result = discountCommentService.findCommentOne(commentId); + return ResponseEntity.ok(result); + } + + @PostMapping("/discounts/comments/modify") + public ResponseEntity modifyDiscountOne( + @RequestBody CommentRequestDto.CommentModifyDto dto) { + CommentResponseDto.DiscountInfoCommentDto result = discountCommentService.modifyComment(dto); + return ResponseEntity.ok(result); + } + + + +} From 4aed8c3e885b5d89aa90bea1e0b9b5a63da528fb Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Wed, 31 Jul 2024 01:01:11 +0900 Subject: [PATCH 16/18] =?UTF-8?q?[remove]=20CommentController=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/controller/CommentController.java | 62 ------------------- 1 file changed, 62 deletions(-) delete mode 100644 src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java 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 deleted file mode 100644 index 7ebab75e..00000000 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java +++ /dev/null @@ -1,62 +0,0 @@ -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.before.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 CommentController implements CommentControllerApi { - - private final CommentService commentService; - - @PostMapping("/discounts/comments") - public ResponseEntity saveDiscountInfoComment( - @RequestParam("userId") Long userId, - @RequestBody CommentRequestDto.DiscountInfoCommentDto discountInfoCommentDto){ - CommentResponseDto.DiscountInfoCommentDto 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.SupportInfoCommentDto 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); - } - - @PostMapping("/comments/delete") - public ResponseEntity deleteComment(@RequestParam("commentId") Long commentId) { - commentService.deleteComment(commentId); - return ResponseEntity.ok("ok"); - } - - - -} From 1b95fcc357610e5fbb64e17161f12bb9af8945e2 Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Thu, 1 Aug 2024 11:50:04 +0900 Subject: [PATCH 17/18] =?UTF-8?q?[feat]=20Swagger=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ntrollerV2.java => CommentController.java} | 7 ++- .../controller/CommentControllerApi.java | 48 ++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) rename src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/{CommentControllerV2.java => CommentController.java} (94%) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java similarity index 94% rename from src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java rename to src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java index 9a4c70a5..96f7e436 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerV2.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentController.java @@ -3,7 +3,6 @@ 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.beans.factory.annotation.Qualifier; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; @@ -12,7 +11,7 @@ import org.springframework.web.bind.annotation.*; @RestController -public class CommentControllerV2 implements CommentControllerApi { +public class CommentController implements CommentControllerApi { @Qualifier("discountCommentService") private final CommentService supportCommentService; - public CommentControllerV2(CommentService discountCommentService, - CommentService supportCommentService) { this.discountCommentService = discountCommentService; this.supportCommentService = supportCommentService; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java index 730f6b6e..ecaa37a4 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/controller/CommentControllerApi.java @@ -11,6 +11,9 @@ import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; public interface CommentControllerApi { @Operation(summary = "[User] 특정 할인 정보 게시글에 댓글달기", description = "특정 할인 정보 게시글에 댓글을 다는 API입니다.") @@ -75,6 +78,49 @@ ResponseEntity> findAllBySupportI @Parameters({ @Parameter(name = "commentId", description = "삭제할 댓글 id 입니다. parameter") }) - @GetMapping("/comments/delete") ResponseEntity deleteComment(Long commentId); + + @Operation(summary = "[User] SupportInfo의 댓글 요청 API ", description = "특정 댓글을 요청하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "commentId", description = "조회할 댓글 id 입니다. parameter") + }) + ResponseEntity findSupportOne(@RequestParam("commentId")Long commentId); + + @Operation(summary = "[User] SupprotInfo의 댓글 변경 API", description = "특정 댓글을 변경하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "commentId", description = "변경할 댓글 id 입니다. requestbody"), + @Parameter(name = "comment", description = "변경할 댓글 내용입니다.. requestbody") + + }) + ResponseEntity modifySupportOne( + @RequestBody CommentRequestDto.CommentModifyDto dto); + + + @Operation(summary = "[User] DiscountInfo의 댓글 요청 API ", description = "특정 댓글을 요청하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "commentId", description = "조회할 댓글 id 입니다. parameter") + }) + ResponseEntity findDiscountOne(@RequestParam("commentId")Long commentId); + + @Operation(summary = "[User] DiscountInfo의 댓글 변경 API", description = "특정 댓글을 변경하는 API입니다.") + @ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"), + }) + @Parameters({ + @Parameter(name = "commentId", description = "변경할 댓글 id 입니다. requestbody"), + @Parameter(name = "comment", description = "변경할 댓글 내용입니다.. requestbody") + + }) + ResponseEntity modifyDiscountOne( + @RequestBody CommentRequestDto.CommentModifyDto dto); + } From ab07c624be698e1a150e1c9854280de8cfc76c9f Mon Sep 17 00:00:00 2001 From: wnd01jun Date: Thu, 1 Aug 2024 11:53:18 +0900 Subject: [PATCH 18/18] =?UTF-8?q?[remove]=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20import=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/comment/service/AbstractCommentService.java | 2 -- .../domain/comment/service/SupportCommentService.java | 2 -- 2 files changed, 4 deletions(-) diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java index fa5f87f9..9bf31236 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/AbstractCommentService.java @@ -1,7 +1,5 @@ package com.bbteam.budgetbuddies.domain.comment.service; -import com.bbteam.budgetbuddies.domain.comment.converter.CommentConverter; -import com.bbteam.budgetbuddies.domain.comment.dto.CommentRequestDto; import com.bbteam.budgetbuddies.domain.comment.entity.Comment; import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; diff --git a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java index 123483d5..9e46d66b 100644 --- a/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java +++ b/src/main/java/com/bbteam/budgetbuddies/domain/comment/service/SupportCommentService.java @@ -5,12 +5,10 @@ import com.bbteam.budgetbuddies.domain.comment.dto.CommentResponseDto; import com.bbteam.budgetbuddies.domain.comment.entity.Comment; import com.bbteam.budgetbuddies.domain.comment.repository.CommentRepository; -import com.bbteam.budgetbuddies.domain.comment.service.before.CommentService; import com.bbteam.budgetbuddies.domain.supportinfo.entity.SupportInfo; import com.bbteam.budgetbuddies.domain.supportinfo.repository.SupportInfoRepository; 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;