diff --git a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandService.java b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandService.java index 51bcfc9d8..a4ac0a5b7 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandService.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandService.java @@ -1,59 +1,43 @@ package com.mapbefine.mapbefine.bookmark.application; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.CONFLICT_TOPIC_ALREADY_ADD; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.FORBIDDEN_TOPIC_ADD; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.FORBIDDEN_TOPIC_DELETE; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.ILLEGAL_TOPIC_ID; - import com.mapbefine.mapbefine.auth.domain.AuthMember; import com.mapbefine.mapbefine.bookmark.domain.Bookmark; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkId; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkBadRequestException; import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkConflictException; import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkForbiddenException; -import com.mapbefine.mapbefine.member.domain.Member; -import com.mapbefine.mapbefine.member.domain.MemberRepository; import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicRepository; -import java.util.NoSuchElementException; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.*; + @Service @Transactional public class BookmarkCommandService { private final BookmarkRepository bookmarkRepository; - private final MemberRepository memberRepository; - private final TopicRepository topicRepository; public BookmarkCommandService( BookmarkRepository bookmarkRepository, - MemberRepository memberRepository, TopicRepository topicRepository ) { this.bookmarkRepository = bookmarkRepository; - this.memberRepository = memberRepository; this.topicRepository = topicRepository; } - public Long addTopicInBookmark(AuthMember authMember, Long topicId) { + public void addTopicInBookmark(AuthMember authMember, Long topicId) { validateBookmarkDuplication(authMember, topicId); Topic topic = getTopicById(topicId); validateBookmarkingPermission(authMember, topic); - Member member = findMemberById(authMember); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, member); + Bookmark bookmark = Bookmark.of(topic, authMember.getMemberId()); bookmarkRepository.save(bookmark); - - return bookmark.getId(); - } - - private Topic getTopicById(Long topicId) { - return topicRepository.findById(topicId) - .orElseThrow(() -> new BookmarkBadRequestException(ILLEGAL_TOPIC_ID)); + topic.increaseBookmarkCount(); } private void validateBookmarkDuplication(AuthMember authMember, Long topicId) { @@ -63,7 +47,12 @@ private void validateBookmarkDuplication(AuthMember authMember, Long topicId) { } private boolean isExistBookmark(AuthMember authMember, Long topicId) { - return bookmarkRepository.existsByMemberIdAndTopicId(authMember.getMemberId(), topicId); + return bookmarkRepository.existsById(BookmarkId.of(topicId, authMember.getMemberId())); + } + + private Topic getTopicById(Long topicId) { + return topicRepository.findById(topicId) + .orElseThrow(() -> new BookmarkBadRequestException(ILLEGAL_TOPIC_ID)); } private void validateBookmarkingPermission(AuthMember authMember, Topic topic) { @@ -74,27 +63,14 @@ private void validateBookmarkingPermission(AuthMember authMember, Topic topic) { throw new BookmarkForbiddenException(FORBIDDEN_TOPIC_ADD); } - private Member findMemberById(AuthMember authMember) { - Long memberId = authMember.getMemberId(); - - return memberRepository.findById(memberId) - .orElseThrow(() -> new NoSuchElementException("findMemberById; memberId=" + memberId)); - } - + // TODO: 2023/12/03 BookmarkCount의 정합성을 어떻게 맞출 것인가 ? 매번 topic 조회하는 것은 불필요한 행위같아보임 public void deleteTopicInBookmark(AuthMember authMember, Long topicId) { validateBookmarkDeletingPermission(authMember, topicId); + BookmarkId bookmarkId = BookmarkId.of(topicId, authMember.getMemberId()); - Bookmark bookmark = findBookmarkByMemberIdAndTopicId(authMember.getMemberId(), topicId); + bookmarkRepository.deleteById(bookmarkId); Topic topic = getTopicById(topicId); - - topic.removeBookmark(bookmark); - } - - private Bookmark findBookmarkByMemberIdAndTopicId(Long memberId, Long topicId) { - return bookmarkRepository.findByMemberIdAndTopicId(memberId, topicId) - .orElseThrow(() -> new NoSuchElementException( - "findBookmarkByMemberIdAndTopicId; memberId=" + memberId + " topicId=" + topicId - )); + topic.decreaseBookmarkCount(); } private void validateBookmarkDeletingPermission(AuthMember authMember, Long topicId) { diff --git a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/Bookmark.java b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/Bookmark.java index c43bcb037..2fbd174d7 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/Bookmark.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/Bookmark.java @@ -1,18 +1,12 @@ package com.mapbefine.mapbefine.bookmark.domain; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.ILLEGAL_MEMBER_ID; -import static com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode.ILLEGAL_TOPIC_ID; - -import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkBadRequestException; -import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.topic.domain.Topic; +import jakarta.persistence.EmbeddedId; import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.FetchType; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import java.util.Objects; +import jakarta.persistence.MapsId; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; @@ -22,40 +16,29 @@ @Getter public class Bookmark { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @EmbeddedId + private BookmarkId id; - @ManyToOne + @MapsId("topicId") + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "topic_id", nullable = false) private Topic topic; - @ManyToOne - @JoinColumn(name = "member_id", nullable = false) - private Member member; - - private Bookmark(Topic topic, Member member) { + private Bookmark(BookmarkId id, Topic topic) { + this.id = id; this.topic = topic; - this.member = member; } - public static Bookmark createWithAssociatedTopicAndMember(Topic topic, Member member) { - validateNotNull(topic, member); - Bookmark bookmark = new Bookmark(topic, member); - - topic.addBookmark(bookmark); - member.addBookmark(bookmark); + public static Bookmark of(Topic topic, Long memberId) { + return new Bookmark(BookmarkId.of(topic.getId(), memberId), topic); + } - return bookmark; + public Long getTopicId() { + return id.getTopicId(); } - private static void validateNotNull(Topic topic, Member member) { - if (Objects.isNull(topic)) { - throw new BookmarkBadRequestException(ILLEGAL_TOPIC_ID); - } - if (Objects.isNull(member)) { - throw new BookmarkBadRequestException(ILLEGAL_MEMBER_ID); - } + public Long getMemberId() { + return id.getMemberId(); } } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkId.java b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkId.java new file mode 100644 index 000000000..1356c5647 --- /dev/null +++ b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkId.java @@ -0,0 +1,58 @@ +package com.mapbefine.mapbefine.bookmark.domain; + +import com.mapbefine.mapbefine.bookmark.exception.BookmarkErrorCode; +import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkBadRequestException; +import jakarta.persistence.Column; +import jakarta.persistence.Embeddable; +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.io.Serializable; +import java.util.Objects; + +@Embeddable +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@Getter +public class BookmarkId implements Serializable { + + @Column(nullable = false, updatable = false) + private Long topicId; + + @Column(nullable = false, updatable = false) + private Long memberId; + + private BookmarkId(Long topicId, Long memberId) { + this.topicId = topicId; + this.memberId = memberId; + } + + public static BookmarkId of(Long topicId, Long memberId) { + validateNotNull(topicId, memberId); + + return new BookmarkId(topicId, memberId); + } + + private static void validateNotNull(Long topicId, Long memberId) { + if (Objects.isNull(topicId)) { + throw new BookmarkBadRequestException(BookmarkErrorCode.ILLEGAL_TOPIC_ID); + } + + if (Objects.isNull(memberId)) { + throw new BookmarkBadRequestException(BookmarkErrorCode.ILLEGAL_MEMBER_ID); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + BookmarkId that = (BookmarkId) o; + return Objects.equals(getTopicId(), that.getTopicId()) && Objects.equals(getMemberId(), that.getMemberId()); + } + + @Override + public int hashCode() { + return Objects.hash(getTopicId(), getMemberId()); + } +} diff --git a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkRepository.java b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkRepository.java index f16d40797..eba70197f 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkRepository.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/domain/BookmarkRepository.java @@ -1,23 +1,26 @@ package com.mapbefine.mapbefine.bookmark.domain; -import java.util.Optional; +import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -public interface BookmarkRepository extends JpaRepository { +import java.util.List; - Optional findByMemberIdAndTopicId(Long memberId, Long topicId); +public interface BookmarkRepository extends JpaRepository { - boolean existsByMemberIdAndTopicId(Long memberId, Long topicId); + List findAllIdTopicIdByIdMemberId(Long memberId); + + @EntityGraph(attributePaths = "topic") + List findAllByIdMemberId(Long memberId); @Modifying(clearAutomatically = true) - @Query("delete from Bookmark b where b.member.id = :memberId") + @Query("delete from Bookmark b where b.id.memberId = :memberId") void deleteAllByMemberId(@Param("memberId") Long memberId); @Modifying(clearAutomatically = true) - @Query("delete from Bookmark b where b.topic.id = :topicId") + @Query("delete from Bookmark b where b.id.topicId = :topicId") void deleteAllByTopicId(@Param("topicId") Long topicId); } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkController.java b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkController.java index 832f719a4..37df5f938 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkController.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkController.java @@ -3,12 +3,12 @@ import com.mapbefine.mapbefine.auth.domain.AuthMember; import com.mapbefine.mapbefine.bookmark.application.BookmarkCommandService; import com.mapbefine.mapbefine.common.interceptor.LoginRequired; -import java.net.URI; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @@ -22,17 +22,17 @@ public BookmarkController(BookmarkCommandService bookmarkCommandService) { } @LoginRequired - @PostMapping("/topics") - public ResponseEntity addTopicInBookmark(AuthMember authMember, @RequestParam Long id) { - Long bookmarkId = bookmarkCommandService.addTopicInBookmark(authMember, id); + @PostMapping("/topics/{topicId}") + public ResponseEntity addTopicInBookmark(AuthMember authMember, @PathVariable Long topicId) { + bookmarkCommandService.addTopicInBookmark(authMember, topicId); - return ResponseEntity.created(URI.create("/bookmarks/topics" + bookmarkId)).build(); + return ResponseEntity.status(HttpStatus.CREATED).build(); } @LoginRequired - @DeleteMapping("/topics") - public ResponseEntity deleteTopicInBookmark(AuthMember authMember, @RequestParam Long id) { - bookmarkCommandService.deleteTopicInBookmark(authMember, id); + @DeleteMapping("/topics/{topicId}") + public ResponseEntity deleteTopicInBookmark(AuthMember authMember, @PathVariable Long topicId) { + bookmarkCommandService.deleteTopicInBookmark(authMember, topicId); return ResponseEntity.noContent().build(); } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/location/application/LocationQueryService.java b/backend/src/main/java/com/mapbefine/mapbefine/location/application/LocationQueryService.java index 5c67c85e1..d68bf7d6f 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/location/application/LocationQueryService.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/location/application/LocationQueryService.java @@ -1,11 +1,8 @@ package com.mapbefine.mapbefine.location.application; -import static java.util.stream.Collectors.counting; -import static java.util.stream.Collectors.groupingBy; - import com.mapbefine.mapbefine.atlas.domain.Atlas; import com.mapbefine.mapbefine.auth.domain.AuthMember; -import com.mapbefine.mapbefine.bookmark.domain.Bookmark; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.location.domain.Coordinate; import com.mapbefine.mapbefine.location.domain.Location; import com.mapbefine.mapbefine.location.domain.LocationRepository; @@ -14,14 +11,18 @@ import com.mapbefine.mapbefine.pin.domain.Pin; import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.dto.response.TopicResponse; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; + +import static java.util.stream.Collectors.counting; +import static java.util.stream.Collectors.groupingBy; @Service @Transactional(readOnly = true) @@ -31,10 +32,16 @@ public class LocationQueryService { private final LocationRepository locationRepository; private final MemberRepository memberRepository; + private final BookmarkRepository bookmarkRepository; - public LocationQueryService(LocationRepository locationRepository, MemberRepository memberRepository) { + public LocationQueryService( + LocationRepository locationRepository, + MemberRepository memberRepository, + BookmarkRepository bookmarkRepository + ) { this.locationRepository = locationRepository; this.memberRepository = memberRepository; + this.bookmarkRepository = bookmarkRepository; } public List findNearbyTopicsSortedByPinCount( @@ -73,7 +80,7 @@ private List sortTopicsByPinCounts( private List getUserTopicResponses(final Map topicCounts, final AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List bookmarkedTopics = findBookMarkedTopics(member); + List bookmarkedTopicIds = bookmarkRepository.findAllIdTopicIdByIdMemberId(member.getId()); List topicsInAtlas = findTopicsInAtlas(member); return topicCounts.entrySet().stream() @@ -85,7 +92,7 @@ private List getUserTopicResponses(final Map topicCo return TopicResponse.from( topic, isInAtlas(topicsInAtlas, topic), - isInBookmark(bookmarkedTopics, topic) + isInBookmark(bookmarkedTopicIds, topic.getId()) ); }) .toList(); @@ -123,15 +130,8 @@ private boolean isInAtlas(List topicsInAtlas, Topic topic) { return topicsInAtlas.contains(topic); } - private List findBookMarkedTopics(Member member) { - return member.getBookmarks() - .stream() - .map(Bookmark::getTopic) - .toList(); - } - - private boolean isInBookmark(List bookmarkedTopics, Topic topic) { - return bookmarkedTopics.contains(topic); + private boolean isInBookmark(List bookmarkedTopics, Long topicId) { + return bookmarkedTopics.contains(topicId); } } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberQueryService.java b/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberQueryService.java index 261ec15f1..fed949125 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberQueryService.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/member/application/MemberQueryService.java @@ -3,6 +3,8 @@ import com.mapbefine.mapbefine.atlas.domain.Atlas; import com.mapbefine.mapbefine.atlas.domain.AtlasRepository; import com.mapbefine.mapbefine.auth.domain.AuthMember; +import com.mapbefine.mapbefine.bookmark.domain.Bookmark; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkId; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; @@ -14,10 +16,11 @@ import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicRepository; import com.mapbefine.mapbefine.topic.dto.response.TopicResponse; -import java.util.List; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; + @Service @Transactional(readOnly = true) public class MemberQueryService { @@ -25,7 +28,6 @@ public class MemberQueryService { private final MemberRepository memberRepository; private final AtlasRepository atlasRepository; private final BookmarkRepository bookmarkRepository; - private final TopicRepository topicRepository; public MemberQueryService( @@ -61,19 +63,24 @@ public List findAll() { } public List findAllTopicsInBookmark(AuthMember authMember) { - Member member = findMemberById(authMember.getMemberId()); + List bookMarkedTopics = findTopicsInBookmark(authMember); - List bookMarkedTopics = topicRepository.findTopicsByBookmarksMemberId( - authMember.getMemberId()); return bookMarkedTopics.stream() .map(topic -> TopicResponse.from( topic, - isInAtlas(member.getId(), topic.getId()), + isInAtlas(authMember.getMemberId(), topic.getId()), true )) .toList(); } + private List findTopicsInBookmark(AuthMember authMember) { + return bookmarkRepository.findAllByIdMemberId(authMember.getMemberId()) + .stream() + .map(Bookmark::getTopic) + .toList(); + } + private boolean isInAtlas(Long memberId, Long topicId) { return atlasRepository.existsByMemberIdAndTopicId(memberId, topicId); } @@ -99,14 +106,14 @@ private List findTopicsInAtlas(Member member) { } private boolean isInBookmark(Long memberId, Long topicId) { - return bookmarkRepository.existsByMemberIdAndTopicId(memberId, topicId); + return bookmarkRepository.existsById(BookmarkId.of(topicId, memberId)); } public List findMyAllTopics(AuthMember authMember) { Long memberId = authMember.getMemberId(); - Member member = findMemberById(memberId); + List createdTopics = topicRepository.findAllByCreatorId(memberId); - return member.getCreatedTopics() + return createdTopics .stream() .map(topic -> TopicResponse.from( topic, diff --git a/backend/src/main/java/com/mapbefine/mapbefine/member/domain/Member.java b/backend/src/main/java/com/mapbefine/mapbefine/member/domain/Member.java index 9f103d549..f2feacf94 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/member/domain/Member.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/member/domain/Member.java @@ -1,7 +1,6 @@ package com.mapbefine.mapbefine.member.domain; import com.mapbefine.mapbefine.atlas.domain.Atlas; -import com.mapbefine.mapbefine.bookmark.domain.Bookmark; import com.mapbefine.mapbefine.common.entity.BaseTimeEntity; import com.mapbefine.mapbefine.pin.domain.Pin; import com.mapbefine.mapbefine.topic.domain.Topic; @@ -45,9 +44,6 @@ public class Member extends BaseTimeEntity { @OneToMany(mappedBy = "creator") private List createdPins = new ArrayList<>(); - @OneToMany(mappedBy = "member") - private List bookmarks = new ArrayList<>(); - @OneToMany(mappedBy = "member") private List atlantes = new ArrayList<>(); @@ -119,10 +115,6 @@ public void addPin(Pin pin) { createdPins.add(pin); } - public void addBookmark(Bookmark bookmark) { - bookmarks.add(bookmark); - } - public void addAtlas(Atlas atlas) { atlantes.add(atlas); } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/member/dto/response/MemberResponse.java b/backend/src/main/java/com/mapbefine/mapbefine/member/dto/response/MemberResponse.java index dc4ac4cfd..c6d2f7739 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/member/dto/response/MemberResponse.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/member/dto/response/MemberResponse.java @@ -13,6 +13,7 @@ public record MemberResponse( * 해당 기본 생성자를 삭제하더라도, 오류가 발생하진 않지만, 경고 문구가 발생합니다. (빨간줄) * 이 부분 어떻게 할까요 ? * */ + // TODO: 2023/12/03 해당 기본 생성자 제거후, Domain -> Service 계층으로 넘기는 DTO 사용 public MemberResponse { } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/permission/domain/PermissionId.java b/backend/src/main/java/com/mapbefine/mapbefine/permission/domain/PermissionId.java index f0cb5512e..79975cc76 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/permission/domain/PermissionId.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/permission/domain/PermissionId.java @@ -1,5 +1,7 @@ package com.mapbefine.mapbefine.permission.domain; +import com.mapbefine.mapbefine.permission.exception.PermissionErrorCode; +import com.mapbefine.mapbefine.permission.exception.PermissionException.PermissionBadRequestException; import jakarta.persistence.Column; import jakarta.persistence.Embeddable; import lombok.AccessLevel; @@ -20,15 +22,27 @@ public class PermissionId implements Serializable { @Column(nullable = false, updatable = false) private Long memberId; - public PermissionId(Long topicId, Long memberId) { + private PermissionId(Long topicId, Long memberId) { this.topicId = topicId; this.memberId = memberId; } public static PermissionId of(Long topicId, Long memberId) { + validateNotNull(topicId, memberId); + return new PermissionId(topicId, memberId); } + private static void validateNotNull(Long topicId, Long memberId) { + if (Objects.isNull(topicId)) { + throw new PermissionBadRequestException(PermissionErrorCode.ILLEGAL_TOPIC_ID); + } + + if (Objects.isNull(memberId)) { + throw new PermissionBadRequestException(PermissionErrorCode.ILLEGAL_MEMBER_ID); + } + } + @Override public boolean equals(Object o) { if (this == o) return true; diff --git a/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionErrorCode.java b/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionErrorCode.java index c3f3f6999..61c3fc55b 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionErrorCode.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionErrorCode.java @@ -7,6 +7,7 @@ public enum PermissionErrorCode { ILLEGAL_TOPIC_ID("07000", "유효하지 않은 지도입니다."), ILLEGAL_PERMISSION_ID("07001", "유효하지 않은 권한 정보입니다."), + ILLEGAL_MEMBER_ID("07002", "유효하지 않은 회원 정보입니다."), FORBIDDEN_ADD_PERMISSION_GUEST("07300", "로그인하지 않은 사용자는 권한을 줄 수 없습니다."), FORBIDDEN_ADD_PERMISSION("07301", "지도를 생성한 사용자가 아니면 권한을 줄 수 없습니다."), PERMISSION_NOT_FOUND("07400", "존재하지 않는 권한 정보입니다."), diff --git a/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionException.java b/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionException.java index 040570e2a..a5435064d 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionException.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/permission/exception/PermissionException.java @@ -20,6 +20,10 @@ public PermissionBadRequestException(PermissionErrorCode errorCode, PermissionId public PermissionBadRequestException(PermissionErrorCode errorCode, Long id) { super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage(), id)); } + + public PermissionBadRequestException(PermissionErrorCode errorCode) { + super(new ErrorCode<>(errorCode.getCode(), errorCode.getMessage())); + } } public static class PermissionForbiddenException extends ForbiddenException { diff --git a/backend/src/main/java/com/mapbefine/mapbefine/topic/application/TopicQueryService.java b/backend/src/main/java/com/mapbefine/mapbefine/topic/application/TopicQueryService.java index 383a742dc..fdc32482d 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/topic/application/TopicQueryService.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/topic/application/TopicQueryService.java @@ -1,10 +1,8 @@ package com.mapbefine.mapbefine.topic.application; -import static com.mapbefine.mapbefine.topic.exception.TopicErrorCode.FORBIDDEN_TOPIC_READ; -import static com.mapbefine.mapbefine.topic.exception.TopicErrorCode.TOPIC_NOT_FOUND; - import com.mapbefine.mapbefine.atlas.domain.AtlasRepository; import com.mapbefine.mapbefine.auth.domain.AuthMember; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkId; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; @@ -17,12 +15,16 @@ import com.mapbefine.mapbefine.topic.dto.response.TopicResponse; import com.mapbefine.mapbefine.topic.exception.TopicException.TopicForbiddenException; import com.mapbefine.mapbefine.topic.exception.TopicException.TopicNotFoundException; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + import java.util.Comparator; import java.util.List; import java.util.NoSuchElementException; import java.util.Objects; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; + +import static com.mapbefine.mapbefine.topic.exception.TopicErrorCode.FORBIDDEN_TOPIC_READ; +import static com.mapbefine.mapbefine.topic.exception.TopicErrorCode.TOPIC_NOT_FOUND; @Service @Transactional(readOnly = true) @@ -83,7 +85,7 @@ private boolean isInAtlas(Long memberId, Long topicId) { } private boolean isBookMarked(Long memberId, Long topicId) { - return bookmarkRepository.existsByMemberIdAndTopicId(memberId, topicId); + return bookmarkRepository.existsById(BookmarkId.of(topicId, memberId)); } public TopicDetailResponse findDetailById(AuthMember authMember, Long topicId) { diff --git a/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/Topic.java b/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/Topic.java index e6bab7b4c..6bbf33cef 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/Topic.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/Topic.java @@ -1,6 +1,5 @@ package com.mapbefine.mapbefine.topic.domain; -import com.mapbefine.mapbefine.bookmark.domain.Bookmark; import com.mapbefine.mapbefine.common.entity.BaseTimeEntity; import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.pin.domain.Pin; @@ -50,13 +49,11 @@ public class Topic extends BaseTimeEntity { @OneToMany(mappedBy = "topic", cascade = CascadeType.PERSIST) private List pins = new ArrayList<>(); - @OneToMany(mappedBy = "topic", cascade = CascadeType.PERSIST, orphanRemoval = true) - private List bookmarks = new ArrayList<>(); - @Column(nullable = false) @ColumnDefault(value = "0") private int pinCount = 0; + // TODO: 2023/12/03 간접 참조에 따른 bookmark Count 정합성을 어떻게 맞출 것인가 ? @Column(nullable = false) @ColumnDefault(value = "0") private int bookmarkCount = 0; @@ -128,11 +125,6 @@ public void addPin(Pin pin) { pinCount++; } - public void addBookmark(Bookmark bookmark) { - bookmarks.add(bookmark); - bookmarkCount++; - } - public int countBookmarks() { return bookmarkCount; } @@ -153,8 +145,12 @@ public void decreasePinCount() { pinCount--; } - public void removeBookmark(Bookmark bookmark) { - bookmarks.remove(bookmark); + public void increaseBookmarkCount() { + bookmarkCount++; + } + + public void decreaseBookmarkCount() { bookmarkCount--; } + } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/TopicRepository.java b/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/TopicRepository.java index 139208760..4ab5a4af8 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/TopicRepository.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/topic/domain/TopicRepository.java @@ -1,7 +1,5 @@ package com.mapbefine.mapbefine.topic.domain; -import java.util.List; -import java.util.Optional; import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; @@ -9,6 +7,9 @@ import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface TopicRepository extends JpaRepository { @@ -27,9 +28,6 @@ public interface TopicRepository extends JpaRepository { @EntityGraph(attributePaths = {"creator"}) List findAllByCreatorId(Long creatorId); - @EntityGraph(attributePaths = {"creator"}) - List findTopicsByBookmarksMemberId(Long memberId); - @Modifying(clearAutomatically = true) @Query("update Topic t set t.isDeleted = true where t.id = :topicId") void deleteById(@Param("topicId") Long topicId); diff --git a/backend/src/main/java/com/mapbefine/mapbefine/topic/dto/response/TopicResponse.java b/backend/src/main/java/com/mapbefine/mapbefine/topic/dto/response/TopicResponse.java index bd757a434..d77d55653 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/topic/dto/response/TopicResponse.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/topic/dto/response/TopicResponse.java @@ -2,6 +2,7 @@ import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicInfo; + import java.time.LocalDateTime; public record TopicResponse( diff --git a/backend/src/test/java/com/mapbefine/mapbefine/admin/application/AdminCommandServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/admin/application/AdminCommandServiceTest.java index 35576e9d7..02e5e1a0a 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/admin/application/AdminCommandServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/admin/application/AdminCommandServiceTest.java @@ -1,12 +1,10 @@ package com.mapbefine.mapbefine.admin.application; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.SoftAssertions.assertSoftly; - import com.mapbefine.mapbefine.TestDatabaseContainer; import com.mapbefine.mapbefine.atlas.domain.Atlas; import com.mapbefine.mapbefine.atlas.domain.AtlasRepository; import com.mapbefine.mapbefine.bookmark.domain.Bookmark; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkId; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.common.annotation.ServiceTest; import com.mapbefine.mapbefine.location.LocationFixture; @@ -34,6 +32,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.SoftAssertions.assertSoftly; + @ServiceTest class AdminCommandServiceTest extends TestDatabaseContainer { @@ -78,7 +79,7 @@ void setup() { @Test void blockMember_Success() { //given - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, member); + Bookmark bookmark = Bookmark.of(topic, member.getId()); Atlas atlas = Atlas.createWithAssociatedMember(topic, member); Permission permission = Permission.of(topic.getId(), member.getId()); @@ -91,8 +92,8 @@ void blockMember_Success() { assertThat(topic.isDeleted()).isFalse(); assertThat(pin.isDeleted()).isFalse(); assertThat(pinImage.isDeleted()).isFalse(); - assertThat(bookmarkRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isTrue(); - assertThat(atlasRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isTrue(); + assertThat(bookmarkRepository.existsById(BookmarkId.of(topic.getId(), member.getId()))).isTrue(); + assertThat(atlasRepository.existsByMemberIdAndTopicId(topic.getId(), member.getId())).isTrue(); assertThat(permissionRepository.existsByIdTopicIdAndIdMemberId(topic.getId(), member.getId())).isTrue(); }); @@ -108,7 +109,7 @@ void blockMember_Success() { assertThat(topicRepository.existsById(topic.getId())).isFalse(); assertThat(pinRepository.existsById(pin.getId())).isFalse(); assertThat(pinImageRepository.existsById(pinImage.getId())).isFalse(); - assertThat(bookmarkRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isFalse(); + assertThat(bookmarkRepository.existsById(BookmarkId.of(topic.getId(), member.getId()))).isFalse(); assertThat(atlasRepository.existsByMemberIdAndTopicId(member.getId(), topic.getId())).isFalse(); assertThat(permissionRepository.existsByIdTopicIdAndIdMemberId(topic.getId(), member.getId())).isFalse(); }); diff --git a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/BookmarkIntegrationTest.java b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/BookmarkIntegrationTest.java index c29311e8a..fc6ce4430 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/BookmarkIntegrationTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/BookmarkIntegrationTest.java @@ -51,15 +51,12 @@ void addTopicInBookmark_Success() { //when ExtractableResponse response = given().log().all() .header(AUTHORIZATION, otherUserAuthHeader) - .param("id", topic.getId()) - .when().post("/bookmarks/topics") + .when().post("/bookmarks/topics/" + topic.getId()) .then().log().all() .extract(); //then assertThat(response.statusCode()).isEqualTo(HttpStatus.CREATED.value()); - assertThat(response.header("Location")).startsWith("/bookmarks/topics") - .isNotNull(); } @Test @@ -72,7 +69,7 @@ void deleteTopicInBookmark_Success() { Topic topic = TopicFixture.createByName("topic1", creator); topicRepository.save(topic); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, creator); + Bookmark bookmark = Bookmark.of(topic, creator.getId()); bookmarkRepository.save(bookmark); String creatorAuthHeader = testAuthHeaderProvider.createAuthHeader(creator); @@ -80,8 +77,7 @@ void deleteTopicInBookmark_Success() { //when then given().log().all() .header(AUTHORIZATION, creatorAuthHeader) - .param("id", topic.getId()) - .when().delete("/bookmarks/topics") + .when().delete("/bookmarks/topics/" + topic.getId()) .then().log().all() .statusCode(HttpStatus.NO_CONTENT.value()); } diff --git a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandServiceTest.java index 3b746aa84..0366c623b 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/application/BookmarkCommandServiceTest.java @@ -3,6 +3,7 @@ import com.mapbefine.mapbefine.TestDatabaseContainer; import com.mapbefine.mapbefine.auth.domain.AuthMember; import com.mapbefine.mapbefine.bookmark.domain.Bookmark; +import com.mapbefine.mapbefine.bookmark.domain.BookmarkId; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.bookmark.exception.BookmarkException.BookmarkForbiddenException; import com.mapbefine.mapbefine.common.annotation.ServiceTest; @@ -60,16 +61,15 @@ void addTopicInBookmark_Success() { Role.USER ); memberRepository.save(otherMember); - Long bookmarkId = bookmarkCommandService.addTopicInBookmark( - MemberFixture.createUserWithoutTopics(otherMember), - topic.getId() - ); + AuthMember user = MemberFixture.createUserWithoutTopics(otherMember); + bookmarkCommandService.addTopicInBookmark(user, topic.getId()); //then - Bookmark bookmark = bookmarkRepository.findById(bookmarkId).get(); - assertThat(bookmark.getTopic().getId()).isEqualTo(topic.getId()); - assertThat(bookmark.getMember().getId()).isEqualTo(otherMember.getId()); + Bookmark bookmark = bookmarkRepository.findById(BookmarkId.of(topic.getId(), user.getMemberId())).get(); + + assertThat(bookmark.getTopicId()).isEqualTo(topic.getId()); + assertThat(bookmark.getMemberId()).isEqualTo(otherMember.getId()); } @Test @@ -122,7 +122,7 @@ void deleteTopicInBookmark_Success() { ); memberRepository.save(otherMember); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, otherMember); + Bookmark bookmark = Bookmark.of(topic, otherMember.getId()); bookmarkRepository.save(bookmark); //when @@ -149,7 +149,7 @@ void deleteTopicInBookmark_Fail() { memberRepository.save(creator); topicRepository.save(topic); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, creator); + Bookmark bookmark = Bookmark.of(topic, creator.getId()); bookmarkRepository.save(bookmark); Member otherMember = MemberFixture.create( diff --git a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkControllerTest.java b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkControllerTest.java index a509b53ac..90ef08bab 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkControllerTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/bookmark/presentation/BookmarkControllerTest.java @@ -21,10 +21,7 @@ class BookmarkControllerTest extends RestDocsIntegration { @Test @DisplayName("토픽을 회원의 즐겨찾기에 추가") void addTopicInBookmark() throws Exception { - given(bookmarkCommandService.addTopicInBookmark(any(), any())).willReturn(1L); - - mockMvc.perform(MockMvcRequestBuilders.post("/bookmarks/topics") - .queryParam("id", String.valueOf(1)) + mockMvc.perform(MockMvcRequestBuilders.post("/bookmarks/topics/1") .header(AUTHORIZATION, testAuthHeaderProvider.createAuthHeaderById(1L))) .andExpect(MockMvcResultMatchers.status().isCreated()) .andDo(restDocs.document()); @@ -35,8 +32,7 @@ void addTopicInBookmark() throws Exception { void deleteTopicInBookmark() throws Exception { doNothing().when(bookmarkCommandService).deleteTopicInBookmark(any(), any()); - mockMvc.perform(MockMvcRequestBuilders.delete("/bookmarks/topics") - .queryParam("id", String.valueOf(1L)) + mockMvc.perform(MockMvcRequestBuilders.delete("/bookmarks/topics/1") .header(AUTHORIZATION, testAuthHeaderProvider.createAuthHeaderById(1L))) .andExpect(MockMvcResultMatchers.status().isNoContent()) .andDo(restDocs.document()); diff --git a/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberQueryServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberQueryServiceTest.java index 3b7a4f85c..f4b66160f 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberQueryServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/member/application/MemberQueryServiceTest.java @@ -68,7 +68,7 @@ void setUp() { createTopics(member); topics.forEach(topic -> atlasRepository.save(Atlas.createWithAssociatedMember(topic, member))); - topics.forEach(topic -> bookmarkRepository.save(Bookmark.createWithAssociatedTopicAndMember(topic, member))); + topics.forEach(topic -> bookmarkRepository.save(Bookmark.of(topic, member.getId()))); } private void createTopics(Member member) { diff --git a/backend/src/test/java/com/mapbefine/mapbefine/permission/domain/PermissionTest.java b/backend/src/test/java/com/mapbefine/mapbefine/permission/domain/PermissionTest.java index 964b1dde2..5257970a9 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/permission/domain/PermissionTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/permission/domain/PermissionTest.java @@ -1,15 +1,8 @@ package com.mapbefine.mapbefine.permission.domain; -import com.mapbefine.mapbefine.member.MemberFixture; -import com.mapbefine.mapbefine.member.domain.Member; -import com.mapbefine.mapbefine.member.domain.Role; -import com.mapbefine.mapbefine.topic.TopicFixture; -import com.mapbefine.mapbefine.topic.domain.Topic; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.List; - import static org.assertj.core.api.Assertions.assertThat; class PermissionTest { @@ -17,20 +10,16 @@ class PermissionTest { @Test @DisplayName("정상적인 값을 입력하면 객체가 생성된다.") void createPermission() { - // given - Member member = MemberFixture.create( - "member", - "member@naver.com", - Role.ADMIN - ); - Topic topic = TopicFixture.createByName("topic", member); + //given + Long topicId = 1L; + Long memberId = 2L; // when - Permission permission = Permission.of(topic.getId(), member.getId()); + Permission permission = Permission.of(topicId, memberId); // then - assertThat(permission.getTopicId()).isEqualTo(topic.getId()); - assertThat(permission.getMemberId()).isEqualTo(member.getId()); + assertThat(permission.getTopicId()).isEqualTo(topicId); + assertThat(permission.getMemberId()).isEqualTo(memberId); } } diff --git a/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicFixture.java b/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicFixture.java index d364100cd..645a291a7 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicFixture.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicFixture.java @@ -52,17 +52,6 @@ public static Topic createPublicAndAllMembersTopic(Member member) { ); } - public static Topic createPublicAndGroupOnlyTopic(Member member) { - return Topic.createTopicAssociatedWithCreator( - "아무나 읽을 수 있는 토픽", - "아무나 읽지만 아무나 생성할 수는 없습니다.", - IMAGE_URL, - Publicity.PUBLIC, - PermissionType.GROUP_ONLY, - member - ); - } - public static Topic createPublicAndAllMembersTopic(String imageUrl, Member member) { return Topic.createTopicAssociatedWithCreator( "아무나 읽을 수 있는 토픽", diff --git a/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java b/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java index 4520c8e53..21f402e1e 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java @@ -1,6 +1,5 @@ package com.mapbefine.mapbefine.topic; -import com.mapbefine.mapbefine.bookmark.domain.Bookmark; import com.mapbefine.mapbefine.bookmark.domain.BookmarkRepository; import com.mapbefine.mapbefine.common.IntegrationTest; import com.mapbefine.mapbefine.location.LocationFixture; @@ -379,10 +378,12 @@ void findAllBestTopics_Success() { Topic bestOneTopic = TopicFixture.createPublicAndAllMembersTopic(member); topicRepository.save(bestOneTopic); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(bestOneTopic, member); - bookmarkRepository.save(bookmark); + RestAssured.given().log().all() + .header(AUTHORIZATION, authHeader) + .when().post("/bookmarks/topics/" + bestOneTopic.getId()) + .then().log().all(); - topicRepository.save(bestOneTopic); + bestOneTopic = topicRepository.findById(bestOneTopic.getId()).get(); // when List expect = List.of( diff --git a/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicQueryServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicQueryServiceTest.java index 8a7d1a970..e217c6b55 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicQueryServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicQueryServiceTest.java @@ -35,7 +35,6 @@ import java.util.Collections; import java.util.List; -import static com.mapbefine.mapbefine.member.MemberFixture.createUser; import static com.mapbefine.mapbefine.member.MemberFixture.createUserWithoutTopics; import static java.util.List.of; import static org.assertj.core.api.Assertions.assertThat; @@ -177,7 +176,7 @@ void findAllReadableWithBookmark_Success() { topicRepository.save(topic1); topicRepository.save(topic2); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic1, member); + Bookmark bookmark = Bookmark.of(topic1, member.getId()); bookmarkRepository.save(bookmark); //when then @@ -200,7 +199,7 @@ void findAllReadableWithoutBookmark_Success() { topicRepository.save(topic1); topicRepository.save(topic2); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic1, member); + Bookmark bookmark = Bookmark.of(topic1, member.getId()); bookmarkRepository.save(bookmark); //when //then @@ -307,7 +306,7 @@ void findDetailById_WithBookmarkStatus_Success() { Topic topic = TopicFixture.createPublicAndAllMembersTopic(member); topicRepository.save(topic); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, member); + Bookmark bookmark = Bookmark.of(topic, member.getId()); bookmarkRepository.save(bookmark); //when then @@ -327,7 +326,7 @@ void findDetailById_WithoutBookmarkStatus_Success() { Topic topic = TopicFixture.createPublicAndAllMembersTopic(member); topicRepository.save(topic); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic, member); + Bookmark bookmark = Bookmark.of(topic, member.getId()); bookmarkRepository.save(bookmark); //when then @@ -434,7 +433,7 @@ void findDetailsByIds_WithBookmarkStatus_Success() { topicRepository.save(topic1); topicRepository.save(topic2); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic1, member); + Bookmark bookmark = Bookmark.of(topic1, member.getId()); bookmarkRepository.save(bookmark); //when //then @@ -458,7 +457,7 @@ void findDetailsByIds_WithoutBookmarkStatus_Success() { topicRepository.save(topic1); topicRepository.save(topic2); - Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(topic1, member); + Bookmark bookmark = Bookmark.of(topic1, member.getId()); bookmarkRepository.save(bookmark); //when //then @@ -542,6 +541,9 @@ void findAllBestTopics_Success1() { Topic topicWithNoBookmark = TopicFixture.createPublicAndAllMembersTopic(member); Topic topicWithOneBookmark = TopicFixture.createPublicAndAllMembersTopic(member); Topic topicWithTwoBookmark = TopicFixture.createPublicAndAllMembersTopic(member); + increaseBookmarkCount(topicWithOneBookmark, 1); + increaseBookmarkCount(topicWithTwoBookmark, 2); + topicRepository.save(topicWithNoBookmark); topicRepository.save(topicWithOneBookmark); topicRepository.save(topicWithTwoBookmark); @@ -652,7 +654,13 @@ void findClusteringPinsByIds_fail() { } private Bookmark saveBookmark(Topic topic, Member member) { - return bookmarkRepository.save(Bookmark.createWithAssociatedTopicAndMember(topic, member)); + return bookmarkRepository.save(Bookmark.of(topic, member.getId())); + } + + private void increaseBookmarkCount(Topic topic, int count) { + for (int i = 0; i < count; i++) { + topic.increaseBookmarkCount(); + } } }