Skip to content

Commit

Permalink
refactor: bookmark 카운트가 bookmark한 대상에게 증가하는 버그를 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Mar 7, 2024
1 parent 5059c82 commit 407bdda
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ public class TargetEntity extends TimeBaseEntity {

@Version
@Column(name = "version")
private Long version;
private Long version = 0L;

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ internal class GalleryServiceTest(

describe("getGalleries 메소드는") {
beforeEach {
galleryRepository.deleteAll()

galleryService.registerGallery(oldDesignerGallery)
galleryService.registerGallery(midDeveloperGallery)
galleryService.registerGallery(latestPmGallery)
Expand Down Expand Up @@ -150,6 +152,33 @@ internal class GalleryServiceTest(
}
}
}

describe("increaseBookmarkCount 메소드는") {
context("targetId를 입력받으면,") {
galleryService.registerGallery(defaultGallery)
galleryService.increaseBookmarkCount(EXIST_TARGET_ID)

it("targetId에 해당하는 target gallery의 bookmarked count를 증가시킨다.") {
val gallery = galleryService.getGalleryByTargetId(EXIST_TARGET_ID)

gallery.getBookmarkedCount() shouldBeEqual 1
}
}
}

describe("decreaseBookmarkCount 메소드는") {
context("targetId를 입력받으면,") {
galleryService.registerGallery(defaultGallery)
galleryService.increaseBookmarkCount(EXIST_TARGET_ID)
galleryService.decreaseBookmarkCount(EXIST_TARGET_ID)

it("targetId에 해당하는 target gallery의 bookmarked count를 감소 시킨다.") {
val gallery = galleryService.getGalleryByTargetId(EXIST_TARGET_ID)

gallery.getBookmarkedCount() shouldBeEqual 0
}
}
}
}) {

companion object {
Expand All @@ -162,6 +191,15 @@ internal class GalleryServiceTest(
val bookmarkPage: PageRequest =
PageRequest.of(0, 5, Sort.by("survey.bookmarkedCount").descending())

val defaultGallery = gallery(
id = EXIST_GALLERY_ID,
targetId = EXIST_TARGET_ID,
surveyId = EXIST_SURVEY_ID,
job = Job.DESIGNER,
updateOrder = TimeUtil.toInstant().minus(1, ChronoUnit.DAYS),
bookmarkedCount = 0
)

val oldDesignerGallery = gallery(
id = 1,
targetId = 101,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import lombok.RequiredArgsConstructor;
import me.nalab.survey.application.exception.SurveyDoesNotExistException;
import me.nalab.survey.application.exception.TargetDoesNotHasSurveyException;
import me.nalab.survey.application.port.in.web.bookmark.SurveyBookmarkUseCase;
import me.nalab.survey.application.port.out.persistence.bookmark.SurveyBookmarkListenPort;
import me.nalab.survey.application.port.out.persistence.bookmark.SurveyBookmarkPort;
import me.nalab.survey.application.port.out.persistence.findfeedback.SurveyExistCheckPort;
import me.nalab.survey.application.port.out.persistence.findtarget.TargetFindPort;
import me.nalab.survey.application.port.out.persistence.findtarget.TargetIdFindPort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -15,6 +17,7 @@
public class SurveyBookmarkService implements SurveyBookmarkUseCase {

private final TargetFindPort targetFindPort;
private final TargetIdFindPort targetIdFindPort;
private final SurveyExistCheckPort surveyExistCheckPort;
private final SurveyBookmarkPort surveyBookmarkPort;
private final SurveyBookmarkListenPort surveyBookmarkListener;
Expand All @@ -28,10 +31,13 @@ public void bookmark(Long targetId, Long surveyId) {
throw new SurveyDoesNotExistException(surveyId);
}

target.bookmark(surveyId);
var bookmarkSuccess = target.bookmark(surveyId);
if (bookmarkSuccess) {
var bookmarkTargetId = targetIdFindPort.findTargetIdBySurveyId(surveyId)
.orElseThrow(() -> new TargetDoesNotHasSurveyException(surveyId));
surveyBookmarkListener.increaseBookmarked(bookmarkTargetId);
}
surveyBookmarkPort.updateBookmark(target);

surveyBookmarkListener.increaseBookmarked(targetId);
}

@Override
Expand All @@ -43,9 +49,12 @@ public void cancelBookmark(Long targetId, Long surveyId) {
throw new SurveyDoesNotExistException(surveyId);
}

target.cancelBookmark(surveyId);
var cancelSuccess = target.cancelBookmark(surveyId);
if (cancelSuccess) {
var bookmarkTargetId = targetIdFindPort.findTargetIdBySurveyId(surveyId)
.orElseThrow(() -> new TargetDoesNotHasSurveyException(surveyId));
surveyBookmarkListener.decreaseBookmarked(bookmarkTargetId);
}
surveyBookmarkPort.updateBookmark(target);

surveyBookmarkListener.decreaseBookmarked(targetId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.LongSupplier;
import lombok.Builder;
Expand Down Expand Up @@ -42,15 +41,21 @@ public void setPosition(String position) {
this.position = position;
}

public void bookmark(Long surveyId) {
public boolean bookmark(Long surveyId) {
var bookmark = new SurveyBookmark(surveyId);
if (bookmarkedSurveys.contains(bookmark)) {
return false;
}
bookmarkedSurveys.add(bookmark);
return true;
}

public void cancelBookmark(Long surveyId) {
bookmarkedSurveys.stream()
.filter(surveyBookmark -> Objects.equals(surveyBookmark.surveyId(), surveyId))
.findFirst()
.ifPresent(bookmarkedSurveys::remove);
public boolean cancelBookmark(Long surveyId) {
var canceledBookmark = new SurveyBookmark(surveyId);
if (bookmarkedSurveys.contains(canceledBookmark)) {
bookmarkedSurveys.remove(canceledBookmark);
return true;
}
return false;
}
}

0 comments on commit 407bdda

Please sign in to comment.