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
18 changes: 18 additions & 0 deletions src/main/java/com/semosan/api/common/util/LikeConflictHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.semosan.api.common.util;

import org.springframework.dao.DataIntegrityViolationException;

public final class LikeConflictHandler {

private LikeConflictHandler() {}

public static boolean handleConcurrentCreate(Runnable createAction, Runnable conflictLogAction) {
try {
createAction.run();
return true;
} catch (DataIntegrityViolationException e) {
conflictLogAction.run();
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.semosan.api.common.exception.GeneralException;
import com.semosan.api.common.status.ErrorStatus;
import com.semosan.api.common.util.LikeConflictHandler;
import com.semosan.api.domain.community.like.dto.PostLikeToggleResponse;
import com.semosan.api.domain.community.like.entity.PostLike;
import com.semosan.api.domain.community.like.repository.PostLikeRepository;
Expand Down Expand Up @@ -42,14 +43,10 @@ private boolean toggle(Long postId, Long userId) {
return false;
}

try {
return LikeConflictHandler.handleConcurrentCreate(() -> {
postLikeRepository.save(PostLike.create(post, user));
communityNotificationService.sendPostLikeNotification(post, user);
return true;
} catch (DataIntegrityViolationException e) {
log.warn("PostLike 동시 요청 감지: postId={}, userId={}", postId, userId);
return true;
}
}, () -> log.warn("PostLike 동시 요청 감지: postId={}, userId={}", postId, userId));
Comment thread
pooreumjung marked this conversation as resolved.
}

public long count(Long postId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.semosan.api.common.exception.GeneralException;
import com.semosan.api.common.status.ErrorStatus;
import com.semosan.api.common.util.LikeConflictHandler;
import com.semosan.api.domain.mountain.dto.response.CourseLikeToggleResponse;
import com.semosan.api.domain.mountain.entity.Course;
import com.semosan.api.domain.mountain.entity.CourseLike;
Expand Down Expand Up @@ -39,17 +40,14 @@ private boolean toggle(Long userId, Long courseId) {
courseLikeRepository.delete(existing);
return false;
})
.orElseGet(() -> createCourseLike(user, course, userId, courseId));
.orElseGet(() -> createCourseLike(user, course));
}

private boolean createCourseLike(User user, Course course, Long userId, Long courseId) {
try {
courseLikeRepository.save(CourseLike.create(user, course));
return true;
} catch (DataIntegrityViolationException e) {
log.warn("CourseLike 동시 요청 감지: courseId={}, userId={}", courseId, userId);
return true;
}
private boolean createCourseLike(User user, Course course) {
return LikeConflictHandler.handleConcurrentCreate(
() -> courseLikeRepository.save(CourseLike.create(user, course)),
() -> log.warn("CourseLike 동시 요청 감지: courseId={}, userId={}", course.getId(), user.getId())
);
Comment thread
pooreumjung marked this conversation as resolved.
}

private Course findCourseById(Long courseId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.semosan.api.common.util;

import org.junit.jupiter.api.Test;
import org.springframework.dao.DataIntegrityViolationException;

import java.util.concurrent.atomic.AtomicBoolean;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class LikeConflictHandlerTest {

@Test
void handleConcurrentCreateReturnsTrueWhenCreateActionSucceeds() {
AtomicBoolean createCalled = new AtomicBoolean(false);
AtomicBoolean logCalled = new AtomicBoolean(false);

boolean result = LikeConflictHandler.handleConcurrentCreate(
() -> createCalled.set(true),
() -> logCalled.set(true)
);

assertThat(result).isTrue();
assertThat(createCalled).isTrue();
assertThat(logCalled).isFalse();
}

@Test
void handleConcurrentCreateReturnsTrueAndRunsLogActionWhenDataIntegrityViolationOccurs() {
AtomicBoolean logCalled = new AtomicBoolean(false);

boolean result = LikeConflictHandler.handleConcurrentCreate(
() -> {
throw new DataIntegrityViolationException("duplicate");
},
() -> logCalled.set(true)
);

assertThat(result).isTrue();
assertThat(logCalled).isTrue();
}

@Test
void handleConcurrentCreateRethrowsUnexpectedException() {
RuntimeException exception = new IllegalStateException("unexpected");

assertThatThrownBy(() -> LikeConflictHandler.handleConcurrentCreate(
() -> {
throw exception;
},
() -> {}
)).isSameAs(exception);
}
}