From c218037a5ab6196189af5919e7881bc366eb1e01 Mon Sep 17 00:00:00 2001 From: ingpyo Date: Fri, 15 Sep 2023 19:07:10 +0900 Subject: [PATCH 1/4] =?UTF-8?q?refactor:=20=EB=A7=88=EC=A7=80=EB=A7=89?= =?UTF-8?q?=EC=9D=84=20=ED=8C=90=EB=B3=84=ED=95=98=EB=8A=94=20=EA=B0=92?= =?UTF-8?q?=EC=9D=84=20null=EC=9D=B4=20=EC=95=84=EB=8B=8C=20END(-1L),DISCO?= =?UTF-8?q?NNECTION(-2L)=20=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/writing/WritingService.java | 62 +++++++++++-------- .../donggle/backend/domain/OrderStatus.java | 16 +++++ .../backend/domain/writing/Writing.java | 39 +++++------- 3 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 backend/src/main/java/org/donggle/backend/domain/OrderStatus.java diff --git a/backend/src/main/java/org/donggle/backend/application/service/writing/WritingService.java b/backend/src/main/java/org/donggle/backend/application/service/writing/WritingService.java index 3b2ee2bdc..6391f33d7 100644 --- a/backend/src/main/java/org/donggle/backend/application/service/writing/WritingService.java +++ b/backend/src/main/java/org/donggle/backend/application/service/writing/WritingService.java @@ -7,6 +7,7 @@ import org.donggle.backend.application.repository.MemberRepository; import org.donggle.backend.application.repository.WritingRepository; import org.donggle.backend.application.service.request.WritingModifyRequest; +import org.donggle.backend.domain.OrderStatus; import org.donggle.backend.domain.blog.BlogWriting; import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.member.Member; @@ -31,6 +32,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; @Service @Transactional @@ -38,6 +40,7 @@ public class WritingService { private static final String MD_FORMAT = ".md"; private static final int LAST_WRITING_FLAG = -1; + private static final int FIRST_WRITING_INDEX = 0; private final MemberRepository memberRepository; private final WritingRepository writingRepository; @@ -72,9 +75,11 @@ public Writing saveAndGetWriting(final Category findCategory, final Writing writ if (isNotEmptyCategory(findCategory)) { final Writing lastWriting = findLastWritingInCategory(findCategory.getId()); final Writing savedWriting = writingRepository.save(writing); - lastWriting.changeNextWriting(savedWriting); + lastWriting.changeNextWritingId(savedWriting.getId()); + savedWriting.changeNextWritingId(OrderStatus.END.getStatusValue()); return savedWriting; } + writing.changeNextWritingId(OrderStatus.END.getStatusValue()); return writingRepository.save(writing); } @@ -107,36 +112,43 @@ public WritingListWithCategoryResponse findWritingListByCategoryId(final Long me if (findWritings.isEmpty()) { return WritingListWithCategoryResponse.of(findCategory, Collections.emptyList()); } - final Writing firstWriting = findFirstWriting(findWritings); - final List sortedWriting = sortWriting(findWritings, firstWriting); - final List writingDetailResponses = sortedWriting.stream() + final Long firstWritingId = findFirstWritingId(findWritings); + final List sortWritingIds = sortWriting(findWritings, firstWritingId); + final Map wiritingMap = findWritings.stream() + .collect(Collectors.toMap(Writing::getId, writing -> writing)); + final List sortWriting = sortWritingIds.stream() + .map(wiritingMap::get) + .toList(); + final List writingDetailResponses = sortWriting.stream() .map(writing -> WritingDetailResponse.of(writing, convertToPublishedDetailResponses(writing.getId()))) .toList(); return WritingListWithCategoryResponse.of(findCategory, writingDetailResponses); } - private Writing findFirstWriting(final List findWritings) { - final List copy = new ArrayList<>(findWritings); - final List nextWritings = findWritings.stream() - .map(Writing::getNextWriting) + private Long findFirstWritingId(final List findWritings) { + final List copy = new ArrayList<>(findWritings.stream() + .map(Writing::getId) + .toList()); + final List nextWritings = findWritings.stream() + .map(Writing::getNextId) .toList(); copy.removeAll(nextWritings); - return copy.get(0); + return copy.get(FIRST_WRITING_INDEX); } - private List sortWriting(final List writings, final Writing firstWriting) { - final Map writingMap = new LinkedHashMap<>(); + private List sortWriting(final List writings, final Long firstWritingId) { + final Map writingMap = new LinkedHashMap<>(); for (final Writing writing : writings) { - writingMap.put(writing, writing.getNextWriting()); + writingMap.put(writing.getId(), writing.getNextId()); } - final List sortedWritings = new ArrayList<>(); - sortedWritings.add(firstWriting); - Writing targetWriting = firstWriting; - while (Objects.nonNull(targetWriting.getNextWriting())) { - targetWriting = writingMap.get(targetWriting); - sortedWritings.add(targetWriting); + final List sortedWritingIds = new ArrayList<>(); + sortedWritingIds.add(firstWritingId); + Long targetWritingId = firstWritingId; + while (!Objects.equals(writingMap.get(targetWritingId), OrderStatus.END.getStatusValue())) { + targetWritingId = writingMap.get(targetWritingId); + sortedWritingIds.add(targetWritingId); } - return sortedWritings; + return sortedWritingIds; } public void modifyWritingOrder(final Long memberId, final Long writingId, final WritingModifyRequest request) { @@ -150,12 +162,12 @@ public void modifyWritingOrder(final Long memberId, final Long writingId, final } private void deleteWritingOrder(final Writing writing) { - final Writing nextWriting = writing.getNextWriting(); - writing.changeNextWritingNull(); + final Long nextWritingId = writing.getNextId(); + writing.changeNextWritingId(null); if (isNotFirstWriting(writing.getId())) { final Writing preWriting = findPreWriting(writing.getId()); - preWriting.changeNextWriting(nextWriting); + preWriting.changeNextWritingId(nextWritingId); } } @@ -167,12 +179,12 @@ private void addWritingOrder(final Long memberId, final Long categoryId, final L } else { preWriting = findPreWriting(nextWritingId); } - preWriting.changeNextWriting(writing); + preWriting.changeNextWritingId(writing.getId()); } if (nextWritingId != LAST_WRITING_FLAG) { final Writing nextWriting = findWritingById(nextWritingId); validateAuthorization(memberId, nextWriting); - writing.changeNextWriting(nextWriting); + writing.changeNextWritingId(nextWritingId); } } @@ -183,7 +195,7 @@ private void validateAuthorization(final Long memberId, final Writing writing) { } private boolean isNotFirstWriting(final Long writingId) { - return writingRepository.countByNextWritingId(writingId) != 0; + return writingRepository.countByNextId(writingId) != 0; } private void changeCategory(final Long memberId, final Long categoryId, final Writing writing) { diff --git a/backend/src/main/java/org/donggle/backend/domain/OrderStatus.java b/backend/src/main/java/org/donggle/backend/domain/OrderStatus.java new file mode 100644 index 000000000..2f72897b2 --- /dev/null +++ b/backend/src/main/java/org/donggle/backend/domain/OrderStatus.java @@ -0,0 +1,16 @@ +package org.donggle.backend.domain; + +public enum OrderStatus { + END(-1L), + DISCONNECTION(-2L); + + private final Long statusValue; + + OrderStatus(final Long statusValue) { + this.statusValue = statusValue; + } + + public Long getStatusValue() { + return statusValue; + } +} diff --git a/backend/src/main/java/org/donggle/backend/domain/writing/Writing.java b/backend/src/main/java/org/donggle/backend/domain/writing/Writing.java index a19f53eb5..4aa71e923 100644 --- a/backend/src/main/java/org/donggle/backend/domain/writing/Writing.java +++ b/backend/src/main/java/org/donggle/backend/domain/writing/Writing.java @@ -12,20 +12,19 @@ import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; -import jakarta.persistence.OneToOne; import jakarta.validation.constraints.NotNull; import lombok.AccessLevel; import lombok.Getter; import lombok.NoArgsConstructor; -import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.BaseEntity; +import org.donggle.backend.domain.OrderStatus; +import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.member.Member; import org.donggle.backend.domain.writing.block.Block; import org.hibernate.annotations.SQLDelete; import org.hibernate.annotations.Where; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -50,9 +49,7 @@ public class Writing extends BaseEntity { @OneToMany(cascade = CascadeType.PERSIST) @JoinColumn(name = "writing_id", nullable = false, updatable = false) private List blocks = new ArrayList<>(); - @OneToOne(fetch = FetchType.LAZY) - @JoinColumn(name = "next_writing_id") - private Writing nextWriting; + private Long nextId; @NotNull @Enumerated(EnumType.STRING) private WritingStatus status = WritingStatus.ACTIVE; @@ -61,20 +58,22 @@ private Writing(final Member member, final Title title, final Category category, final List blocks, - final Writing nextWriting) { + final Long nextId) { this.member = member; this.title = title; this.category = category; this.blocks = blocks; - this.nextWriting = nextWriting; + this.nextId = nextId; } - public static Writing lastOf(final Member member, final Title title, final Category category) { - return new Writing(member, title, category, Collections.emptyList(), null); - } - public static Writing of(final Member member, final Title title, final Category category, final List blocks) { - return new Writing(member, title, category, blocks, null); + public static Writing of( + final Member member, + final Title title, + final Category category, + final List blocks + ) { + return new Writing(member, title, category, blocks, OrderStatus.DISCONNECTION.getStatusValue()); } public void updateTitle(final Title title) { @@ -83,7 +82,7 @@ public void updateTitle(final Title title) { public void restore() { this.status = WritingStatus.ACTIVE; - changeNextWritingNull(); + this.nextId = OrderStatus.END.getStatusValue(); } public String getTitleValue() { @@ -94,17 +93,13 @@ public void changeCategory(final Category category) { this.category = category; } - public void changeNextWriting(final Writing nextWriting) { - this.nextWriting = nextWriting; - } - - public void changeNextWritingNull() { - this.nextWriting = null; + public void changeNextWritingId(final Long value) { + this.nextId = value; } public void moveToTrash() { this.status = WritingStatus.TRASHED; - changeNextWritingNull(); + this.nextId = null; } public boolean isOwnedBy(final Long memberId) { @@ -136,7 +131,7 @@ public String toString() { ", title=" + title + ", category=" + category + ", blocks=" + blocks + - ", nextWriting=" + nextWriting + + ", nextId=" + nextId + ", status=" + status + '}'; } From d4845f3abbf9f0d94784f3f84afd029a7f854349 Mon Sep 17 00:00:00 2001 From: ingpyo Date: Fri, 15 Sep 2023 19:07:58 +0900 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=EA=B8=80=EC=9D=84=20=EC=A0=95?= =?UTF-8?q?=EB=A0=AC=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/CategoryService.java | 57 +++++++++++-------- 1 file changed, 33 insertions(+), 24 deletions(-) diff --git a/backend/src/main/java/org/donggle/backend/application/service/CategoryService.java b/backend/src/main/java/org/donggle/backend/application/service/CategoryService.java index b13d7cd82..7cbd382f1 100644 --- a/backend/src/main/java/org/donggle/backend/application/service/CategoryService.java +++ b/backend/src/main/java/org/donggle/backend/application/service/CategoryService.java @@ -6,6 +6,7 @@ import org.donggle.backend.application.repository.WritingRepository; import org.donggle.backend.application.service.request.CategoryAddRequest; import org.donggle.backend.application.service.request.CategoryModifyRequest; +import org.donggle.backend.domain.OrderStatus; import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.category.CategoryName; import org.donggle.backend.domain.member.Member; @@ -29,6 +30,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.stream.Collectors; @Service @Transactional @@ -97,38 +99,45 @@ public CategoryWritingsResponse findAllWritings(final Long memberId, final Long if (findWritings.isEmpty()) { return CategoryWritingsResponse.of(findCategory, Collections.emptyList()); } - final Writing firstWriting = findFirstWriting(findWritings); - final List sortedWriting = sortWriting(findWritings, firstWriting); - final List writingSimpleResponses = sortedWriting.stream() + final Long firstWritingId = findFirstWritingId(findWritings); + final List sortWritingIds = sortWriting(findWritings, firstWritingId); + final Map wiritingMap = findWritings.stream() + .collect(Collectors.toMap(Writing::getId, writing -> writing)); + final List sortWriting = sortWritingIds.stream() + .map(wiritingMap::get) + .toList(); + final List writingSimpleResponses = sortWriting.stream() .map(WritingSimpleResponse::from) .toList(); return CategoryWritingsResponse.of(findCategory, writingSimpleResponses); } - private Writing findFirstWriting(final List findWritings) { - final List copy = new ArrayList<>(findWritings); - final List nextWritings = findWritings.stream() - .map(Writing::getNextWriting) + private List sortWriting(final List writings, final Long firstWritingId) { + final Map writingMap = new LinkedHashMap<>(); + for (final Writing writing : writings) { + writingMap.put(writing.getId(), writing.getNextId()); + } + final List sortedWritingIds = new ArrayList<>(); + sortedWritingIds.add(firstWritingId); + Long targetWritingId = firstWritingId; + while (!Objects.equals(writingMap.get(targetWritingId), OrderStatus.END.getStatusValue())) { + targetWritingId = writingMap.get(targetWritingId); + sortedWritingIds.add(targetWritingId); + } + return sortedWritingIds; + } + + private Long findFirstWritingId(final List findWritings) { + final List copy = new ArrayList<>(findWritings.stream() + .map(Writing::getId) + .toList()); + final List nextWritings = findWritings.stream() + .map(Writing::getNextId) .toList(); copy.removeAll(nextWritings); return copy.get(FIRST_WRITING_INDEX); } - private List sortWriting(final List findWritings, final Writing firstWriting) { - final Map writingMap = new LinkedHashMap<>(); - for (final Writing findWriting : findWritings) { - writingMap.put(findWriting, findWriting.getNextWriting()); - } - final List sortedWriting = new ArrayList<>(); - sortedWriting.add(firstWriting); - Writing targetWriting = firstWriting; - while (Objects.nonNull(targetWriting.getNextWriting())) { - targetWriting = writingMap.get(targetWriting); - sortedWriting.add(targetWriting); - } - return sortedWriting; - } - public void modifyCategoryName(final Long memberId, final Long categoryId, final CategoryModifyRequest request) { final Member findMember = findMember(memberId); final Category findCategory = findCategory(findMember.getId(), categoryId); @@ -156,10 +165,10 @@ public void removeCategory(final Long memberId, final Long categoryId) { private void transferToBasicCategory(final Category basicCategory, final Category findCategory) { if (haveWritingsCategory(findCategory)) { final List findWritings = writingRepository.findAllByCategoryId(findCategory.getId()); - final Writing firstWritingInCategory = findFirstWriting(findWritings); + final Long firstWritingId = findFirstWritingId(findWritings); if (haveWritingsCategory(basicCategory)) { final Writing lastWritingInBasicCategory = findLastWritingInCategory(basicCategory); - lastWritingInBasicCategory.changeNextWriting(firstWritingInCategory); + lastWritingInBasicCategory.changeNextWritingId(firstWritingId); } findWritings.forEach(writing -> writing.changeCategory(basicCategory)); } From 3fc21aacf48c6761cb01b3f4de2cfe02f9b063d0 Mon Sep 17 00:00:00 2001 From: ingpyo Date: Fri, 15 Sep 2023 19:09:35 +0900 Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=EA=B8=80=EC=9D=84=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=ED=95=A0=EB=95=8C=20=EC=A0=84=EB=9E=B5=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/service/TrashService.java | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/org/donggle/backend/application/service/TrashService.java b/backend/src/main/java/org/donggle/backend/application/service/TrashService.java index 2d7669423..75357a31d 100644 --- a/backend/src/main/java/org/donggle/backend/application/service/TrashService.java +++ b/backend/src/main/java/org/donggle/backend/application/service/TrashService.java @@ -2,9 +2,9 @@ import lombok.RequiredArgsConstructor; import org.donggle.backend.application.repository.WritingRepository; +import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.writing.Writing; import org.donggle.backend.exception.notfound.DeleteWritingNotFoundException; -import org.donggle.backend.exception.notfound.RestoreWritingNotFoundException; import org.donggle.backend.exception.notfound.WritingNotFoundException; import org.donggle.backend.ui.response.TrashResponse; import org.springframework.stereotype.Service; @@ -33,36 +33,28 @@ public void trashWritings(final Long memberId, final List writingIds) { return findWriting; }) .forEach(writing -> { - final Writing nextWriting = writing.getNextWriting(); + final Long nextWritingId = writing.getNextId(); writing.moveToTrash(); writingRepository.findPreWritingByWritingId(writing.getId()) - .ifPresent(preWriting -> preWriting.changeNextWriting(nextWriting)); + .ifPresent(preWriting -> preWriting.changeNextWritingId(nextWritingId)); }); } public void deleteWritings(final Long memberId, final List writingIds) { - writingIds.stream() - .map(writingId -> writingRepository.findByMemberIdAndWritingIdAndStatusIsNotDeleted(memberId, writingId) - .orElseThrow(() -> new DeleteWritingNotFoundException(writingId))) - .forEach(writing -> { - final Writing nextWriting = writing.getNextWriting(); - writing.changeNextWritingNull(); - writingRepository.delete(writing); - writingRepository.findPreWritingByWritingId(writing.getId()) - .ifPresent(preWriting -> preWriting.changeNextWriting(nextWriting)); - }); + final List trashedWritings = writingRepository.findTrashedWritingsByIds(memberId, writingIds); + writingRepository.deleteAll(trashedWritings); } public void restoreWritings(final Long memberId, final List writingIds) { - writingIds.stream() - .map(writingId -> writingRepository.findByMemberIdAndWritingIdAndStatusIsTrashed(memberId, writingId) - .orElseThrow(() -> new RestoreWritingNotFoundException(writingId))) - .forEach(writing -> { - writingRepository.findLastWritingByCategoryId(writing.getCategory().getId()) - .ifPresent(lastWriting -> lastWriting.changeNextWriting(writing)); - writing.restore(); - } - ); + final List trashedWritings = writingRepository.findTrashedWritingsByIds(memberId, writingIds); + for (final Writing trashedWriting : trashedWritings) { + final Category category = trashedWriting.getCategory(); + if (writingRepository.findLastWritingByCategoryId(category.getId()).isPresent()) { + final Writing writing = writingRepository.findLastWritingByCategoryId(category.getId()).get(); + writing.changeNextWritingId(trashedWriting.getId()); + } + trashedWriting.restore(); + } } private void validateAuthorization(final Long memberId, final Writing writing) { From 6c9e25caa4102280e3b59bc3626e6abeab67c3a6 Mon Sep 17 00:00:00 2001 From: ingpyo Date: Fri, 15 Sep 2023 19:09:52 +0900 Subject: [PATCH 4/4] =?UTF-8?q?refactor:=20=EB=A1=9C=EC=A7=81=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=9D=98=20=EB=94=B0=EB=A5=B8=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EC=BD=94=EB=93=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/WritingRepository.java | 17 +++++-- .../backend/config/InitLocalAndTestData.java | 4 +- .../service/CategoryServiceTest.java | 13 +---- .../application/service/TrashServiceTest.java | 47 ++++++++----------- 4 files changed, 36 insertions(+), 45 deletions(-) diff --git a/backend/src/main/java/org/donggle/backend/application/repository/WritingRepository.java b/backend/src/main/java/org/donggle/backend/application/repository/WritingRepository.java index 42051bb40..0767d9ccc 100644 --- a/backend/src/main/java/org/donggle/backend/application/repository/WritingRepository.java +++ b/backend/src/main/java/org/donggle/backend/application/repository/WritingRepository.java @@ -1,7 +1,6 @@ package org.donggle.backend.application.repository; import org.donggle.backend.domain.writing.Writing; -import org.springframework.data.jpa.repository.EntityGraph; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; @@ -14,15 +13,15 @@ public interface WritingRepository extends JpaRepository { int countByCategoryId(Long id); - int countByNextWritingId(Long nextWritingId); + int countByNextId(Long nextWritingId); @Query("select w from Writing w " + "where w.category.id = :categoryId and " + - "w.nextWriting is null") + "w.nextId = -1") Optional findLastWritingByCategoryId(@Param("categoryId") final Long categoryId); @Query("select w from Writing w " + - "where w.nextWriting.id = :writingId") + "where w.nextId = :writingId") Optional findPreWritingByWritingId(@Param("writingId") final Long writingId); @Query("select w from Writing w join fetch w.blocks where w.id = :writingId") @@ -33,6 +32,11 @@ public interface WritingRepository extends JpaRepository { "w.status = 'TRASHED'", nativeQuery = true) List findAllByMemberIdAndStatusIsTrashed(@Param("memberId") final Long memberId); + @Query(value = "select * from writing w " + + "where w.member_id = :memberId and " + + "w.status = 'TRASHED' IN (:writingIds)", nativeQuery = true) + List findTrashedWritingsByIds(@Param("memberId") final Long memberId, @Param("writingIds") final List writingIds); + @Query(value = "select * from writing w " + "where w.member_id = :memberId and " + "w.category_id = :categoryId and " + @@ -50,4 +54,9 @@ public interface WritingRepository extends JpaRepository { "w.id = :writingId and " + "w.status != 'DELETED'", nativeQuery = true) Optional findByMemberIdAndWritingIdAndStatusIsNotDeleted(@Param("memberId") final Long memberId, @Param("writingId") final Long writingId); + + @Query(value = "update writing " + + "set next_writing_id = newWritingId " + + "where id = lastWritingId ", nativeQuery = true) + void updateNextId(Long lastWritingId, Long newWritingId); } diff --git a/backend/src/main/java/org/donggle/backend/config/InitLocalAndTestData.java b/backend/src/main/java/org/donggle/backend/config/InitLocalAndTestData.java index b18a13dc5..4ac10536a 100644 --- a/backend/src/main/java/org/donggle/backend/config/InitLocalAndTestData.java +++ b/backend/src/main/java/org/donggle/backend/config/InitLocalAndTestData.java @@ -6,6 +6,7 @@ import org.donggle.backend.application.repository.MemberCredentialsRepository; import org.donggle.backend.application.repository.MemberRepository; import org.donggle.backend.application.repository.WritingRepository; +import org.donggle.backend.domain.OrderStatus; import org.donggle.backend.domain.blog.Blog; import org.donggle.backend.domain.blog.BlogType; import org.donggle.backend.domain.category.Category; @@ -60,7 +61,7 @@ public void init() { blogRepository.save(new Blog(BlogType.MEDIUM)); blogRepository.save(new Blog(BlogType.TISTORY)); - writingRepository.save(Writing.of( + final Writing savedWriting = writingRepository.save(Writing.of( savedMember, new Title("테스트 글"), savedCategory, @@ -74,6 +75,7 @@ public void init() { ) ) )); + savedWriting.changeNextWritingId(OrderStatus.END.getStatusValue()); } } } diff --git a/backend/src/test/java/org/donggle/backend/application/service/CategoryServiceTest.java b/backend/src/test/java/org/donggle/backend/application/service/CategoryServiceTest.java index 144b0fdfe..d29016a1d 100644 --- a/backend/src/test/java/org/donggle/backend/application/service/CategoryServiceTest.java +++ b/backend/src/test/java/org/donggle/backend/application/service/CategoryServiceTest.java @@ -119,27 +119,16 @@ void findAll() { void removeCategory() { //given final Long secondId = categoryService.addCategory(1L, new CategoryAddRequest("두 번째 카테고리")); - final Category secondCategory = categoryRepository.findById(secondId).get(); - writingRepository.findById(1L).get() - .changeCategory(secondCategory); - writingRepository.delete(writingRepository.findById(1L).get()); //when categoryService.removeCategory(1L, secondId); final List categories = categoryRepository.findAllByMemberId(1L); - categoryRepository.flush(); - writingRepository.flush(); - - final Writing writing = writingRepository.findAllByMemberIdAndCategoryIdAndStatusIsTrashedAndDeleted(1L, 1L).get(0); - //then assertAll( () -> assertThat(categories).hasSize(1), () -> assertThat(categories.get(0).getCategoryName()).isEqualTo(new CategoryName("기본")), - () -> assertThat(categories.get(0).getNextCategory()).isNull(), - () -> assertThat(writing.getCategory()).isEqualTo(categories.get(0)), - () -> assertThat(writing.getNextWriting()).isNull() + () -> assertThat(categories.get(0).getNextCategory()).isNull() ); } diff --git a/backend/src/test/java/org/donggle/backend/application/service/TrashServiceTest.java b/backend/src/test/java/org/donggle/backend/application/service/TrashServiceTest.java index f043b2444..31159ef80 100644 --- a/backend/src/test/java/org/donggle/backend/application/service/TrashServiceTest.java +++ b/backend/src/test/java/org/donggle/backend/application/service/TrashServiceTest.java @@ -4,6 +4,7 @@ import org.donggle.backend.application.repository.CategoryRepository; import org.donggle.backend.application.repository.MemberRepository; import org.donggle.backend.application.repository.WritingRepository; +import org.donggle.backend.domain.OrderStatus; import org.donggle.backend.domain.category.Category; import org.donggle.backend.domain.member.Member; import org.donggle.backend.domain.writing.Title; @@ -16,6 +17,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.annotation.DirtiesContext; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -40,15 +42,15 @@ void setUp() { final Writing writing1 = writingRepository.findById(1L).get(); final Member member = memberRepository.findById(1L).get(); final Category category = categoryRepository.findFirstByMemberId(1L).get(); - final Writing writing2 = Writing.lastOf(member, new Title("test2"), category); - final Writing writing3 = Writing.lastOf(member, new Title("test3"), category); - final Writing writing4 = Writing.lastOf(member, new Title("test4"), category); + final Writing writing2 = lastOf(member, new Title("test2"), category); + final Writing writing3 = lastOf(member, new Title("test3"), category); + final Writing writing4 = lastOf(member, new Title("test4"), category); writingRepository.save(writing2); writingRepository.save(writing3); writingRepository.save(writing4); - writing1.changeNextWriting(writing2); - writing2.changeNextWriting(writing3); - writing3.changeNextWriting(writing4); + writing1.changeNextWritingId(writing2.getId()); + writing2.changeNextWritingId(writing3.getId()); + writing3.changeNextWritingId(writing4.getId()); } @Test @@ -62,12 +64,7 @@ void findTrashedWritingList() { final TrashResponse trashResponse = trashService.findTrashedWritingList(1L); //then - assertAll( - () -> assertThat(trashResponse.writings()).hasSize(1), - () -> assertThat(trashResponse.writings()).usingRecursiveComparison() - .comparingOnlyFields("id") - .isEqualTo(writing.getId()) - ); + assertAll(() -> assertThat(trashResponse.writings()).hasSize(1), () -> assertThat(trashResponse.writings()).usingRecursiveComparison().comparingOnlyFields("id").isEqualTo(writing.getId())); } @Test @@ -79,10 +76,7 @@ void changeOrderOfTrashedWritingsMiddle() { //then final List writings = writingRepository.findAllByCategoryId(1L); - assertAll( - () -> assertThat(writings).hasSize(3), - () -> assertThat(writings.get(0).getNextWriting()).isEqualTo(writings.get(1)) - ); + assertAll(() -> assertThat(writings).hasSize(3), () -> assertThat(writings.get(0).getNextId()).isEqualTo(writings.get(1).getId())); } @Test @@ -90,7 +84,7 @@ void changeOrderOfTrashedWritingsMiddle() { void changeOrderOfTrashedWritingsLast() { //given final Writing writing = writingRepository.findById(4L).get(); - + //when trashService.trashWritings(1L, List.of(writing.getId())); @@ -98,7 +92,7 @@ void changeOrderOfTrashedWritingsLast() { final List writings = writingRepository.findAllByCategoryId(1L); assertAll( () -> assertThat(writings).hasSize(3), - () -> assertThat(writings.get(0).getNextWriting()).isEqualTo(writings.get(1)) + () -> assertThat(writings.get(0).getNextId()).isEqualTo(writings.get(1).getId()) ); } @@ -112,18 +106,15 @@ void changeOrderOfTrashedWritingsFirst() { //then final List writings = writingRepository.findAllByCategoryId(1L); - assertAll( - () -> assertThat(writings).hasSize(3), - () -> assertThat(writings.get(0).getNextWriting()).isEqualTo(writings.get(1)) - ); + assertAll(() -> assertThat(writings).hasSize(3), () -> assertThat(writings.get(0).getNextId()).isEqualTo(writings.get(1).getId())); } @Test @DisplayName("글을 삭제한다.") void deleteWritings() { //given - //when + trashService.trashWritings(1L, List.of(1L)); trashService.deleteWritings(1L, List.of(1L)); //then @@ -143,10 +134,10 @@ void restoreWritings() { //then final List writings = writingRepository.findAllByCategoryId(1L); final Optional restoredWriting = writingRepository.findById(1L); - assertAll( - () -> assertThat(restoredWriting).isNotEmpty(), - () -> assertThat(writings).hasSize(4), - () -> assertThat(restoredWriting.get().getNextWriting()).isNull() - ); + assertAll(() -> assertThat(restoredWriting).isNotEmpty(), () -> assertThat(writings).hasSize(4), () -> assertThat(restoredWriting.get().getNextId()).isEqualTo(OrderStatus.END.getStatusValue())); + } + + public Writing lastOf(final Member member, final Title title, final Category category) { + return Writing.of(member, title, category, Collections.emptyList()); } }