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

[LIME-61] bucket - bucketItem - item 양방향 연관관계 제거 #29

Merged
merged 10 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -55,8 +55,7 @@ public void modifyBucket(
) {
validateEmptyRegistry(registry);

Long memberId = memberUtils.getCurrentMemberId();
bucketModifier.modify(memberId, bucketId, bucketInfo, registry);
bucketModifier.modify(bucketId, bucketInfo, registry);
}

/** 버킷 삭제 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,20 @@
import com.programmers.lime.domains.bucket.domain.Bucket;
import com.programmers.lime.domains.bucket.domain.BucketInfo;
import com.programmers.lime.domains.bucket.domain.BucketItem;
import com.programmers.lime.domains.bucket.repository.BucketItemRepository;
import com.programmers.lime.domains.bucket.repository.BucketRepository;
import com.programmers.lime.domains.item.domain.Item;
import com.programmers.lime.domains.item.implementation.ItemReader;
import com.programmers.lime.error.BusinessException;
import com.programmers.lime.error.ErrorCode;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class BucketAppender {

private final BucketItemRepository bucketItemRepository;
private final BucketRepository bucketRepository;
private final ItemReader itemReader;

Expand All @@ -30,24 +34,31 @@ public Long append(
final BucketInfo bucketInfo,
final ItemIdRegistry registry
) {
List<BucketItem> bucketItems = createBucketItems(registry);

Bucket bucket = new Bucket(bucketInfo, memberId);
bucketItems.forEach(bucket::addBucketItem);
Long bucketId = bucketRepository.save(bucket).getId();

List<BucketItem> bucketItems = createBucketItems(registry,bucketId);

bucketItemRepository.saveAll(bucketItems);

return bucketRepository.save(bucket).getId();
return bucketId;
}

/** 버킷 아이템 생성 */
public List<BucketItem> createBucketItems(final ItemIdRegistry registry) {
return registry.itemIds().stream()
.distinct()
.map(itemId -> {
Item item = itemReader.read(itemId);
BucketItem bucketItem = new BucketItem(item);

return bucketItem;
})
.collect(Collectors.toList());
public List<BucketItem> createBucketItems(
final ItemIdRegistry registry,
final Long bucketId
) {
if(itemReader.existsAll(registry.itemIds())){
return registry.itemIds().stream()
.distinct()
.map(itemId -> {
BucketItem bucketItem = new BucketItem(itemId, bucketId);

return bucketItem;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;

return new BucketItem(itemId, bucketId);

이렇게 작성하는 것은 어떤가요??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

d827982 반영 완!

}).toList();
}

throw new BusinessException(ErrorCode.ITEM_NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.programmers.lime.domains.bucket.domain.Bucket;
import com.programmers.lime.domains.bucket.domain.BucketInfo;
import com.programmers.lime.domains.bucket.domain.BucketItem;
import com.programmers.lime.domains.bucket.repository.BucketItemRepository;
import com.programmers.lime.domains.bucket.repository.BucketRepository;

import lombok.RequiredArgsConstructor;
Expand All @@ -20,25 +21,23 @@ public class BucketModifier {
private final BucketAppender bucketAppender;
private final BucketRemover bucketRemover;
private final BucketReader bucketReader;
private final BucketItemRepository bucketItemRepository;
private final BucketRepository bucketRepository;

/** 버킷 수정 */
@Transactional
public void modify(
final Long memberId,
final Long bucketId,
final BucketInfo bucketInfo,
final ItemIdRegistry registry
) {
Bucket bucket = bucketReader.read(bucketId, memberId);
Bucket bucket = bucketReader.read(bucketId);
bucket.modifyBucket(bucketInfo);

bucket.removeBucketItems();
bucketRemover.removeBucketItems(bucket.getId());

List<BucketItem> bucketItems = bucketAppender.createBucketItems(registry);
bucket.modifyBucket(bucketInfo, bucket.getMemberId());
bucketItems.forEach(bucket::addBucketItem);
bucketRemover.removeBucketItems(bucketId);
List<BucketItem> bucketItems = bucketAppender.createBucketItems(registry, bucketId);

bucketRepository.save(bucket);
bucketItemRepository.saveAll(bucketItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public List<Bucket> readByMemberId(final Long memberId) {
}

/** 버킷 아이템 정보 조회 */
public List<BucketItem> bucketItemRead(final Long bucketId) {
return bucketItemRepository.findByBucketId(bucketId);
public List<BucketItem> readBucketItems(final Long bucketId) {
return bucketItemRepository.findAllByBucketId(bucketId);
}

/** 버킷 조회와 수정을 위한 MemberItem 커서 조회 */
Expand All @@ -81,17 +81,14 @@ public CursorSummary<BucketMemberItemSummary> readByMemberItems(
List<Long> itemIdsFromMemberItem = memberItemReader.readByMemberId(memberId).stream()
.map(memberItem -> memberItem.getItem().getId())
.toList();
List<Long> itemIdsFromBucketItem = null;
List<Long> itemIds = null;

if (bucketId != null) {
Bucket bucket = read(bucketId, memberId);
itemIdsFromBucketItem = bucket.getBucketItems().stream()
.map(bucketItem -> bucketItem.getItem().getId())
.toList();
itemIds = getItemIds(bucketId);
}

List<BucketMemberItemSummary> summaries = memberItemReader.readBucketMemberItem(
itemIdsFromBucketItem,
itemIds,
itemIdsFromMemberItem,
hobby,
memberId,
Expand Down Expand Up @@ -123,17 +120,20 @@ public CursorSummary<BucketSummary> readByCursor(
/** 버킷 정보 상세 조회 */
public BucketGetServiceResponse readDetail(final Long bucketId) {
Bucket bucket = read(bucketId);
List<ItemInfo> itemInfos = bucket.getBucketItems().stream()
.map(bucketItem -> itemReader.read(bucketItem.getItem().getId()))
List<Long> itemIds = getItemIds(bucketId);

List<ItemInfo> itemInfos = itemReader.readAll(itemIds).stream()
.map(item -> ItemInfo.from(item))
.toList();

int totalPrice = itemInfos.stream()
.mapToInt(ItemInfo::price)
.sum();

return BucketGetServiceResponse.of(bucket, totalPrice, itemInfos);
}


/** 마이페이지를 위한 버킷 프로필 조회 (3개) */
public List<BucketProfile> readBucketProfile(final Long memberId) {
List<Bucket> buckets = readByMemberId(memberId);
Expand All @@ -146,7 +146,7 @@ private List<BucketProfile> selectBucketProfile(final List<Bucket> buckets) {

return selectedBuckets.stream()
.map(bucket -> {
List<String> itemImages = extractBucketItemImages(bucket);
List<String> itemImages = extractBucketItemImages(bucket.getId());

return BucketProfile.of(bucket, itemImages);
})
Expand All @@ -164,10 +164,12 @@ private List<Bucket> selectBucketsByHobby(final List<Bucket> selectedBuckets) {
.toList();
}

private List<String> extractBucketItemImages(final Bucket bucket) {
return bucket.getBucketItems().stream()
private List<String> extractBucketItemImages(final Long bucketId) {
List<BucketItem> bucketItems = readBucketItems(bucketId);

return bucketItems.stream()
.limit(ITEM_IMAGE_LIMIT)
.map(BucketItem::getItem)
.map(bucketItem -> itemReader.read(bucketItem.getItemId()))
.map(Item::getImage)
.toList();
}
Expand All @@ -177,6 +179,14 @@ private int getPageSize(final CursorPageParameters parameters) {
return pageSize;
}

public List<Long> getItemIds(final Long bucketId) {
List<BucketItem> bucketItems = bucketItemRepository.findAllByBucketId(bucketId);

return bucketItems.stream()
.map(bucketItem -> bucketItem.getItemId())
.toList();
}

public int countByMemberIdAndHobby(
final Long memberId,
final Hobby hobby
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ public void remove(
final Long bucketId,
final Long memberId
) {
List<BucketItem> bucketItems = bucketReader.readBucketItems(bucketId);
Bucket bucket = bucketReader.read(bucketId, memberId);

bucketItemRepository.deleteAll(bucketItems);
bucketRepository.delete(bucket);
}

/** 버킷 아이템 삭제 */
public void removeBucketItems(final Long bucketId) {
List<BucketItem> bucketItems = bucketReader.bucketItemRead(bucketId);
List<BucketItem> bucketItems = bucketReader.readBucketItems(bucketId);

bucketItems.forEach(bucketItem -> bucketItemRepository.delete(bucketItem));
bucketItemRepository.deleteAll(bucketItems);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.programmers.lime.domains.bucket.domain.Bucket;
import com.programmers.lime.domains.bucket.domain.BucketItem;
import com.programmers.lime.domains.bucket.implementation.BucketReader;
import com.programmers.lime.domains.feed.domain.Feed;
import com.programmers.lime.domains.feed.domain.FeedItem;
Expand Down Expand Up @@ -38,9 +39,7 @@ public Long append(
final FeedCreateServiceRequest request
) {
Bucket bucket = bucketReader.read(request.bucketId(), memberId);
List<Long> itemIds = bucket.getBucketItems().stream()
.map(bucketItem -> bucketItem.getItem().getId())
.toList();
List<Long> itemIds = bucketReader.getItemIds(bucket.getId());
List<FeedItem> feedItems = createFeedItems(itemIds);

Feed feed = Feed.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@ public int getItemTotalCountByKeyword(final String keyword, final Hobby hobby) {

return itemRepository.countItemByKeywordAndHobby(trimmedKeyword, hobby);
}

public boolean existsAll(final List<Long> itemIds) {
return itemRepository.existsAllByIdIn(itemIds);
}

public List<Item> readAll(final List<Long> itemIds) {
return itemRepository.findAllByIdIn(itemIds);

}
}