Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

[BE] refactor: 글과 카테고리 EAGER 로딩 개선 #396

Merged
merged 5 commits into from
Sep 12, 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 @@ -75,13 +75,14 @@ public CategoryListResponse findAll(final Long memberId) {
return CategoryListResponse.from(categoryResponses);
}

private List<Category> sortCategory(final List<Category> categories, Category targetCategory) {
private List<Category> sortCategory(final List<Category> categories, final Category basicCategory) {
final Map<Category, Category> categoryMap = new LinkedHashMap<>();
for (final Category category : categories) {
categoryMap.put(category, category.getNextCategory());
}
final List<Category> sortedCategories = new ArrayList<>();
sortedCategories.add(targetCategory);
sortedCategories.add(basicCategory);
Category targetCategory = basicCategory;
while (Objects.nonNull(targetCategory.getNextCategory())) {
targetCategory = categoryMap.get(targetCategory);
sortedCategories.add(targetCategory);
Expand Down Expand Up @@ -113,13 +114,14 @@ private Writing findFirstWriting(final List<Writing> findWritings) {
return copy.get(FIRST_WRITING_INDEX);
}

private List<Writing> sortWriting(final List<Writing> findWritings, Writing targetWriting) {
private List<Writing> sortWriting(final List<Writing> findWritings, final Writing firstWriting) {
final Map<Writing, Writing> writingMap = new LinkedHashMap<>();
for (final Writing findWriting : findWritings) {
writingMap.put(findWriting, findWriting.getNextWriting());
}
final List<Writing> sortedWriting = new ArrayList<>();
sortedWriting.add(targetWriting);
sortedWriting.add(firstWriting);
Writing targetWriting = firstWriting;
while (Objects.nonNull(targetWriting.getNextWriting())) {
targetWriting = writingMap.get(targetWriting);
sortedWriting.add(targetWriting);
Expand All @@ -130,8 +132,8 @@ private List<Writing> sortWriting(final List<Writing> findWritings, Writing targ
public void modifyCategoryName(final Long memberId, final Long categoryId, final CategoryModifyRequest request) {
final Member findMember = findMember(memberId);
final Category findCategory = findCategory(findMember.getId(), categoryId);
validateBasicCategory(memberId, findCategory);

final Category basicCategory = findBasicCategoryByMemberId(memberId);
validateBasicCategory(basicCategory, findCategory);
final CategoryName categoryName = new CategoryName(request.categoryName());
validateCategoryName(categoryName);

Expand All @@ -141,20 +143,17 @@ public void modifyCategoryName(final Long memberId, final Long categoryId, final
public void removeCategory(final Long memberId, final Long categoryId) {
final Member findMember = findMember(memberId);
final Category findCategory = findCategory(findMember.getId(), categoryId);
validateBasicCategory(memberId, findCategory);

final List<Writing> trashedWritingInCategory = writingRepository.findAllByMemberIdAndCategoryIdAndStatusIsTrashedAndDeleted(memberId, categoryId);
final Category basicCategory = findBasicCategoryByMemberId(memberId);
validateBasicCategory(basicCategory, findCategory);
final List<Writing> trashedWritingInCategory = writingRepository.findAllByMemberIdAndCategoryIdAndStatusIsTrashedAndDeleted(memberId, categoryId);
for (final Writing writing : trashedWritingInCategory) {
writing.changeCategory(basicCategory);
}

transferToBasicCategory(memberId, findCategory);
transferToBasicCategory(basicCategory, findCategory);
deleteCategory(findCategory);
}

private void transferToBasicCategory(final Long memberId, final Category findCategory) {
final Category basicCategory = findBasicCategoryByMemberId(memberId);
private void transferToBasicCategory(final Category basicCategory, final Category findCategory) {
if (haveWritingsCategory(findCategory)) {
final List<Writing> findWritings = writingRepository.findAllByCategoryId(findCategory.getId());
final Writing firstWritingInCategory = findFirstWriting(findWritings);
Expand Down Expand Up @@ -183,7 +182,8 @@ public void modifyCategoryOrder(final Long memberId, final Long categoryId, fina
final Member member = findMember(memberId);
final Long nextCategoryId = request.nextCategoryId();
final Category source = findCategory(member.getId(), categoryId);
validateBasicCategory(member.getId(), source);
final Category basicCategory = findBasicCategoryByMemberId(member.getId());
validateBasicCategory(basicCategory, source);
deleteCategoryOrder(source);
addCategoryOrder(nextCategoryId, source, member.getId());
}
Expand All @@ -209,9 +209,7 @@ private void addCategoryOrder(final Long nextCategoryId, final Category category
}
}

private void validateBasicCategory(final Long memberId, final Category category) {
final Category basicCategory = categoryRepository.findFirstByMemberId(memberId)
.orElseThrow(IllegalStateException::new);
private void validateBasicCategory(final Category basicCategory, final Category category) {
if (basicCategory.equals(category)) {
throw new InvalidBasicCategoryException(category.getId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,14 @@ private Writing findFirstWriting(final List<Writing> findWritings) {
return copy.get(0);
}

private List<Writing> sortWriting(final List<Writing> writings, Writing targetWriting) {
private List<Writing> sortWriting(final List<Writing> writings, final Writing firstWriting) {
final Map<Writing, Writing> writingMap = new LinkedHashMap<>();
for (final Writing writing : writings) {
writingMap.put(writing, writing.getNextWriting());
}
final List<Writing> sortedWritings = new ArrayList<>();
sortedWritings.add(targetWriting);
sortedWritings.add(firstWriting);
Writing targetWriting = firstWriting;
while (Objects.nonNull(targetWriting.getNextWriting())) {
targetWriting = writingMap.get(targetWriting);
sortedWritings.add(targetWriting);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class Category {
@NotNull
@Embedded
private CategoryName categoryName;
@OneToOne(fetch = FetchType.EAGER)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "next_category_id")
private Category nextCategory;
@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -50,7 +50,6 @@ public static Category of(final CategoryName categoryName, final Member member)
return new Category(categoryName, null, member);
}


public String getCategoryNameValue() {
return categoryName.getName();
}
Expand Down Expand Up @@ -83,14 +82,4 @@ public boolean equals(final Object o) {
public int hashCode() {
return Objects.hash(id);
}

@Override
public String toString() {
return "Category{" +
"id=" + id +
", categoryName=" + categoryName +
", nextCategory=" + nextCategory +
", member=" + member +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Writing extends BaseEntity {
@OneToMany(cascade = CascadeType.PERSIST)
@JoinColumn(name = "writing_id", nullable = false, updatable = false)
private List<Block> blocks = new ArrayList<>();
@OneToOne(fetch = FetchType.EAGER)
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "next_writing_id")
private Writing nextWriting;
@NotNull
Expand Down