-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #155 from sharemindteam/feature/152-post-like
feat: 일대다 상담 좋아요 기능 구현
- Loading branch information
Showing
27 changed files
with
641 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/com/example/sharemind/commentLike/application/CommentLikeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.example.sharemind.commentLike.application; | ||
|
||
public interface CommentLikeService { | ||
|
||
void createCommentLike(Long commentId, Long customerId); | ||
|
||
void deleteCommentLike(Long commentId, Long customerId); | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/com/example/sharemind/commentLike/application/CommentLikeServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
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); | ||
} | ||
} | ||
|
||
@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(); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/example/sharemind/commentLike/domain/CommentLike.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/example/sharemind/commentLike/exception/CommentLikeErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/sharemind/commentLike/exception/CommentLikeException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.