diff --git a/backend/src/main/java/com/mapbefine/mapbefine/atlas/domain/AtlasRepository.java b/backend/src/main/java/com/mapbefine/mapbefine/atlas/domain/AtlasRepository.java index bd37a9baa..664cdc944 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/atlas/domain/AtlasRepository.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/atlas/domain/AtlasRepository.java @@ -7,7 +7,6 @@ public interface AtlasRepository extends JpaRepository { boolean existsByMemberIdAndTopicId(Long memberId, Long topicId); - void deleteByMemberIdAndTopicId(Long memberId, Long topicId); void deleteAllByMemberId(Long memberId); 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 6f34de32e..78d7ce1f7 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 @@ -15,10 +15,11 @@ 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 java.util.NoSuchElementException; + @Service @Transactional public class BookmarkCommandService { @@ -84,7 +85,17 @@ private Member findMemberById(AuthMember authMember) { public void deleteTopicInBookmark(AuthMember authMember, Long topicId) { validateBookmarkDeletingPermission(authMember, topicId); - bookmarkRepository.deleteByMemberIdAndTopicId(authMember.getMemberId(), topicId); + Bookmark bookmark = findBookmarkByMemberIdAndTopicId(authMember.getMemberId(), topicId); + 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 + )); } private void validateBookmarkDeletingPermission(AuthMember authMember, Long topicId) { @@ -95,6 +106,7 @@ private void validateBookmarkDeletingPermission(AuthMember authMember, Long topi throw new BookmarkForbiddenException(FORBIDDEN_TOPIC_DELETE); } + @Deprecated public void deleteAllBookmarks(AuthMember authMember) { bookmarkRepository.deleteAllByMemberId(authMember.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 3ce145277..34c58d3ce 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,12 +1,17 @@ package com.mapbefine.mapbefine.bookmark.domain; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; + +import java.util.Optional; public interface BookmarkRepository extends JpaRepository { boolean existsByMemberIdAndTopicId(Long memberId, Long topicId); + @Modifying(clearAutomatically = true) void deleteAllByMemberId(Long memberId); - void deleteByMemberIdAndTopicId(Long memberId, Long topicId); + Optional findByMemberIdAndTopicId(Long memberId, Long topicId); + } diff --git a/backend/src/main/java/com/mapbefine/mapbefine/location/domain/Location.java b/backend/src/main/java/com/mapbefine/mapbefine/location/domain/Location.java index 504ec9092..098debd5e 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/location/domain/Location.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/location/domain/Location.java @@ -10,11 +10,12 @@ import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.OneToMany; -import java.util.ArrayList; -import java.util.List; import lombok.Getter; import lombok.NoArgsConstructor; +import java.util.ArrayList; +import java.util.List; + @Entity @NoArgsConstructor(access = PROTECTED) @Getter 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 df5296f96..762d77d17 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 @@ -1,8 +1,9 @@ package com.mapbefine.mapbefine.member.application; 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.BookmarkRepository; import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; import com.mapbefine.mapbefine.member.dto.response.MemberDetailResponse; @@ -11,19 +12,33 @@ import com.mapbefine.mapbefine.member.exception.MemberException.MemberNotFoundException; import com.mapbefine.mapbefine.pin.dto.response.PinResponse; 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 { private final MemberRepository memberRepository; + private final AtlasRepository atlasRepository; + private final BookmarkRepository bookmarkRepository; + + private final TopicRepository topicRepository; - public MemberQueryService(MemberRepository memberRepository) { + public MemberQueryService( + MemberRepository memberRepository, + AtlasRepository atlasRepository, + BookmarkRepository bookmarkRepository, + TopicRepository topicRepository + ) { this.memberRepository = memberRepository; + this.atlasRepository = atlasRepository; + this.bookmarkRepository = bookmarkRepository; + this.topicRepository = topicRepository; } public MemberDetailResponse findById(Long id) { @@ -48,13 +63,11 @@ public List findAll() { public List findAllTopicsInBookmark(AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List bookMarkedTopics = findBookMarkedTopics(member); - List topicsInAtlas = findTopicsInAtlas(member); - + List bookMarkedTopics = topicRepository.findTopicsByBookmarksMemberId(authMember.getMemberId()); return bookMarkedTopics.stream() .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), + isInAtlas(member.getId(), topic.getId()), true )) .toList(); @@ -67,51 +80,40 @@ private List findTopicsInAtlas(Member member) { .toList(); } - private List findBookMarkedTopics(Member member) { - return member.getBookmarks() - .stream() - .map(Bookmark::getTopic) - .toList(); - } - - private boolean isInAtlas(List topicsInAtlas, Topic topic) { - return topicsInAtlas.contains(topic); + private boolean isInAtlas(Long memberId, Long topicId) { + return atlasRepository.existsByMemberIdAndTopicId(memberId, topicId); } public List findAllTopicsInAtlas(AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List bookMarkedTopics = findBookMarkedTopics(member); List topicsInAtlas = findTopicsInAtlas(member); return topicsInAtlas.stream() .map(topic -> TopicResponse.from( topic, true, - isBookMarked(bookMarkedTopics, topic) + isInBookmark(authMember.getMemberId(), topic.getId()) )) .toList(); } - private boolean isBookMarked(List bookMarkedTopics, Topic topic) { - return bookMarkedTopics.contains(topic); + private boolean isInBookmark(Long memberId, Long topicId) { + return bookmarkRepository.existsByMemberIdAndTopicId(memberId, topicId); } public List findMyAllTopics(AuthMember authMember) { - Member member = findMemberById(authMember.getMemberId()); - - List bookMarkedTopics = findBookMarkedTopics(member); - List topicsInAtlas = findTopicsInAtlas(member); + Long memberId = authMember.getMemberId(); + Member member = findMemberById(memberId); return member.getCreatedTopics() .stream() .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(bookMarkedTopics, topic) - )) - .toList(); + isInAtlas(memberId, topic.getId()), + isInBookmark(memberId, topic.getId()) + )).toList(); } public List findMyAllPins(AuthMember authMember) { diff --git a/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/Pin.java b/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/Pin.java index 8081b9139..1496b0078 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/Pin.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/Pin.java @@ -19,12 +19,13 @@ import jakarta.persistence.OneToMany; import jakarta.persistence.PrePersist; import jakarta.persistence.PreUpdate; -import java.util.ArrayList; -import java.util.List; import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.ColumnDefault; +import java.util.ArrayList; +import java.util.List; + @Entity @NoArgsConstructor(access = PROTECTED) @Getter diff --git a/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/PinRepository.java b/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/PinRepository.java index 20ac5a4ac..6cfdcf0df 100644 --- a/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/PinRepository.java +++ b/backend/src/main/java/com/mapbefine/mapbefine/pin/domain/PinRepository.java @@ -1,12 +1,14 @@ package com.mapbefine.mapbefine.pin.domain; -import java.util.List; +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; import org.springframework.stereotype.Repository; +import java.util.List; + @Repository public interface PinRepository extends JpaRepository { @@ -22,8 +24,13 @@ public interface PinRepository extends JpaRepository { @Query("update Pin p set p.isDeleted = true where p.creator.id = :memberId") void deleteAllByMemberId(@Param("memberId") Long memberId); + List findAll(); + + @EntityGraph(attributePaths = {"location", "topic", "creator", "pinImages"}) List findAllByTopicId(Long topicId); + @EntityGraph(attributePaths = {"location", "topic", "creator", "pinImages"}) List findAllByCreatorId(Long creatorId); + } 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 5f217e71b..29b660060 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 @@ -3,9 +3,9 @@ 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.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.BookmarkRepository; import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; import com.mapbefine.mapbefine.topic.domain.Topic; @@ -14,12 +14,13 @@ 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; @Service @Transactional(readOnly = true) @@ -27,13 +28,19 @@ public class TopicQueryService { private final TopicRepository topicRepository; private final MemberRepository memberRepository; + private final AtlasRepository atlasRepository; + private final BookmarkRepository bookmarkRepository; public TopicQueryService( TopicRepository topicRepository, - MemberRepository memberRepository + MemberRepository memberRepository, + AtlasRepository atlasRepository, + BookmarkRepository bookmarkRepository ) { this.topicRepository = topicRepository; this.memberRepository = memberRepository; + this.atlasRepository = atlasRepository; + this.bookmarkRepository = bookmarkRepository; } public List findAllReadable(AuthMember authMember) { @@ -54,15 +61,12 @@ private List getGuestTopicResponses(AuthMember authMember) { private List getUserTopicResponses(AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return topicRepository.findAll().stream() .filter(authMember::canRead) .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(topicsInBookMark, topic) + isInAtlas(member.getId(), topic.getId()), + isBookMarked(member.getId(), topic.getId()) )) .toList(); } @@ -72,26 +76,12 @@ private Member findMemberById(Long id) { .orElseThrow(() -> new NoSuchElementException("findCreatorByAuthMember; member not found; id=" + id)); } - private List findTopicsInAtlas(Member member) { - return member.getAtlantes() - .stream() - .map(Atlas::getTopic) - .toList(); - } - - private List findBookMarkedTopics(Member member) { - return member.getBookmarks() - .stream() - .map(Bookmark::getTopic) - .toList(); - } - - private boolean isInAtlas(List topicsInAtlas, Topic topic) { - return topicsInAtlas.contains(topic); + private boolean isInAtlas(Long memberId, Long topicId) { + return atlasRepository.existsByMemberIdAndTopicId(memberId, topicId); } - private boolean isBookMarked(List bookMarkedTopics, Topic topic) { - return bookMarkedTopics.contains(topic); + private boolean isBookMarked(Long memberId, Long topicId) { + return bookmarkRepository.existsByMemberIdAndTopicId(memberId, topicId); } public TopicDetailResponse findDetailById(AuthMember authMember, Long topicId) { @@ -104,13 +94,10 @@ public TopicDetailResponse findDetailById(AuthMember authMember, Long topicId) { Member member = findMemberById(authMember.getMemberId()); - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return TopicDetailResponse.of( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(topicsInBookMark, topic), + isInAtlas(member.getId(), topic.getId()), + isBookMarked(member.getId(), topic.getId()), authMember.canTopicUpdate(topic) ); } @@ -142,16 +129,11 @@ public List findDetailsByIds(AuthMember authMember, List getUserTopicDetailResponses(AuthMember authMember, List topics) { - Member member = findMemberById(authMember.getMemberId()); - - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return topics.stream() .map(topic -> TopicDetailResponse.of( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(topicsInBookMark, topic), + isInAtlas(authMember.getMemberId(), topic.getId()), + isBookMarked(authMember.getMemberId(), topic.getId()), authMember.canTopicUpdate(topic) )) .toList(); @@ -190,19 +172,15 @@ public List findAllTopicsByMemberId(AuthMember authMember, Long m Member member = findMemberById(authMember.getMemberId()); - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return topicRepository.findAllByCreatorId(memberId) .stream() .filter(authMember::canRead) .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(topicsInBookMark, topic) + isInAtlas(member.getId(), topic.getId()), + isBookMarked(member.getId(), topic.getId()) )). toList(); - } public List findAllByOrderByUpdatedAtDesc(AuthMember authMember) { @@ -215,16 +193,13 @@ public List findAllByOrderByUpdatedAtDesc(AuthMember authMember) private List getUserNewestTopicResponse(AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return topicRepository.findAllByOrderByLastPinUpdatedAtDesc() .stream() .filter(authMember::canRead) .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked(topicsInBookMark, topic) + isInAtlas(member.getId(), topic.getId()), + isBookMarked(member.getId(), topic.getId()) )). toList(); } @@ -256,18 +231,15 @@ private List getGuestBestTopicResponse(AuthMember authMember) { private List getUserBestTopicResponse(AuthMember authMember) { Member member = findMemberById(authMember.getMemberId()); - List topicsInAtlas = findTopicsInAtlas(member); - List topicsInBookMark = findBookMarkedTopics(member); - return topicRepository.findAll() .stream() .filter(authMember::canRead) .sorted(Comparator.comparing(Topic::countBookmarks).reversed()) .map(topic -> TopicResponse.from( topic, - isInAtlas(topicsInAtlas, topic), - isBookMarked((topicsInBookMark), topic)) - ).toList(); + isInAtlas(member.getId(), topic.getId()), + isBookMarked(member.getId(), topic.getId()) + )).toList(); } } 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 3483e21cf..6bd7ad3b0 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 @@ -11,6 +11,7 @@ import jakarta.persistence.Column; import jakarta.persistence.Embedded; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; @@ -18,13 +19,14 @@ import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import jakarta.persistence.PrePersist; -import java.time.LocalDateTime; -import java.util.ArrayList; -import java.util.List; import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.ColumnDefault; +import java.time.LocalDateTime; +import java.util.HashSet; +import java.util.Set; + @Entity @NoArgsConstructor(access = PROTECTED) @Getter @@ -40,18 +42,26 @@ public class Topic extends BaseTimeEntity { @Embedded private TopicStatus topicStatus; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "member_id") private Member creator; @OneToMany(mappedBy = "topic") - private List permissions = new ArrayList<>(); + private Set permissions = new HashSet<>(); @OneToMany(mappedBy = "topic", cascade = CascadeType.PERSIST) - private List pins = new ArrayList<>(); + private Set pins = new HashSet<>(); - @OneToMany(mappedBy = "topic") - private List bookmarks = new ArrayList<>(); + @OneToMany(mappedBy = "topic", cascade = CascadeType.PERSIST, orphanRemoval = true) + private Set bookmarks = new HashSet<>(); + + @Column(nullable = false) + @ColumnDefault(value = "0") + private int pinCount = 0; + + @Column(nullable = false) + @ColumnDefault(value = "0") + private int bookmarkCount = 0; @Column(nullable = false) @ColumnDefault(value = "false") @@ -109,15 +119,17 @@ public void updateTopicStatus(Publicity publicity, PermissionType permissionType } public int countPins() { - return pins.size(); + return pinCount; } public void addPin(Pin pin) { pins.add(pin); + pinCount++; } public void addBookmark(Bookmark bookmark) { bookmarks.add(bookmark); + bookmarkCount++; } public void addMemberTopicPermission(Permission permission) { @@ -125,14 +137,19 @@ public void addMemberTopicPermission(Permission permission) { } public int countBookmarks() { - return bookmarks.size(); + return bookmarkCount; } public Publicity getPublicity() { return topicStatus.getPublicity(); } + public void removeImage() { this.topicInfo = topicInfo.removeImage(); } + public void removeBookmark(Bookmark bookmark) { + bookmarks.remove(bookmark); + 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 22fe220a5..28618cbba 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,21 +1,33 @@ package com.mapbefine.mapbefine.topic.domain; -import java.util.List; +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; import org.springframework.stereotype.Repository; +import java.util.List; +import java.util.Optional; + @Repository public interface TopicRepository extends JpaRepository { + @EntityGraph(attributePaths = {"creator", "permissions", "bookmarks"}) + Optional findById(Long id); + + @EntityGraph(attributePaths = {"creator", "permissions"}) List findByIdIn(List ids); boolean existsById(Long id); + @EntityGraph(attributePaths = {"creator", "permissions", "bookmarks"}) + List findAll(); + + @EntityGraph(attributePaths = {"creator", "permissions", "bookmarks"}) List findAllByOrderByLastPinUpdatedAtDesc(); + @EntityGraph(attributePaths = {"creator", "permissions", "bookmarks"}) List findAllByCreatorId(Long creatorId); @Modifying(clearAutomatically = true) @@ -26,4 +38,5 @@ public interface TopicRepository extends JpaRepository { @Query("update Topic t set t.isDeleted = true where t.creator.id = :memberId") void deleteAllByMemberId(@Param("memberId") Long memberId); + List findTopicsByBookmarksMemberId(Long memberId); } diff --git a/backend/src/test/java/com/mapbefine/mapbefine/admin/AdminIntegrationTest.java b/backend/src/test/java/com/mapbefine/mapbefine/admin/AdminIntegrationTest.java index cd2f9b3a7..109c9380f 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/admin/AdminIntegrationTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/admin/AdminIntegrationTest.java @@ -24,17 +24,15 @@ import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicRepository; import io.restassured.common.mapper.TypeRef; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Profile; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.TestPropertySource; + +import java.util.List; class AdminIntegrationTest extends IntegrationTest { @@ -52,6 +50,7 @@ class AdminIntegrationTest extends IntegrationTest { @Autowired private PinImageRepository pinImageRepository; + @Value("${security.admin.key}") private String secretKey; @@ -67,6 +66,7 @@ void setup() { topic = topicRepository.save(TopicFixture.createByName("topic", member)); location = locationRepository.save(LocationFixture.create()); pin = pinRepository.save(PinFixture.create(location, topic, member)); + topic = topicRepository.save(topic); pinImage = pinImageRepository.save(PinImageFixture.create(pin)); } @@ -124,7 +124,7 @@ void findMemberDetail_Success() { .extract() .as(new TypeRef<>() { }); - + System.out.println("====" + topic.getPinCount()); //then AdminMemberDetailResponse expected = AdminMemberDetailResponse.of( diff --git a/backend/src/test/java/com/mapbefine/mapbefine/auth/application/TokenServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/auth/application/TokenServiceTest.java index 10b782f80..527f6a2c9 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/auth/application/TokenServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/auth/application/TokenServiceTest.java @@ -9,9 +9,6 @@ import com.mapbefine.mapbefine.member.domain.Member; import com.mapbefine.mapbefine.member.domain.MemberRepository; import com.mapbefine.mapbefine.member.domain.Role; -import java.util.Calendar; -import java.util.Date; -import java.util.Optional; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; @@ -21,6 +18,10 @@ import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; import org.springframework.test.context.TestPropertySource; +import java.util.Calendar; +import java.util.Date; +import java.util.Optional; + @DataJpaTest @TestPropertySource(locations = "classpath:application.yml") class TokenServiceTest { 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 1c396822b..e1329fa8f 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 @@ -181,21 +181,24 @@ void deleteAllBookmarks_Success() { topicRepository.save(topic2); Bookmark bookmark1 = Bookmark.createWithAssociatedTopicAndMember(topic1, creatorBefore); - Bookmark bookmark2 = Bookmark.createWithAssociatedTopicAndMember(topic1, creatorBefore); + Bookmark bookmark2 = Bookmark.createWithAssociatedTopicAndMember(topic2, creatorBefore); bookmarkRepository.save(bookmark1); bookmarkRepository.save(bookmark2); + testEntityManager.flush(); + testEntityManager.clear(); + //when assertThat(creatorBefore.getBookmarks()).hasSize(2); AuthMember user = MemberFixture.createUser(creatorBefore); bookmarkCommandService.deleteAllBookmarks(user); - testEntityManager.flush(); testEntityManager.clear(); //then + assertThat(bookmarkRepository.findById(creatorBefore.getId())).isEmpty(); Member creatorAfter = memberRepository.findById(creatorBefore.getId()).get(); assertThat(creatorAfter.getBookmarks()).isEmpty(); } 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 d2778835b..2e29f50c1 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 @@ -7,10 +7,12 @@ import com.mapbefine.mapbefine.member.domain.Role; import com.mapbefine.mapbefine.topic.TopicFixture; import com.mapbefine.mapbefine.topic.domain.Topic; -import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Set; + class PermissionTest { @Test @@ -52,7 +54,7 @@ void createPermissionAssociatedWithTopicAndMember() { Permission permission = Permission.createPermissionAssociatedWithTopicAndMember(topic, member); List topicsWithPermission = member.getTopicsWithPermissions(); - List permissions = topic.getPermissions(); + Set permissions = topic.getPermissions(); // then assertThat(topicsWithPermission).hasSize(1); @@ -60,7 +62,7 @@ void createPermissionAssociatedWithTopicAndMember() { assertThat(topicsWithPermission.get(0)).usingRecursiveComparison() .ignoringFields("createdAt", "updatedAt") .isEqualTo(topic); - assertThat(permissions.get(0)).usingRecursiveComparison() + assertThat(permissions.iterator().next()).usingRecursiveComparison() .ignoringFields("createdAt", "updatedAt") .isEqualTo(permission); } diff --git a/backend/src/test/java/com/mapbefine/mapbefine/pin/domain/PinTest.java b/backend/src/test/java/com/mapbefine/mapbefine/pin/domain/PinTest.java index 56973f6e5..91756763c 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/pin/domain/PinTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/pin/domain/PinTest.java @@ -9,12 +9,14 @@ import com.mapbefine.mapbefine.member.domain.Role; import com.mapbefine.mapbefine.topic.TopicFixture; import com.mapbefine.mapbefine.topic.domain.Topic; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import java.util.List; +import java.util.Set; + class PinTest { @@ -47,7 +49,7 @@ void createPinAssociatedWithLocationAndTopicAndMember_associate_Success() { ); List pinsInLocation = location.getPins(); - List pinsInTopic = topic.getPins(); + Set pinsInTopic = topic.getPins(); List pinsInMember = member.getCreatedPins(); // then @@ -56,7 +58,7 @@ void createPinAssociatedWithLocationAndTopicAndMember_associate_Success() { assertThat(pinsInMember).hasSize(1); assertThat(pinsInLocation.get(0)).usingRecursiveComparison() .isEqualTo(pin); - assertThat(pinsInTopic.get(0)).usingRecursiveComparison() + assertThat(pinsInTopic.iterator().next()).usingRecursiveComparison() .isEqualTo(pin); assertThat(pinsInMember.get(0)).usingRecursiveComparison() .isEqualTo(pin); @@ -79,7 +81,7 @@ void copy_Success() { // when original.copyToTopic(memberForCopy, topicForCopy); - Pin actual = topicForCopy.getPins().get(0); + Pin actual = topicForCopy.getPins().iterator().next(); // then assertThat(original).usingRecursiveComparison() 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 20152e7f7..5a03a8c29 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/topic/TopicIntegrationTest.java @@ -21,7 +21,6 @@ import com.mapbefine.mapbefine.topic.domain.Topic; import com.mapbefine.mapbefine.topic.domain.TopicRepository; import com.mapbefine.mapbefine.topic.dto.request.TopicCreateRequestWithoutImage; -import com.mapbefine.mapbefine.topic.dto.request.TopicMergeRequest; import com.mapbefine.mapbefine.topic.dto.request.TopicMergeRequestWithoutImage; import com.mapbefine.mapbefine.topic.dto.request.TopicUpdateRequest; import com.mapbefine.mapbefine.topic.dto.response.TopicDetailResponse; @@ -29,9 +28,6 @@ import io.restassured.RestAssured; import io.restassured.response.ExtractableResponse; import io.restassured.response.Response; -import java.io.File; -import java.util.Collections; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -39,6 +35,10 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import java.io.File; +import java.util.Collections; +import java.util.List; + class TopicIntegrationTest extends IntegrationTest { @Autowired @@ -414,6 +414,8 @@ void findAllBestTopics_Success() { Bookmark bookmark = Bookmark.createWithAssociatedTopicAndMember(bestOneTopic, member); bookmarkRepository.save(bookmark); + topicRepository.save(bestOneTopic); + // when List expect = List.of( TopicResponse.from(bestOneTopic, Boolean.FALSE, Boolean.TRUE), @@ -460,6 +462,7 @@ void findAllByOrderByUpdatedAtDesc_Success() { ); pinRepository.saveAll(pins); + topicRepository.saveAll(List.of(topic1, topic2, topic3)); // when ExtractableResponse response = RestAssured diff --git a/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicCommandServiceTest.java b/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicCommandServiceTest.java index 904590944..38c477471 100644 --- a/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicCommandServiceTest.java +++ b/backend/src/test/java/com/mapbefine/mapbefine/topic/application/TopicCommandServiceTest.java @@ -29,13 +29,15 @@ import com.mapbefine.mapbefine.topic.dto.response.TopicDetailResponse; import com.mapbefine.mapbefine.topic.exception.TopicException.TopicBadRequestException; import com.mapbefine.mapbefine.topic.exception.TopicException.TopicForbiddenException; -import java.util.Collections; -import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import java.util.Collections; +import java.util.List; +import java.util.Set; + @ServiceTest class TopicCommandServiceTest { @@ -310,8 +312,8 @@ void copyPin_Success() { topicCommandService.copyPin(user, target.getId(), pinIds); // then - List targetPins = target.getPins(); - Pin targetPin = targetPins.get(0); + Set targetPins = target.getPins(); + Pin targetPin = targetPins.iterator().next(); Pin sourcePin = sourcePins.get(0); assertThat(targetPins).hasSize(sourcePins.size());