Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import java.time.LocalDateTime;

@Entity
@Table(
name = "cocktail_comment",
uniqueConstraints = @UniqueConstraint(columnNames = {"cocktail_id", "user_id"}) // 사용자 1개 댓글 제한
)
@Getter
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface CocktailCommentRepository extends JpaRepository<CocktailComment
List<CocktailComment> findTop10ByCocktailIdOrderByIdDesc(Long cocktailId);

List<CocktailComment> findTop10ByCocktailIdAndIdLessThanOrderByIdDesc(Long cocktailId, Long lastId);

boolean existsByCocktailIdAndUserId(Long cocktailId, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public CocktailCommentResponseDto createCocktailComment(Long cocktailId, Cocktai
Cocktail cocktail = cocktailRepository.findById(cocktailId)
.orElseThrow(() -> new IllegalArgumentException("칵테일이 존재하지 않습니다. id=" + cocktailId));

// 사용자당 댓글 1개 제한 체크
boolean exists = cocktailCommentRepository.existsByCocktailIdAndUserId(cocktailId, user.getId());
if (exists) {
throw new IllegalArgumentException("이미 댓글을 작성하셨습니다.");
}

CocktailComment cocktailComment = CocktailComment.builder()
.cocktail(cocktail)
.user(user)
Expand Down