Skip to content

Commit

Permalink
fix : createComment
Browse files Browse the repository at this point in the history
  • Loading branch information
BuildTools committed Aug 20, 2024
1 parent afaa598 commit 3078c96
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public BaseResponse<PostCommentResponse> createComment(@PreAuthorize long userId
if (bindingResult.hasErrors()) {
throw new DebateException(INVALID_COMMENT_VALUE, getErrorMessages(bindingResult));
}
debateService.validateCreateComment(postCommentRequest, userId);
PostCommentResponse postCommentResponse = debateService.createComment(postCommentRequest, userId);
alarmService.createAlarmComment(postCommentResponse.getCommentId(),userId);

Expand Down
7 changes: 3 additions & 4 deletions src/main/java/store/itpick/backend/model/Comment.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package store.itpick.backend.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;

import java.sql.Timestamp;
import java.util.List;
Expand Down Expand Up @@ -36,10 +33,12 @@ public class Comment {

@ManyToOne
@JoinColumn(name = "debate_id", nullable = false)
@ToString.Exclude
private Debate debate;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@ToString.Exclude
private User user;

@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/store/itpick/backend/model/Debate.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Entity
@Table(name = "debate")
@EqualsAndHashCode(exclude = {"vote"})
@EqualsAndHashCode(exclude = {"vote", "comment"})
@Data
@NoArgsConstructor
@AllArgsConstructor
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/store/itpick/backend/service/DebateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,9 @@ public PostDebateResponse createDebate(PostDebateRequest postDebateRequest, long
public PostCommentResponse createComment(PostCommentRequest postCommentRequest, long userId) {

Optional<Debate> debateOptional = debateRepository.findById(postCommentRequest.getDebateId());
if (!debateOptional.isPresent()) {
throw new DebateException(DEBATE_NOT_FOUND);
}

Debate debate = debateOptional.get();

// 유저 아이디, debateId, comment 내용이 동일한 댓글이 있는지 확인
List<Comment> existingComment = commentRepository.findByUser_UserIdAndDebate_DebateIdAndComment(userId, postCommentRequest.getDebateId(), postCommentRequest.getComment());
if (!existingComment.isEmpty()) {
throw new DebateException(DUPLICATE_COMMENT);
}

Comment parentComment = null;
if (postCommentRequest.getParentCommentId() != null) {
Expand Down Expand Up @@ -279,6 +272,24 @@ public List<DebateByKeywordDTO> getRecentViewedDebate(long userId){

}


public void validateCreateComment(PostCommentRequest postCommentRequest, long userId) {

Optional<Debate> debateOptional = debateRepository.findById(postCommentRequest.getDebateId());
if (!debateOptional.isPresent()) {
throw new DebateException(DEBATE_NOT_FOUND);
}

// 유저 아이디, debateId, comment 내용이 동일한 댓글이 있는지 확인
List<Comment> existingComment = commentRepository.findByUser_UserIdAndDebate_DebateIdAndComment(userId, postCommentRequest.getDebateId(), postCommentRequest.getComment());
if (!existingComment.isEmpty()) {
log.info("test" + existingComment.toString());
log.info("test");
throw new DebateException(DUPLICATE_COMMENT);
}

}

@Transactional
public void deleteDebate(Long debateId, long userId) {
Optional<Debate> debate = debateRepository.getDebateByDebateId(debateId);
Expand Down

0 comments on commit 3078c96

Please sign in to comment.