Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] Refactor/#494 성능 개선을 위한 조회 쿼리 수정 #508

Merged
merged 2 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
public interface AtlasRepository extends JpaRepository<Atlas, Long> {

boolean existsByMemberIdAndTopicId(Long memberId, Long topicId);

void deleteByMemberIdAndTopicId(Long memberId, Long topicId);

void deleteAllByMemberId(Long memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Bookmark, Long> {

boolean existsByMemberIdAndTopicId(Long memberId, Long topicId);

@Modifying(clearAutomatically = true)
void deleteAllByMemberId(Long memberId);

void deleteByMemberIdAndTopicId(Long memberId, Long topicId);
Optional<Bookmark> findByMemberIdAndTopicId(Long memberId, Long topicId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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) {
Expand All @@ -48,13 +63,11 @@ public List<MemberResponse> findAll() {
public List<TopicResponse> findAllTopicsInBookmark(AuthMember authMember) {
Member member = findMemberById(authMember.getMemberId());

List<Topic> bookMarkedTopics = findBookMarkedTopics(member);
List<Topic> topicsInAtlas = findTopicsInAtlas(member);

List<Topic> bookMarkedTopics = topicRepository.findTopicsByBookmarksMemberId(authMember.getMemberId());
return bookMarkedTopics.stream()
.map(topic -> TopicResponse.from(
topic,
isInAtlas(topicsInAtlas, topic),
isInAtlas(member.getId(), topic.getId()),
true
))
.toList();
Expand All @@ -67,51 +80,40 @@ private List<Topic> findTopicsInAtlas(Member member) {
.toList();
}

private List<Topic> findBookMarkedTopics(Member member) {
return member.getBookmarks()
.stream()
.map(Bookmark::getTopic)
.toList();
}

private boolean isInAtlas(List<Topic> topicsInAtlas, Topic topic) {
return topicsInAtlas.contains(topic);
private boolean isInAtlas(Long memberId, Long topicId) {
return atlasRepository.existsByMemberIdAndTopicId(memberId, topicId);
}

public List<TopicResponse> findAllTopicsInAtlas(AuthMember authMember) {
Member member = findMemberById(authMember.getMemberId());

List<Topic> bookMarkedTopics = findBookMarkedTopics(member);
List<Topic> 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<Topic> bookMarkedTopics, Topic topic) {
return bookMarkedTopics.contains(topic);
private boolean isInBookmark(Long memberId, Long topicId) {
return bookmarkRepository.existsByMemberIdAndTopicId(memberId, topicId);
}

public List<TopicResponse> findMyAllTopics(AuthMember authMember) {

Member member = findMemberById(authMember.getMemberId());

List<Topic> bookMarkedTopics = findBookMarkedTopics(member);
List<Topic> 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<PinResponse> findMyAllPins(AuthMember authMember) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Pin, Long> {

Expand All @@ -22,8 +24,13 @@ public interface PinRepository extends JpaRepository<Pin, Long> {
@Query("update Pin p set p.isDeleted = true where p.creator.id = :memberId")
void deleteAllByMemberId(@Param("memberId") Long memberId);

List<Pin> findAll();

@EntityGraph(attributePaths = {"location", "topic", "creator", "pinImages"})
List<Pin> findAllByTopicId(Long topicId);

@EntityGraph(attributePaths = {"location", "topic", "creator", "pinImages"})
List<Pin> findAllByCreatorId(Long creatorId);


}
Loading
Loading