From ea92d63748e05b0074599f2a73be667a4c871d8a Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:00:43 +0900 Subject: [PATCH 01/22] =?UTF-8?q?#152=20feat:=20PostLike=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sharemind/postLike/domain/PostLike.java | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/com/example/sharemind/postLike/domain/PostLike.java diff --git a/src/main/java/com/example/sharemind/postLike/domain/PostLike.java b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java new file mode 100644 index 00000000..d0df165d --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java @@ -0,0 +1,56 @@ +package com.example.sharemind.postLike.domain; + +import com.example.sharemind.customer.domain.Customer; +import com.example.sharemind.global.common.BaseEntity; +import com.example.sharemind.post.domain.Post; +import com.example.sharemind.postLike.exception.PostLikeErrorCode; +import com.example.sharemind.postLike.exception.PostLikeException; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +@Entity +public class PostLike extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "post_like_id") + private Long postLikeId; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "customer_id") + private Customer customer; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "post_id") + private Post post; + + @Builder + public PostLike(Customer customer, Post post) { + this.customer = customer; + this.post = post; + } + + public void updateIsActivatedTrue() { + checkIsActivatedFalse(); + + super.updateIsActivatedTrue(); + } + + private void checkIsActivatedFalse() { + if (this.isActivated()) { + throw new PostLikeException(PostLikeErrorCode.POST_ALREADY_LIKED); + } + } +} From cf99b6a1047d4caa87b9d17548ef369b0a6f24ae Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:01:42 +0900 Subject: [PATCH 02/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postLike/application/PostLikeService.java | 6 +++ .../application/PostLikeServiceImpl.java | 44 +++++++++++++++++++ .../repository/PostLikeRepository.java | 16 +++++++ 3 files changed, 66 insertions(+) create mode 100644 src/main/java/com/example/sharemind/postLike/application/PostLikeService.java create mode 100644 src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java create mode 100644 src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java diff --git a/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java b/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java new file mode 100644 index 00000000..4c6a0b65 --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java @@ -0,0 +1,6 @@ +package com.example.sharemind.postLike.application; + +public interface PostLikeService { + + void createPostLike(Long postId, Long customerId); +} diff --git a/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java b/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java new file mode 100644 index 00000000..88f9d1d5 --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java @@ -0,0 +1,44 @@ +package com.example.sharemind.postLike.application; + +import com.example.sharemind.customer.application.CustomerService; +import com.example.sharemind.customer.domain.Customer; +import com.example.sharemind.post.application.PostService; +import com.example.sharemind.post.domain.Post; +import com.example.sharemind.postLike.domain.PostLike; +import com.example.sharemind.postLike.exception.PostLikeErrorCode; +import com.example.sharemind.postLike.exception.PostLikeException; +import com.example.sharemind.postLike.repository.PostLikeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class PostLikeServiceImpl implements PostLikeService { + + private final CustomerService customerService; + private final PostService postService; + private final PostLikeRepository postLikeRepository; + + @Override + public void createPostLike(Long postId, Long customerId) { + Post post = postService.getPostByPostId(postId); + Customer customer = customerService.getCustomerByCustomerId(customerId); + + if (postLikeRepository.existsByPostAndCustomer(post, customer)) { + PostLike postLike = postLikeRepository.findByPostAndCustomer(post, customer) + .orElseThrow( + () -> new PostLikeException(PostLikeErrorCode.POST_LIKE_NOT_FOUND)); + + postLike.updateIsActivatedTrue(); + } else { + PostLike postLike = PostLike.builder() + .post(post) + .customer(customer) + .build(); + + postLikeRepository.save(postLike); + } + } +} diff --git a/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java new file mode 100644 index 00000000..0648c42f --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java @@ -0,0 +1,16 @@ +package com.example.sharemind.postLike.repository; + +import com.example.sharemind.customer.domain.Customer; +import com.example.sharemind.post.domain.Post; +import com.example.sharemind.postLike.domain.PostLike; +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface PostLikeRepository extends JpaRepository { + + Boolean existsByPostAndCustomer(Post post, Customer customer); + + Optional findByPostAndCustomer(Post post, Customer customer); +} From 963c19de94bb4105a882f7f5beaf3894fd908e96 Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:02:07 +0900 Subject: [PATCH 03/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=BB=A4=EC=8A=A4=ED=85=80=20=EC=98=88?= =?UTF-8?q?=EC=99=B8=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/exception/CustomExceptionHandler.java | 8 ++++++++ .../postLike/exception/PostLikeErrorCode.java | 16 ++++++++++++++++ .../postLike/exception/PostLikeException.java | 14 ++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/main/java/com/example/sharemind/postLike/exception/PostLikeErrorCode.java create mode 100644 src/main/java/com/example/sharemind/postLike/exception/PostLikeException.java diff --git a/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java b/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java index da0f986c..38c31f35 100644 --- a/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java +++ b/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java @@ -12,6 +12,7 @@ import com.example.sharemind.letterMessage.exception.LetterMessageException; import com.example.sharemind.payment.exception.PaymentException; import com.example.sharemind.post.exception.PostException; +import com.example.sharemind.postLike.exception.PostLikeException; import com.example.sharemind.review.exception.ReviewException; import com.example.sharemind.wishList.exception.WishListException; import jakarta.validation.ConstraintViolationException; @@ -124,6 +125,13 @@ public ResponseEntity catchPostException(PostException .body(CustomExceptionResponse.of(e.getErrorCode().name(), e.getMessage())); } + @ExceptionHandler(PostLikeException.class) + public ResponseEntity catchPostLikeException(PostLikeException e) { + log.error(e.getMessage(), e); + return ResponseEntity.status(e.getErrorCode().getHttpStatus()) + .body(CustomExceptionResponse.of(e.getErrorCode().name(), e.getMessage())); + } + @ExceptionHandler(GlobalException.class) public ResponseEntity catchGlobalException(GlobalException e) { log.error(e.getMessage(), e); diff --git a/src/main/java/com/example/sharemind/postLike/exception/PostLikeErrorCode.java b/src/main/java/com/example/sharemind/postLike/exception/PostLikeErrorCode.java new file mode 100644 index 00000000..dc96a236 --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/exception/PostLikeErrorCode.java @@ -0,0 +1,16 @@ +package com.example.sharemind.postLike.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum PostLikeErrorCode { + + POST_LIKE_NOT_FOUND(HttpStatus.NOT_FOUND, "일대다 상담 질문 좋아요 정보가 존재하지 않습니다."), + POST_ALREADY_LIKED(HttpStatus.BAD_REQUEST, "이미 좋아요를 누른 일대다 상담 질문입니다."); + + private final HttpStatus httpStatus; + private final String message; +} diff --git a/src/main/java/com/example/sharemind/postLike/exception/PostLikeException.java b/src/main/java/com/example/sharemind/postLike/exception/PostLikeException.java new file mode 100644 index 00000000..4d2165c7 --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/exception/PostLikeException.java @@ -0,0 +1,14 @@ +package com.example.sharemind.postLike.exception; + +import lombok.Getter; + +@Getter +public class PostLikeException extends RuntimeException { + + private final PostLikeErrorCode errorCode; + + public PostLikeException(PostLikeErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} From 4ae2a7bb2efc2c192436f6c7c7d13d3c4f28c8f8 Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:02:35 +0900 Subject: [PATCH 04/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=83=9D=EC=84=B1=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/PostLikeController.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java diff --git a/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java b/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java new file mode 100644 index 00000000..382ab102 --- /dev/null +++ b/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java @@ -0,0 +1,53 @@ +package com.example.sharemind.postLike.presentation; + +import com.example.sharemind.global.exception.CustomExceptionResponse; +import com.example.sharemind.global.jwt.CustomUserDetails; +import com.example.sharemind.postLike.application.PostLikeService; +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.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "PostLike Controller", description = "일대다 상담 질문 좋아요 컨트롤러") +@RestController +@RequestMapping("/api/v1/postLikes") +@RequiredArgsConstructor +public class PostLikeController { + + private final PostLikeService postLikeService; + + @Operation(summary = "일대다 상담 질문 좋아요 생성", description = "일대다 상담 질문 좋아요 생성") + @ApiResponses({ + @ApiResponse(responseCode = "201", description = "생성 성공"), + @ApiResponse(responseCode = "400", + description = "이미 좋아요를 누른 질문", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ), + @ApiResponse(responseCode = "404", description = "1. 존재하지 않는 질문\n 2. 존재하지 않는 회원", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ) + }) + @Parameters({ + @Parameter(name = "postId", description = "일대다 질문 아이디") + }) + @PostMapping("/{postId}") + public ResponseEntity createPostLike(@PathVariable Long postId, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + postLikeService.createPostLike(postId, customUserDetails.getCustomer().getCustomerId()); + return ResponseEntity.status(HttpStatus.CREATED).build(); + } +} From 5647295250f607cbfb72f05b3d9749b0a8733a7e Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:06:59 +0900 Subject: [PATCH 05/22] =?UTF-8?q?#152=20feat:=20Post=20=EC=97=94=ED=8B=B0?= =?UTF-8?q?=ED=8B=B0=EC=9D=98=20totalLike=20=ED=95=84=EB=93=9C=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/example/sharemind/post/domain/Post.java | 8 ++++++++ .../com/example/sharemind/postLike/domain/PostLike.java | 3 +++ 2 files changed, 11 insertions(+) diff --git a/src/main/java/com/example/sharemind/post/domain/Post.java b/src/main/java/com/example/sharemind/post/domain/Post.java index b765fe9a..149742d0 100644 --- a/src/main/java/com/example/sharemind/post/domain/Post.java +++ b/src/main/java/com/example/sharemind/post/domain/Post.java @@ -99,6 +99,14 @@ public void updatePostStatus(PostStatus postStatus) { } } + public void increaseTotalLike() { + this.totalLike++; + } + + public void decreaseTotalLike() { + this.totalLike--; + } + public void updatePost(ConsultCategory consultCategory, String title, String content, Boolean isCompleted, Customer customer) { checkUpdatability(); diff --git a/src/main/java/com/example/sharemind/postLike/domain/PostLike.java b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java index d0df165d..d75b7379 100644 --- a/src/main/java/com/example/sharemind/postLike/domain/PostLike.java +++ b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java @@ -40,12 +40,15 @@ public class PostLike extends BaseEntity { public PostLike(Customer customer, Post post) { this.customer = customer; this.post = post; + + this.post.increaseTotalLike(); } public void updateIsActivatedTrue() { checkIsActivatedFalse(); super.updateIsActivatedTrue(); + this.post.increaseTotalLike(); } private void checkIsActivatedFalse() { From 93d1a0630ac15977e5f552969ed10c289d2f994c Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:13:23 +0900 Subject: [PATCH 06/22] =?UTF-8?q?#152=20feat:=20PostLike=EC=97=90=20?= =?UTF-8?q?=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/sharemind/postLike/domain/PostLike.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/com/example/sharemind/postLike/domain/PostLike.java b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java index d75b7379..66772601 100644 --- a/src/main/java/com/example/sharemind/postLike/domain/PostLike.java +++ b/src/main/java/com/example/sharemind/postLike/domain/PostLike.java @@ -51,6 +51,12 @@ public void updateIsActivatedTrue() { this.post.increaseTotalLike(); } + public void updateIsActivatedFalse() { + super.updateIsActivatedFalse(); + + this.post.decreaseTotalLike(); + } + private void checkIsActivatedFalse() { if (this.isActivated()) { throw new PostLikeException(PostLikeErrorCode.POST_ALREADY_LIKED); From 5c83547d83b6a9014b33c7edf1bca671c1976f0e Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:17:24 +0900 Subject: [PATCH 07/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../postLike/application/PostLikeService.java | 2 ++ .../postLike/application/PostLikeServiceImpl.java | 14 ++++++++++++++ .../postLike/repository/PostLikeRepository.java | 2 ++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java b/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java index 4c6a0b65..cb82d8bb 100644 --- a/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java +++ b/src/main/java/com/example/sharemind/postLike/application/PostLikeService.java @@ -3,4 +3,6 @@ public interface PostLikeService { void createPostLike(Long postId, Long customerId); + + void deletePostLike(Long postId, Long customerId); } diff --git a/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java b/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java index 88f9d1d5..6f555159 100644 --- a/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java +++ b/src/main/java/com/example/sharemind/postLike/application/PostLikeServiceImpl.java @@ -21,6 +21,7 @@ public class PostLikeServiceImpl implements PostLikeService { private final PostService postService; private final PostLikeRepository postLikeRepository; + @Transactional @Override public void createPostLike(Long postId, Long customerId) { Post post = postService.getPostByPostId(postId); @@ -41,4 +42,17 @@ public void createPostLike(Long postId, Long customerId) { postLikeRepository.save(postLike); } } + + @Transactional + @Override + public void deletePostLike(Long postId, Long customerId) { + Post post = postService.getPostByPostId(postId); + Customer customer = customerService.getCustomerByCustomerId(customerId); + + PostLike postLike = postLikeRepository.findByPostAndCustomerAndIsActivatedIsTrue(post, + customer) + .orElseThrow(() -> new PostLikeException(PostLikeErrorCode.POST_LIKE_NOT_FOUND)); + + postLike.updateIsActivatedFalse(); + } } diff --git a/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java index 0648c42f..01f5cc46 100644 --- a/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java +++ b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java @@ -13,4 +13,6 @@ public interface PostLikeRepository extends JpaRepository { Boolean existsByPostAndCustomer(Post post, Customer customer); Optional findByPostAndCustomer(Post post, Customer customer); + + Optional findByPostAndCustomerAndIsActivatedIsTrue(Post post, Customer customer); } From c11432aff5c894dc791f9281041b0f978fb00256 Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:17:38 +0900 Subject: [PATCH 08/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/PostLikeController.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java b/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java index 382ab102..8e67e856 100644 --- a/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java +++ b/src/main/java/com/example/sharemind/postLike/presentation/PostLikeController.java @@ -15,6 +15,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -50,4 +51,25 @@ public ResponseEntity createPostLike(@PathVariable Long postId, postLikeService.createPostLike(postId, customUserDetails.getCustomer().getCustomerId()); return ResponseEntity.status(HttpStatus.CREATED).build(); } + + @Operation(summary = "일대다 상담 질문 좋아요 취소", description = "일대다 상담 질문 좋아요 취소") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "취소 성공"), + @ApiResponse(responseCode = "404", description = """ + 1. 존재하지 않는 질문 + 2. 존재하지 않는 회원 + 3. 존재하지 않는 좋아요""", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ) + }) + @Parameters({ + @Parameter(name = "postId", description = "일대다 질문 아이디") + }) + @DeleteMapping("/{postId}") + public ResponseEntity deletePostLike(@PathVariable Long postId, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + postLikeService.deletePostLike(postId, customUserDetails.getCustomer().getCustomerId()); + return ResponseEntity.ok().build(); + } } From e73e68ed2cc3a1f583423048179f8e33f14bb7dd Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:46:51 +0900 Subject: [PATCH 09/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EB=8B=A8?= =?UTF-8?q?=EA=B1=B4/=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20re?= =?UTF-8?q?sponse=20dto=EC=97=90=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=97=AC?= =?UTF-8?q?=EB=B6=80=20=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/dto/response/PostGetListResponse.java | 11 +++++++++-- .../sharemind/post/dto/response/PostGetResponse.java | 9 +++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/example/sharemind/post/dto/response/PostGetListResponse.java b/src/main/java/com/example/sharemind/post/dto/response/PostGetListResponse.java index 620c21d2..6568f59f 100644 --- a/src/main/java/com/example/sharemind/post/dto/response/PostGetListResponse.java +++ b/src/main/java/com/example/sharemind/post/dto/response/PostGetListResponse.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonFormat; import io.swagger.v3.oas.annotations.media.Schema; import java.time.LocalDateTime; + import lombok.Builder; import lombok.Getter; @@ -23,6 +24,9 @@ public class PostGetListResponse { @Schema(description = "공개/비공개 여부") private final Boolean isPublic; + @Schema(description = "조회한 회원의 좋아요 여부") + private final Boolean isLiked; + @Schema(description = "좋아요 수") private final Long totalLike; @@ -41,12 +45,13 @@ public class PostGetListResponse { @Builder public PostGetListResponse(Long postId, String title, String content, Boolean isPublic, - Long totalLike, Long totalScrap, Long totalComment, String updatedAt, + Boolean isLiked, Long totalLike, Long totalScrap, Long totalComment, String updatedAt, LocalDateTime finishedAt) { this.postId = postId; this.title = title; this.content = content; this.isPublic = isPublic; + this.isLiked = isLiked; this.totalLike = totalLike; this.totalScrap = totalScrap; this.totalComment = totalComment; @@ -54,12 +59,13 @@ public PostGetListResponse(Long postId, String title, String content, Boolean is this.finishedAt = finishedAt; } - public static PostGetListResponse of(Post post) { + public static PostGetListResponse of(Post post, Boolean isLiked) { return PostGetListResponse.builder() .postId(post.getPostId()) .title(post.getTitle()) .content(post.getContent()) .isPublic(post.getIsPublic()) + .isLiked(isLiked) .totalLike(post.getTotalLike()) .totalScrap(post.getTotalScrap()) .totalComment(post.getTotalComment()) @@ -74,6 +80,7 @@ public static PostGetListResponse ofIsNotCompleted(Post post) { .title(null) .content(null) .isPublic(post.getIsPublic()) + .isLiked(false) .totalLike(post.getTotalLike()) .totalScrap(post.getTotalScrap()) .totalComment(post.getTotalComment()) diff --git a/src/main/java/com/example/sharemind/post/dto/response/PostGetResponse.java b/src/main/java/com/example/sharemind/post/dto/response/PostGetResponse.java index 0adb1668..e4438f5e 100644 --- a/src/main/java/com/example/sharemind/post/dto/response/PostGetResponse.java +++ b/src/main/java/com/example/sharemind/post/dto/response/PostGetResponse.java @@ -24,6 +24,9 @@ public class PostGetResponse { @Schema(description = "공개/비공개 여부") private final Boolean isPublic; + @Schema(description = "조회한 회원의 좋아요 여부") + private final Boolean isLiked; + @Schema(description = "좋아요 수") private final Long totalLike; @@ -35,24 +38,26 @@ public class PostGetResponse { @Builder public PostGetResponse(Long postId, String consultCategory, String title, String content, - Boolean isPublic, Long totalLike, Long totalScrap, String updatedAt) { + Boolean isPublic, Boolean isLiked, Long totalLike, Long totalScrap, String updatedAt) { this.postId = postId; this.consultCategory = consultCategory; this.title = title; this.content = content; this.isPublic = isPublic; + this.isLiked = isLiked; this.totalLike = totalLike; this.totalScrap = totalScrap; this.updatedAt = updatedAt; } - public static PostGetResponse of(Post post) { + public static PostGetResponse of(Post post, Boolean isLiked) { return PostGetResponse.builder() .postId(post.getPostId()) .consultCategory(post.getConsultCategory().getDisplayName()) .title(post.getTitle()) .content(post.getContent()) .isPublic(post.getIsPublic()) + .isLiked(isLiked) .totalLike(post.getTotalLike()) .totalScrap(post.getTotalScrap()) .updatedAt(TimeUtil.getUpdatedAt(post.getUpdatedAt())) From 0ba9a37a30cf988ae9982d02d63cd37c8599769f Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:47:36 +0900 Subject: [PATCH 10/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EB=8B=A8?= =?UTF-8?q?=EA=B1=B4/=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EC=8B=9C=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=97=AC=EB=B6=80=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=ED=95=98=EB=8A=94=20=EB=B9=84=EC=A6=88?= =?UTF-8?q?=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/application/PostService.java | 3 +- .../post/application/PostServiceImpl.java | 36 ++++++++++++++----- .../repository/PostLikeRepository.java | 2 ++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/example/sharemind/post/application/PostService.java b/src/main/java/com/example/sharemind/post/application/PostService.java index 430aa901..1206011c 100644 --- a/src/main/java/com/example/sharemind/post/application/PostService.java +++ b/src/main/java/com/example/sharemind/post/application/PostService.java @@ -26,7 +26,8 @@ public interface PostService { List getPostsByCustomer(Boolean filter, Long postId, Long customerId); - List getPublicPostsByCustomer(Long postId, LocalDateTime finishedAt); + List getPublicPostsByCustomer(Long postId, LocalDateTime finishedAt, + Long customerId); List getPopularityPosts(); diff --git a/src/main/java/com/example/sharemind/post/application/PostServiceImpl.java b/src/main/java/com/example/sharemind/post/application/PostServiceImpl.java index c935cbf9..f5eb6020 100644 --- a/src/main/java/com/example/sharemind/post/application/PostServiceImpl.java +++ b/src/main/java/com/example/sharemind/post/application/PostServiceImpl.java @@ -17,6 +17,7 @@ import com.example.sharemind.post.exception.PostErrorCode; import com.example.sharemind.post.exception.PostException; import com.example.sharemind.post.repository.PostRepository; +import com.example.sharemind.postLike.repository.PostLikeRepository; import java.time.LocalDate; import java.time.LocalDateTime; import java.util.List; @@ -31,11 +32,13 @@ public class PostServiceImpl implements PostService { private static final int POST_CUSTOMER_PAGE_SIZE = 4; private static final int POST_POPULARITY_SIZE = 3; + private static final Boolean POST_IS_NOT_LIKED = false; private final CustomerService customerService; private final CounselorService counselorService; private final PostRepository postRepository; private final CommentRepository commentRepository; + private final PostLikeRepository postLikeRepository; @Transactional @Override @@ -84,11 +87,15 @@ public PostGetResponse getPost(Long postId, Long customerId) { Post post = getPostByPostId(postId); if (customerId != 0) { - customerService.getCustomerByCustomerId(customerId); + Customer customer = customerService.getCustomerByCustomerId(customerId); + + post.checkReadAuthority(customerId); + return PostGetResponse.of(post, + postLikeRepository.existsByPostAndCustomerAndIsActivatedIsTrue(post, customer)); } - post.checkReadAuthority(customerId); - return PostGetResponse.of(post); + post.checkReadAuthority(customerId); + return PostGetResponse.of(post, POST_IS_NOT_LIKED); } @Override @@ -99,16 +106,29 @@ public List getPostsByCustomer(Boolean filter, Long postId, return postRepository.findAllByCustomerAndIsActivatedIsTrue(customer, filter, postId, POST_CUSTOMER_PAGE_SIZE).stream() .map(post -> (post.getIsCompleted() != null && !post.getIsCompleted()) - ? PostGetListResponse.ofIsNotCompleted(post) : PostGetListResponse.of(post)) + ? PostGetListResponse.ofIsNotCompleted(post) : PostGetListResponse.of(post, + postLikeRepository.existsByPostAndCustomerAndIsActivatedIsTrue(post, + customer))) .toList(); } @Override - public List getPublicPostsByCustomer(Long postId, - LocalDateTime finishedAt) { + public List getPublicPostsByCustomer(Long postId, LocalDateTime finishedAt, + Long customerId) { + if (customerId != 0) { + Customer customer = customerService.getCustomerByCustomerId(customerId); + + return postRepository.findAllByIsPublicAndIsActivatedIsTrue(postId, finishedAt, + POST_CUSTOMER_PAGE_SIZE).stream() + .map(post -> PostGetListResponse.of(post, + postLikeRepository.existsByPostAndCustomerAndIsActivatedIsTrue(post, + customer))) + .toList(); + } + return postRepository.findAllByIsPublicAndIsActivatedIsTrue(postId, finishedAt, POST_CUSTOMER_PAGE_SIZE).stream() - .map(PostGetListResponse::of) + .map(post -> PostGetListResponse.of(post, POST_IS_NOT_LIKED)) .toList(); } @@ -129,7 +149,7 @@ public List getRandomPosts() { public PostGetResponse getCounselorPostContent(Long postId, Long customerId) { Post post = checkAndGetCounselorPost(postId, customerId); - return PostGetResponse.of(post); + return PostGetResponse.of(post, POST_IS_NOT_LIKED); } @Override diff --git a/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java index 01f5cc46..53776004 100644 --- a/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java +++ b/src/main/java/com/example/sharemind/postLike/repository/PostLikeRepository.java @@ -12,6 +12,8 @@ public interface PostLikeRepository extends JpaRepository { Boolean existsByPostAndCustomer(Post post, Customer customer); + Boolean existsByPostAndCustomerAndIsActivatedIsTrue(Post post, Customer customer); + Optional findByPostAndCustomer(Post post, Customer customer); Optional findByPostAndCustomerAndIsActivatedIsTrue(Post post, Customer customer); From 4d0c2e5a94d75fdd0cb071ca765cc77e7ee574f5 Mon Sep 17 00:00:00 2001 From: letskuku Date: Sun, 24 Mar 2024 22:48:44 +0900 Subject: [PATCH 11/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EC=A7=88=EB=AC=B8=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=82=AC=EC=9A=A9=EC=9E=90=20=EC=95=84?= =?UTF-8?q?=EC=9D=B4=EB=94=94=20=EB=B0=9B=EC=95=84=EC=98=A4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sharemind/post/presentation/PostController.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/example/sharemind/post/presentation/PostController.java b/src/main/java/com/example/sharemind/post/presentation/PostController.java index 6aac1180..623e5d60 100644 --- a/src/main/java/com/example/sharemind/post/presentation/PostController.java +++ b/src/main/java/com/example/sharemind/post/presentation/PostController.java @@ -149,6 +149,7 @@ public ResponseEntity> getPostsByCustomer( @Operation(summary = "구매자 사이드 공개상담 탭 일대다 상담 리스트 기본순 조회", description = """ - 구매자 사이드의 공개상담 탭에서 답변 완료된 일대다 상담 질문 리스트 기본순 조회 + - 로그인한 사용자일 경우 헤더에 accessToken을 넣어주세요 - 주소 형식: /api/v1/posts/customers/public?postId=0&finishedAt=2024-03-22T00:47:59""") @ApiResponses({ @ApiResponse(responseCode = "200", description = "조회 성공") @@ -166,8 +167,10 @@ public ResponseEntity> getPostsByCustomer( @GetMapping("/customers/public") public ResponseEntity> getPublicPostsByCustomer( @RequestParam Long postId, - @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime finishedAt) { - return ResponseEntity.ok(postService.getPublicPostsByCustomer(postId, finishedAt)); + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss") LocalDateTime finishedAt, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + return ResponseEntity.ok(postService.getPublicPostsByCustomer(postId, finishedAt, + customUserDetails == null ? 0 : customUserDetails.getCustomer().getCustomerId())); } @Operation(summary = "구매자 사이드 공개상담 탭 일대다 상담 리스트 인기순 조회", description = """ From fef9641d38790b41ab4054a313b03742abc84d5b Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:49:29 +0900 Subject: [PATCH 12/22] =?UTF-8?q?#152=20feat:=20Comment=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=EC=9D=98=20=EC=A2=8B=EC=95=84=EC=9A=94=20?= =?UTF-8?q?=EC=88=98=20=ED=95=84=EB=93=9C=20=EC=88=98=EC=A0=95=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/example/sharemind/comment/domain/Comment.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/example/sharemind/comment/domain/Comment.java b/src/main/java/com/example/sharemind/comment/domain/Comment.java index 4622362b..d81ffcb7 100644 --- a/src/main/java/com/example/sharemind/comment/domain/Comment.java +++ b/src/main/java/com/example/sharemind/comment/domain/Comment.java @@ -45,4 +45,12 @@ public Comment(Post post, Counselor counselor, String content) { isChosen = false; totalLike = 0L; } + + public void increaseTotalLike() { + this.totalLike++; + } + + public void decreaseTotalLike() { + this.totalLike--; + } } From 85f4b275162063a480efb61753e1c28421910cb9 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:50:02 +0900 Subject: [PATCH 13/22] =?UTF-8?q?#152=20feat:=20CommentLike=20=EC=97=94?= =?UTF-8?q?=ED=8B=B0=ED=8B=B0=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commentLike/domain/CommentLike.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 src/main/java/com/example/sharemind/commentLike/domain/CommentLike.java diff --git a/src/main/java/com/example/sharemind/commentLike/domain/CommentLike.java b/src/main/java/com/example/sharemind/commentLike/domain/CommentLike.java new file mode 100644 index 00000000..3254b991 --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/domain/CommentLike.java @@ -0,0 +1,65 @@ +package com.example.sharemind.commentLike.domain; + +import com.example.sharemind.comment.domain.Comment; +import com.example.sharemind.commentLike.exception.CommentLikeErrorCode; +import com.example.sharemind.commentLike.exception.CommentLikeException; +import com.example.sharemind.customer.domain.Customer; +import com.example.sharemind.global.common.BaseEntity; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import lombok.AccessLevel; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +@Entity +public class CommentLike extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "comment_like_id") + private Long commentLikeId; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "customer_id") + private Customer customer; + + @ManyToOne(fetch = FetchType.LAZY, optional = false) + @JoinColumn(name = "comment_id") + private Comment comment; + + @Builder + public CommentLike(Customer customer, Comment comment) { + this.customer = customer; + this.comment = comment; + + this.comment.increaseTotalLike(); + } + + public void updateIsActivatedTrue() { + checkIsActivatedFalse(); + + super.updateIsActivatedTrue(); + this.comment.increaseTotalLike(); + } + + public void updateIsActivatedFalse() { + super.updateIsActivatedFalse(); + + this.comment.decreaseTotalLike(); + } + + private void checkIsActivatedFalse() { + if (this.isActivated()) { + throw new CommentLikeException(CommentLikeErrorCode.COMMENT_ALREADY_LIKED); + } + } +} From 31c1483139a55498e99a5f84b0e425a186aa26e4 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:51:00 +0900 Subject: [PATCH 14/22] =?UTF-8?q?#152=20feat:=20=EC=95=84=EC=9D=B4?= =?UTF-8?q?=EB=94=94=EB=A1=9C=20Comment=20=EC=A1=B0=ED=9A=8C=ED=95=98?= =?UTF-8?q?=EB=8A=94=20=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/application/CommentService.java | 3 +++ .../application/CommentServiceImpl.java | 23 ++++++++++++++----- .../comment/repository/CommentRepository.java | 4 ++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/example/sharemind/comment/application/CommentService.java b/src/main/java/com/example/sharemind/comment/application/CommentService.java index 5f24cbe1..36ff22e4 100644 --- a/src/main/java/com/example/sharemind/comment/application/CommentService.java +++ b/src/main/java/com/example/sharemind/comment/application/CommentService.java @@ -1,5 +1,6 @@ package com.example.sharemind.comment.application; +import com.example.sharemind.comment.domain.Comment; import com.example.sharemind.comment.dto.request.CommentCreateRequest; import com.example.sharemind.comment.dto.response.CommentGetResponse; @@ -9,4 +10,6 @@ public interface CommentService { List getCommentsByPost(Long postId, Long customerId); void createComment(CommentCreateRequest commentCreateRequest, Long customerId); + + Comment getCommentByCommentId(Long commentId); } diff --git a/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java b/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java index d2eeb7a3..7070c603 100644 --- a/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java +++ b/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java @@ -22,12 +22,12 @@ @RequiredArgsConstructor public class CommentServiceImpl implements CommentService { + private static final Integer MAX_COMMENTS = 5; + private final PostService postService; private final CounselorService counselorService; private final CommentRepository commentRepository; - private static final Integer MAX_COMMENTS = 5; - @Override public List getCommentsByPost(Long postId, Long customerId) { Post post = postService.checkAndGetCounselorPost(postId, customerId); @@ -41,16 +41,27 @@ public List getCommentsByPost(Long postId, Long customerId) @Transactional @Override public void createComment(CommentCreateRequest commentCreateRequest, Long customerId) { - Post post = postService.checkAndGetCounselorPost(commentCreateRequest.getPostId(), customerId); + Post post = postService.checkAndGetCounselorPost(commentCreateRequest.getPostId(), + customerId); Counselor counselor = counselorService.getCounselorByCustomerId(customerId); - if (commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post, counselor) != null) - throw new CommentException(CommentErrorCode.COMMENT_ALREADY_REGISTERED, counselor.getNickname()); + if (commentRepository.findByPostAndCounselorAndIsActivatedIsTrue(post, counselor) != null) { + throw new CommentException(CommentErrorCode.COMMENT_ALREADY_REGISTERED, + counselor.getNickname()); + } commentRepository.save(commentCreateRequest.toEntity(post, counselor)); List comments = commentRepository.findByPostAndIsActivatedIsTrue(post); - if (comments.size() == MAX_COMMENTS) + if (comments.size() == MAX_COMMENTS) { post.updatePostStatus(PostStatus.COMPLETED); + } + } + + @Override + public Comment getCommentByCommentId(Long commentId) { + return commentRepository.findByCommentIdAndIsActivatedIsTrue(commentId).orElseThrow( + () -> new CommentException(CommentErrorCode.COMMENT_NOT_FOUND, + commentId.toString())); } } diff --git a/src/main/java/com/example/sharemind/comment/repository/CommentRepository.java b/src/main/java/com/example/sharemind/comment/repository/CommentRepository.java index 5ff20f9c..8f63acfe 100644 --- a/src/main/java/com/example/sharemind/comment/repository/CommentRepository.java +++ b/src/main/java/com/example/sharemind/comment/repository/CommentRepository.java @@ -3,6 +3,7 @@ import com.example.sharemind.comment.domain.Comment; import com.example.sharemind.counselor.domain.Counselor; import com.example.sharemind.post.domain.Post; +import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -10,7 +11,10 @@ @Repository public interface CommentRepository extends JpaRepository { + List findByPostAndIsActivatedIsTrue(Post post); Comment findByPostAndCounselorAndIsActivatedIsTrue(Post post, Counselor counselor); + + Optional findByCommentIdAndIsActivatedIsTrue(Long commentId); } From f96184f841a8b2784d8fe16e668271cc5b9ed24a Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:51:31 +0900 Subject: [PATCH 15/22] =?UTF-8?q?#152=20feat:=20Comment=20=EC=BB=A4?= =?UTF-8?q?=EC=8A=A4=ED=85=80=20=EC=98=88=EC=99=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/sharemind/comment/exception/CommentErrorCode.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/example/sharemind/comment/exception/CommentErrorCode.java b/src/main/java/com/example/sharemind/comment/exception/CommentErrorCode.java index 5c3fbb7e..bea0950e 100644 --- a/src/main/java/com/example/sharemind/comment/exception/CommentErrorCode.java +++ b/src/main/java/com/example/sharemind/comment/exception/CommentErrorCode.java @@ -6,6 +6,7 @@ @Getter public enum CommentErrorCode { + COMMENT_NOT_FOUND(HttpStatus.NOT_FOUND, "일대다 상담 댓글이 존재하지 않습니다."), COMMENT_ALREADY_REGISTERED(HttpStatus.BAD_REQUEST, "상담사 당 댓글은 한 번만 작성할 수 있습니다."); private final HttpStatus httpStatus; From 13f5cc8edd97f6cac1101a5fa35643f418c35618 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:53:33 +0900 Subject: [PATCH 16/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=83=9D=EC=84=B1=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/CommentLikeService.java | 6 +++ .../application/CommentLikeServiceImpl.java | 45 +++++++++++++++++++ .../repository/CommentLikeRepository.java | 16 +++++++ 3 files changed, 67 insertions(+) create mode 100644 src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java create mode 100644 src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java create mode 100644 src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java diff --git a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java new file mode 100644 index 00000000..215d77a7 --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java @@ -0,0 +1,6 @@ +package com.example.sharemind.commentLike.application; + +public interface CommentLikeService { + + void createCommentLike(Long commentId, Long customerId); +} diff --git a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java new file mode 100644 index 00000000..378a6b5f --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java @@ -0,0 +1,45 @@ +package com.example.sharemind.commentLike.application; + +import com.example.sharemind.comment.application.CommentService; +import com.example.sharemind.comment.domain.Comment; +import com.example.sharemind.commentLike.domain.CommentLike; +import com.example.sharemind.commentLike.exception.CommentLikeErrorCode; +import com.example.sharemind.commentLike.exception.CommentLikeException; +import com.example.sharemind.commentLike.repository.CommentLikeRepository; +import com.example.sharemind.customer.application.CustomerService; +import com.example.sharemind.customer.domain.Customer; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class CommentLikeServiceImpl implements CommentLikeService { + + private final CustomerService customerService; + private final CommentService commentService; + private final CommentLikeRepository commentLikeRepository; + + @Transactional + @Override + public void createCommentLike(Long commentId, Long customerId) { + Comment comment = commentService.getCommentByCommentId(commentId); + Customer customer = customerService.getCustomerByCustomerId(customerId); + + if (commentLikeRepository.existsByCommentAndCustomer(comment, customer)) { + CommentLike commentLike = commentLikeRepository.findByCommentAndCustomer(comment, + customer).orElseThrow( + () -> new CommentLikeException(CommentLikeErrorCode.COMMENT_LIKE_NOT_FOUND)); + + commentLike.updateIsActivatedTrue(); + } else { + CommentLike commentLike = CommentLike.builder() + .comment(comment) + .customer(customer) + .build(); + + commentLikeRepository.save(commentLike); + } + } +} diff --git a/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java new file mode 100644 index 00000000..c5156130 --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java @@ -0,0 +1,16 @@ +package com.example.sharemind.commentLike.repository; + +import com.example.sharemind.comment.domain.Comment; +import com.example.sharemind.commentLike.domain.CommentLike; +import com.example.sharemind.customer.domain.Customer; +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface CommentLikeRepository extends JpaRepository { + + Boolean existsByCommentAndCustomer(Comment comment, Customer customer); + + Optional findByCommentAndCustomer(Comment comment, Customer customer); +} From e26e2cb43459de1727586bed1a62a3b1f07a251d Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:54:55 +0900 Subject: [PATCH 17/22] =?UTF-8?q?#152=20feat:=20CommentLike=20=EC=BB=A4?= =?UTF-8?q?=EC=8A=A4=ED=85=80=20=EC=98=88=EC=99=B8=20=EA=B5=AC=ED=98=84=20?= =?UTF-8?q?=EB=B0=8F=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../exception/CommentLikeErrorCode.java | 16 ++++++++++++++++ .../exception/CommentLikeException.java | 14 ++++++++++++++ .../global/exception/CustomExceptionHandler.java | 8 ++++++++ 3 files changed, 38 insertions(+) create mode 100644 src/main/java/com/example/sharemind/commentLike/exception/CommentLikeErrorCode.java create mode 100644 src/main/java/com/example/sharemind/commentLike/exception/CommentLikeException.java diff --git a/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeErrorCode.java b/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeErrorCode.java new file mode 100644 index 00000000..e0f53f08 --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeErrorCode.java @@ -0,0 +1,16 @@ +package com.example.sharemind.commentLike.exception; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum CommentLikeErrorCode { + + COMMENT_LIKE_NOT_FOUND(HttpStatus.NOT_FOUND, "일대다 상담 댓글 좋아요 정보가 존재하지 않습니다."), + COMMENT_ALREADY_LIKED(HttpStatus.BAD_REQUEST, "이미 좋아요를 누른 일대다 상담 댓글입니다."); + + private final HttpStatus httpStatus; + private final String message; +} diff --git a/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeException.java b/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeException.java new file mode 100644 index 00000000..8070558d --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/exception/CommentLikeException.java @@ -0,0 +1,14 @@ +package com.example.sharemind.commentLike.exception; + +import lombok.Getter; + +@Getter +public class CommentLikeException extends RuntimeException { + + private final CommentLikeErrorCode errorCode; + + public CommentLikeException(CommentLikeErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} diff --git a/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java b/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java index 38c31f35..028b205a 100644 --- a/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java +++ b/src/main/java/com/example/sharemind/global/exception/CustomExceptionHandler.java @@ -4,6 +4,7 @@ import com.example.sharemind.chat.exception.ChatException; import com.example.sharemind.chatMessage.exception.ChatMessageException; import com.example.sharemind.comment.exception.CommentException; +import com.example.sharemind.commentLike.exception.CommentLikeException; import com.example.sharemind.consult.exception.ConsultException; import com.example.sharemind.counselor.exception.CounselorException; import com.example.sharemind.customer.exception.CustomerException; @@ -104,6 +105,13 @@ public ResponseEntity catchCommentException(CommentExce .body(CustomExceptionResponse.of(e.getErrorCode().name(), e.getMessage())); } + @ExceptionHandler(CommentLikeException.class) + public ResponseEntity catchCommentLikeException(CommentLikeException e) { + log.error(e.getMessage(), e); + return ResponseEntity.status(e.getErrorCode().getHttpStatus()) + .body(CustomExceptionResponse.of(e.getErrorCode().name(), e.getMessage())); + } + @ExceptionHandler(ReviewException.class) public ResponseEntity catchReviewException(ReviewException e) { log.error(e.getMessage(), e); From 0dd0ef7534255eac214a14c14769bb57b9d34611 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 00:55:16 +0900 Subject: [PATCH 18/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=83=9D=EC=84=B1=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/CommentLikeController.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java diff --git a/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java b/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java new file mode 100644 index 00000000..9db7278b --- /dev/null +++ b/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java @@ -0,0 +1,54 @@ +package com.example.sharemind.commentLike.presentation; + +import com.example.sharemind.commentLike.application.CommentLikeService; +import com.example.sharemind.global.exception.CustomExceptionResponse; +import com.example.sharemind.global.jwt.CustomUserDetails; +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.media.Content; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.responses.ApiResponse; +import io.swagger.v3.oas.annotations.responses.ApiResponses; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Tag(name = "CommentLike Controller", description = "일대다 상담 댓글 좋아요 컨트롤러") +@RestController +@RequestMapping("/api/v1/commentLikes") +@RequiredArgsConstructor +public class CommentLikeController { + + private final CommentLikeService commentLikeService; + + @Operation(summary = "일대다 상담 댓글 좋아요 생성", description = "일대다 상담 댓글 좋아요 생성") + @ApiResponses({ + @ApiResponse(responseCode = "201", description = "생성 성공"), + @ApiResponse(responseCode = "400", + description = "이미 좋아요를 누른 댓글", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ), + @ApiResponse(responseCode = "404", description = "1. 존재하지 않는 댓글\n 2. 존재하지 않는 회원", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ) + }) + @Parameters({ + @Parameter(name = "commentId", description = "일대다 상담 댓글 아이디") + }) + @PostMapping("/{commentId}") + public ResponseEntity createCommentLike(@PathVariable Long commentId, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + commentLikeService.createCommentLike(commentId, + customUserDetails.getCustomer().getCustomerId()); + return ResponseEntity.status(HttpStatus.CREATED).build(); + } +} From db653245c3b32c92831993d51fb6f02320ebfcd3 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 01:05:59 +0900 Subject: [PATCH 19/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=B9=84=EC=A6=88=EB=8B=88=EC=8A=A4=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../commentLike/application/CommentLikeService.java | 2 ++ .../application/CommentLikeServiceImpl.java | 13 +++++++++++++ .../repository/CommentLikeRepository.java | 3 +++ 3 files changed, 18 insertions(+) diff --git a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java index 215d77a7..6b3d6e5a 100644 --- a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java +++ b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java @@ -3,4 +3,6 @@ public interface CommentLikeService { void createCommentLike(Long commentId, Long customerId); + + void deleteCommentLike(Long commentId, Long customerId); } diff --git a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java index 378a6b5f..4e0043c9 100644 --- a/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java +++ b/src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java @@ -42,4 +42,17 @@ public void createCommentLike(Long commentId, Long customerId) { commentLikeRepository.save(commentLike); } } + + @Transactional + @Override + public void deleteCommentLike(Long commentId, Long customerId) { + Comment comment = commentService.getCommentByCommentId(commentId); + Customer customer = customerService.getCustomerByCustomerId(customerId); + + CommentLike commentLike = commentLikeRepository.findByCommentAndCustomerAndIsActivatedIsTrue( + comment, customer).orElseThrow( + () -> new CommentLikeException(CommentLikeErrorCode.COMMENT_LIKE_NOT_FOUND)); + + commentLike.updateIsActivatedFalse(); + } } diff --git a/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java index c5156130..ebecfff9 100644 --- a/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java +++ b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java @@ -13,4 +13,7 @@ public interface CommentLikeRepository extends JpaRepository Boolean existsByCommentAndCustomer(Comment comment, Customer customer); Optional findByCommentAndCustomer(Comment comment, Customer customer); + + Optional findByCommentAndCustomerAndIsActivatedIsTrue(Comment comment, + Customer customer); } From a91f40d1e6e637dda48fcb5f457d34e091c945d7 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 01:07:45 +0900 Subject: [PATCH 20/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=B7=A8=EC=86=8C=20api=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/CommentLikeController.java | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java b/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java index 9db7278b..be6da221 100644 --- a/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java +++ b/src/main/java/com/example/sharemind/commentLike/presentation/CommentLikeController.java @@ -15,6 +15,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; @@ -51,4 +52,26 @@ public ResponseEntity createCommentLike(@PathVariable Long commentId, customUserDetails.getCustomer().getCustomerId()); return ResponseEntity.status(HttpStatus.CREATED).build(); } + + @Operation(summary = "일대다 상담 댓글 좋아요 취소", description = "일대다 상담 댓글 좋아요 취소") + @ApiResponses({ + @ApiResponse(responseCode = "200", description = "취소 성공"), + @ApiResponse(responseCode = "404", description = """ + 1. 존재하지 않는 댓글 + 2. 존재하지 않는 회원 + 3. 존재하지 않는 좋아요""", + content = @Content(mediaType = "application/json", + schema = @Schema(implementation = CustomExceptionResponse.class)) + ) + }) + @Parameters({ + @Parameter(name = "commentId", description = "일대다 상담 댓글 아이디") + }) + @DeleteMapping("/{commentId}") + public ResponseEntity deleteCommentLike(@PathVariable Long commentId, + @AuthenticationPrincipal CustomUserDetails customUserDetails) { + commentLikeService.deleteCommentLike(commentId, + customUserDetails.getCustomer().getCustomerId()); + return ResponseEntity.ok().build(); + } } From b38a38c613c464204bd020d223f1a8fec5afd904 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 01:18:48 +0900 Subject: [PATCH 21/22] =?UTF-8?q?#152=20feat:=20CommentGetResponse?= =?UTF-8?q?=EC=97=90=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=97=AC=EB=B6=80=20?= =?UTF-8?q?=ED=95=84=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/dto/response/CommentGetResponse.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/sharemind/comment/dto/response/CommentGetResponse.java b/src/main/java/com/example/sharemind/comment/dto/response/CommentGetResponse.java index a44f34ec..e5f5bb78 100644 --- a/src/main/java/com/example/sharemind/comment/dto/response/CommentGetResponse.java +++ b/src/main/java/com/example/sharemind/comment/dto/response/CommentGetResponse.java @@ -15,28 +15,34 @@ public class CommentGetResponse { @Schema(description = "답변 내용") private final String content; + @Schema(description = "조회한 회원의 좋아요 여부") + private final Boolean isLiked; + @Schema(description = "좋아요 수") private final Long totalLike; @Schema(description = "마지막 업데이트 일시", example = "오전 11:10") private final String updatedAt; - @Schema(description = "채택 여부", example="true") + @Schema(description = "채택 여부", example = "true") private final Boolean isChosen; @Builder - public CommentGetResponse(String nickName, String content, Long totalLike, String updatedAt, Boolean isChosen) { + public CommentGetResponse(String nickName, String content, Boolean isLiked, Long totalLike, + String updatedAt, Boolean isChosen) { this.nickName = nickName; this.content = content; + this.isLiked = isLiked; this.totalLike = totalLike; this.updatedAt = updatedAt; this.isChosen = isChosen; } - public static CommentGetResponse of(Comment comment) { + public static CommentGetResponse of(Comment comment, Boolean isLiked) { return CommentGetResponse.builder() .nickName(comment.getCounselor().getNickname()) .content(comment.getContent()) + .isLiked(isLiked) .totalLike(comment.getTotalLike()) .updatedAt(TimeUtil.getUpdatedAt(comment.getUpdatedAt())) .isChosen(comment.getIsChosen()) From 9b9f0368ef9f79599aebe7afb4f53ed77489a105 Mon Sep 17 00:00:00 2001 From: letskuku Date: Mon, 25 Mar 2024 01:19:29 +0900 Subject: [PATCH 22/22] =?UTF-8?q?#152=20feat:=20=EC=9D=BC=EB=8C=80?= =?UTF-8?q?=EB=8B=A4=20=EC=83=81=EB=8B=B4=20=EB=8C=93=EA=B8=80=20=EC=A2=8B?= =?UTF-8?q?=EC=95=84=EC=9A=94=20=EC=97=AC=EB=B6=80=20=ED=99=95=EC=9D=B8?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../comment/application/CommentServiceImpl.java | 10 +++++++++- .../commentLike/repository/CommentLikeRepository.java | 2 ++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java b/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java index 7070c603..ea4347ba 100644 --- a/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java +++ b/src/main/java/com/example/sharemind/comment/application/CommentServiceImpl.java @@ -6,8 +6,11 @@ import com.example.sharemind.comment.exception.CommentErrorCode; import com.example.sharemind.comment.exception.CommentException; import com.example.sharemind.comment.repository.CommentRepository; +import com.example.sharemind.commentLike.repository.CommentLikeRepository; import com.example.sharemind.counselor.application.CounselorService; import com.example.sharemind.counselor.domain.Counselor; +import com.example.sharemind.customer.application.CustomerService; +import com.example.sharemind.customer.domain.Customer; import com.example.sharemind.post.application.PostService; import com.example.sharemind.post.content.PostStatus; import com.example.sharemind.post.domain.Post; @@ -26,15 +29,20 @@ public class CommentServiceImpl implements CommentService { private final PostService postService; private final CounselorService counselorService; + private final CustomerService customerService; private final CommentRepository commentRepository; + private final CommentLikeRepository commentLikeRepository; @Override public List getCommentsByPost(Long postId, Long customerId) { Post post = postService.checkAndGetCounselorPost(postId, customerId); + Customer customer = customerService.getCustomerByCustomerId(customerId); List comments = commentRepository.findByPostAndIsActivatedIsTrue(post); return comments.stream() - .map(CommentGetResponse::of) + .map(comment -> CommentGetResponse.of(comment, + commentLikeRepository.existsByCommentAndCustomerAndIsActivatedIsTrue( + comment, customer))) .toList(); } diff --git a/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java index ebecfff9..b4be3302 100644 --- a/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java +++ b/src/main/java/com/example/sharemind/commentLike/repository/CommentLikeRepository.java @@ -12,6 +12,8 @@ public interface CommentLikeRepository extends JpaRepository Boolean existsByCommentAndCustomer(Comment comment, Customer customer); + Boolean existsByCommentAndCustomerAndIsActivatedIsTrue(Comment comment, Customer customer); + Optional findByCommentAndCustomer(Comment comment, Customer customer); Optional findByCommentAndCustomerAndIsActivatedIsTrue(Comment comment,