From b1fb3e113bce2c28fb6c4dd94ea018eb3b49d2e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9D=B4=EC=A7=80=EC=9A=B0?= <49433615+LJW25@users.noreply.github.com> Date: Wed, 27 Sep 2023 15:32:34 +0900 Subject: [PATCH] =?UTF-8?q?Shared=20Status=20=EC=9D=B4=EB=8F=99=20(#639)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: shared_status 이동 쿼리 작성 Co-authored-by: hgo641 * refactor: shared_status 이동에 따른 프로덕션 코드 수정 * refactor: boolean primitive 타입으로 변경 * fix: if 조건 수정 --------- Co-authored-by: hgo641 --- .../java/hanglog/share/domain/SharedTrip.java | 19 ++------ .../dto/response/SharedTripCodeResponse.java | 4 +- .../share/service/SharedTripService.java | 4 +- .../main/java/hanglog/trip/domain/Trip.java | 24 ++++++++-- .../V8__Move_shared_status_to_trip.sql | 10 ++++ .../expense/fixture/TripExpenseFixture.java | 2 + .../hanglog/share/fixture/ShareFixture.java | 46 +++++++++++++++---- .../SharedTripControllerTest.java | 4 +- .../share/service/SharedTripServiceTest.java | 6 +-- .../hanglog/trip/fixture/DayLogFixture.java | 2 + .../hanglog/trip/fixture/TripFixture.java | 3 ++ .../hanglog/trip/service/TripServiceTest.java | 5 ++ 12 files changed, 92 insertions(+), 37 deletions(-) create mode 100644 backend/src/main/resources/db/migration/V8__Move_shared_status_to_trip.sql diff --git a/backend/src/main/java/hanglog/share/domain/SharedTrip.java b/backend/src/main/java/hanglog/share/domain/SharedTrip.java index ded58e16f..e60571779 100644 --- a/backend/src/main/java/hanglog/share/domain/SharedTrip.java +++ b/backend/src/main/java/hanglog/share/domain/SharedTrip.java @@ -1,20 +1,15 @@ package hanglog.share.domain; import static hanglog.global.exception.ExceptionCode.FAIL_SHARE_CODE_HASH; -import static hanglog.share.domain.type.SharedStatusType.SHARED; -import static hanglog.share.domain.type.SharedStatusType.UNSHARED; -import static jakarta.persistence.EnumType.STRING; import static jakarta.persistence.FetchType.LAZY; import static jakarta.persistence.GenerationType.IDENTITY; import static lombok.AccessLevel.PROTECTED; import hanglog.global.BaseEntity; import hanglog.global.exception.InvalidDomainException; -import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.Trip; import jakarta.persistence.Column; import jakarta.persistence.Entity; -import jakarta.persistence.Enumerated; import jakarta.persistence.GeneratedValue; import jakarta.persistence.Id; import jakarta.persistence.Index; @@ -53,12 +48,8 @@ public class SharedTrip extends BaseEntity { @Column(nullable = false) private String sharedCode; - @Column(nullable = false) - @Enumerated(value = STRING) - private SharedStatusType sharedStatus; - public static SharedTrip createdBy(final Trip trip) { - return new SharedTrip(null, trip, createCode(trip.getId()), SHARED); + return new SharedTrip(null, trip, createCode(trip.getId())); } private static String createCode(final Long tripId) { @@ -78,11 +69,11 @@ private static String bytesToHex(final byte[] bytes) { .collect(Collectors.joining()); } - public void updateSharedStatus(final Boolean sharedStatus) { - this.sharedStatus = SharedStatusType.mappingType(sharedStatus); + public void changeSharedStatus(final Boolean sharedStatus) { + this.trip.changeSharedStatus(sharedStatus); } - public boolean isUnShared() { - return sharedStatus == UNSHARED; + public boolean isShared() { + return this.trip.isShared(); } } diff --git a/backend/src/main/java/hanglog/share/dto/response/SharedTripCodeResponse.java b/backend/src/main/java/hanglog/share/dto/response/SharedTripCodeResponse.java index 86ea04c85..9e0f1404b 100644 --- a/backend/src/main/java/hanglog/share/dto/response/SharedTripCodeResponse.java +++ b/backend/src/main/java/hanglog/share/dto/response/SharedTripCodeResponse.java @@ -1,7 +1,5 @@ package hanglog.share.dto.response; -import static hanglog.share.domain.type.SharedStatusType.UNSHARED; - import hanglog.share.domain.SharedTrip; import lombok.Getter; import lombok.RequiredArgsConstructor; @@ -13,7 +11,7 @@ public class SharedTripCodeResponse { private final String sharedCode; public static SharedTripCodeResponse of(final SharedTrip sharedTrip) { - if (sharedTrip.getSharedStatus() == UNSHARED) { + if (!sharedTrip.isShared()) { return new SharedTripCodeResponse(null); } return new SharedTripCodeResponse(sharedTrip.getSharedCode()); diff --git a/backend/src/main/java/hanglog/share/service/SharedTripService.java b/backend/src/main/java/hanglog/share/service/SharedTripService.java index 9bc036ca4..94de54f14 100644 --- a/backend/src/main/java/hanglog/share/service/SharedTripService.java +++ b/backend/src/main/java/hanglog/share/service/SharedTripService.java @@ -35,7 +35,7 @@ public Long getTripId(final String sharedCode) { final SharedTrip sharedTrip = sharedTripRepository.findBySharedCode(sharedCode) .orElseThrow(() -> new BadRequestException(NOT_FOUND_SHARED_CODE)); - if (sharedTrip.isUnShared()) { + if (!sharedTrip.isShared()) { throw new BadRequestException(INVALID_SHARE_CODE); } @@ -65,7 +65,7 @@ public SharedTripCodeResponse updateSharedTripStatus( final SharedTrip sharedTrip = Optional.ofNullable(trip.getSharedTrip()) .orElseGet(() -> SharedTrip.createdBy(trip)); - sharedTrip.updateSharedStatus(sharedTripStatusRequest.getSharedStatus()); + sharedTrip.changeSharedStatus(sharedTripStatusRequest.getSharedStatus()); sharedTripRepository.save(sharedTrip); return SharedTripCodeResponse.of(sharedTrip); } diff --git a/backend/src/main/java/hanglog/trip/domain/Trip.java b/backend/src/main/java/hanglog/trip/domain/Trip.java index 6bfb2699c..83092f993 100644 --- a/backend/src/main/java/hanglog/trip/domain/Trip.java +++ b/backend/src/main/java/hanglog/trip/domain/Trip.java @@ -14,6 +14,7 @@ import hanglog.global.BaseEntity; import hanglog.member.domain.Member; import hanglog.share.domain.SharedTrip; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.dto.request.TripUpdateRequest; import jakarta.persistence.Column; import jakarta.persistence.Entity; @@ -70,6 +71,10 @@ public class Trip extends BaseEntity { @Enumerated(value = STRING) private PublishedStatusType publishedStatus; + @Column(nullable = false) + @Enumerated(value = STRING) + private SharedStatusType sharedStatus; + @OneToMany(mappedBy = "trip", cascade = {PERSIST, REMOVE, MERGE}, orphanRemoval = true) @OrderBy(value = "ordinal ASC") private List dayLogs = new ArrayList<>(); @@ -87,7 +92,8 @@ public Trip( final String description, final SharedTrip sharedTrip, final List dayLogs, - final PublishedStatusType publishedStatusType + final SharedStatusType sharedStatus, + final PublishedStatusType publishedStatus ) { super(USABLE); this.id = id; @@ -99,7 +105,8 @@ public Trip( this.description = description; this.sharedTrip = sharedTrip; this.dayLogs = dayLogs; - this.publishedStatus = publishedStatusType; + this.sharedStatus = sharedStatus; + this.publishedStatus = publishedStatus; } public static Trip of(final Member member, final String title, final LocalDate startDate, final LocalDate endDate) { @@ -113,6 +120,7 @@ public static Trip of(final Member member, final String title, final LocalDate s "", null, new ArrayList<>(), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); } @@ -136,13 +144,21 @@ public Optional getSharedCode() { if (Optional.ofNullable(sharedTrip).isEmpty()) { return Optional.empty(); } - if (sharedTrip.isUnShared()) { + if (!isShared()) { return Optional.empty(); } return Optional.of(sharedTrip.getSharedCode()); } - public Boolean isPublished() { + public boolean isShared() { + return this.sharedStatus.equals(SharedStatusType.SHARED); + } + + public void changeSharedStatus(final Boolean isShared) { + this.sharedStatus = SharedStatusType.mappingType(isShared); + } + + public boolean isPublished() { return this.publishedStatus.equals(PublishedStatusType.PUBLISHED); } diff --git a/backend/src/main/resources/db/migration/V8__Move_shared_status_to_trip.sql b/backend/src/main/resources/db/migration/V8__Move_shared_status_to_trip.sql new file mode 100644 index 000000000..035942791 --- /dev/null +++ b/backend/src/main/resources/db/migration/V8__Move_shared_status_to_trip.sql @@ -0,0 +1,10 @@ +ALTER TABLE trip ADD COLUMN shared_status ENUM ('SHARED','UNSHARED'); +UPDATE trip SET shared_status = 'UNSHARED'; +ALTER TABLE trip MODIFY COLUMN shared_status ENUM ('SHARED','UNSHARED') NOT NULL; + +CREATE TABLE copy_shared_trip AS SELECT * FROM shared_trip; + +UPDATE trip INNER JOIN shared_trip ON trip.id = shared_trip.trip_id +SET trip.shared_status = shared_trip.shared_status; + +ALTER TABLE shared_trip DROP COLUMN shared_status; diff --git a/backend/src/test/java/hanglog/expense/fixture/TripExpenseFixture.java b/backend/src/test/java/hanglog/expense/fixture/TripExpenseFixture.java index 3a1f72def..d39daf92e 100644 --- a/backend/src/test/java/hanglog/expense/fixture/TripExpenseFixture.java +++ b/backend/src/test/java/hanglog/expense/fixture/TripExpenseFixture.java @@ -6,6 +6,7 @@ import static hanglog.integration.IntegrationFixture.MEMBER; import hanglog.community.domain.type.PublishedStatusType; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.DayLog; import hanglog.trip.domain.Item; import hanglog.trip.domain.Trip; @@ -27,6 +28,7 @@ public class TripExpenseFixture { "", null, new ArrayList<>(), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); diff --git a/backend/src/test/java/hanglog/share/fixture/ShareFixture.java b/backend/src/test/java/hanglog/share/fixture/ShareFixture.java index e965afd22..79984fe4f 100644 --- a/backend/src/test/java/hanglog/share/fixture/ShareFixture.java +++ b/backend/src/test/java/hanglog/share/fixture/ShareFixture.java @@ -1,12 +1,11 @@ package hanglog.share.fixture; import static hanglog.integration.IntegrationFixture.MEMBER; -import static hanglog.share.domain.type.SharedStatusType.SHARED; -import static hanglog.share.domain.type.SharedStatusType.UNSHARED; import hanglog.city.domain.City; import hanglog.community.domain.type.PublishedStatusType; import hanglog.share.domain.SharedTrip; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.DayLog; import hanglog.trip.domain.Trip; import java.math.BigDecimal; @@ -17,7 +16,7 @@ public class ShareFixture { - public static final Trip TRIP = new Trip( + public static final Trip TRIP_SHARE = new Trip( 1L, MEMBER, "test", @@ -25,8 +24,23 @@ public class ShareFixture { LocalDate.of(2023, 7, 1), LocalDate.of(2023, 7, 2), "", - new SharedTrip(null, null, "sharedCode", SHARED), + new SharedTrip(null, null, "sharedCode"), new ArrayList<>(), + SharedStatusType.SHARED, + PublishedStatusType.UNPUBLISHED + ); + + public static final Trip TRIP_UNSHARE = new Trip( + 1L, + MEMBER, + "test", + "default-image.png", + LocalDate.of(2023, 7, 1), + LocalDate.of(2023, 7, 2), + "", + new SharedTrip(null, null, "sharedCode"), + new ArrayList<>(), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); @@ -34,7 +48,7 @@ public class ShareFixture { 1L, "런던 여행 1일차", 1, - TRIP, + TRIP_SHARE, List.of() ); @@ -42,7 +56,7 @@ public class ShareFixture { 2L, "런던 여행 2일차", 2, - TRIP, + TRIP_SHARE, List.of() ); @@ -69,11 +83,25 @@ public class ShareFixture { BigDecimal.valueOf(-33.9248685), BigDecimal.valueOf(18.4240553) ); - public static final SharedTrip SHARED_TRIP = new SharedTrip(1L, TRIP, "sharedCode", SHARED); - public static final SharedTrip UNSHARED_TRIP = new SharedTrip(2L, TRIP, "sharedCode", UNSHARED); + public static final SharedTrip SHARED_TRIP = new SharedTrip(1L, TRIP_SHARE, "sharedCode"); + public static final SharedTrip UNSHARED_TRIP = new SharedTrip(2L, TRIP_UNSHARE, "sharedCode"); + + public static final Trip TRIP_HAS_SHARED_TRIP = new Trip( + 1L, + MEMBER, + "test", + "default-image.png", + LocalDate.of(2023, 7, 1), + LocalDate.of(2023, 7, 2), + "", + SHARED_TRIP, + new ArrayList<>(), + SharedStatusType.SHARED, + PublishedStatusType.UNPUBLISHED + ); static { - final List dayLogs = TRIP.getDayLogs(); + final List dayLogs = TRIP_SHARE.getDayLogs(); dayLogs.add(LONDON_DAYLOG_1); dayLogs.add(LONDON_DAYLOG_2); } diff --git a/backend/src/test/java/hanglog/share/presentation/SharedTripControllerTest.java b/backend/src/test/java/hanglog/share/presentation/SharedTripControllerTest.java index 866467550..edb69c4e0 100644 --- a/backend/src/test/java/hanglog/share/presentation/SharedTripControllerTest.java +++ b/backend/src/test/java/hanglog/share/presentation/SharedTripControllerTest.java @@ -7,7 +7,7 @@ import static hanglog.share.fixture.ShareFixture.BEIJING; import static hanglog.share.fixture.ShareFixture.CALIFORNIA; import static hanglog.share.fixture.ShareFixture.TOKYO; -import static hanglog.share.fixture.ShareFixture.TRIP; +import static hanglog.share.fixture.ShareFixture.TRIP_SHARE; import static hanglog.trip.fixture.CityFixture.LONDON; import static hanglog.trip.fixture.DayLogFixture.EXPENSE_LONDON_DAYLOG; import static hanglog.trip.fixture.TripFixture.LONDON_TO_JAPAN; @@ -82,7 +82,7 @@ void getSharedTrip() throws Exception { when(sharedTripService.getTripId(anyString())) .thenReturn(1L); when(sharedTripService.getSharedTripDetail(anyLong())) - .thenReturn(SharedTripDetailResponse.of(TRIP, List.of(CALIFORNIA, TOKYO, BEIJING))); + .thenReturn(SharedTripDetailResponse.of(TRIP_SHARE, List.of(CALIFORNIA, TOKYO, BEIJING))); // when mockMvc.perform(get("/shared-trips/{sharedCode}", "xxxxxx").contentType(APPLICATION_JSON)) diff --git a/backend/src/test/java/hanglog/share/service/SharedTripServiceTest.java b/backend/src/test/java/hanglog/share/service/SharedTripServiceTest.java index 477dc4f61..704d7dca5 100644 --- a/backend/src/test/java/hanglog/share/service/SharedTripServiceTest.java +++ b/backend/src/test/java/hanglog/share/service/SharedTripServiceTest.java @@ -4,7 +4,7 @@ import static hanglog.global.exception.ExceptionCode.NOT_FOUND_SHARED_CODE; import static hanglog.global.exception.ExceptionCode.NOT_FOUND_TRIP_ID; import static hanglog.share.fixture.ShareFixture.SHARED_TRIP; -import static hanglog.share.fixture.ShareFixture.TRIP; +import static hanglog.share.fixture.ShareFixture.TRIP_HAS_SHARED_TRIP; import static hanglog.share.fixture.ShareFixture.UNSHARED_TRIP; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -88,7 +88,7 @@ void updateSharedStatus() { // given final SharedTripStatusRequest sharedTripStatusRequest = new SharedTripStatusRequest(true); given(tripRepository.findById(anyLong())) - .willReturn(Optional.of(TRIP)); + .willReturn(Optional.of(TRIP_HAS_SHARED_TRIP)); // when final SharedTripCodeResponse actual = sharedTripService.updateSharedTripStatus(1L, sharedTripStatusRequest); @@ -104,7 +104,7 @@ void updateSharedStatus_CreateSharedTrip() { // given final SharedTripStatusRequest sharedTripStatusRequest = new SharedTripStatusRequest(true); given(tripRepository.findById(anyLong())) - .willReturn(Optional.of(TRIP)); + .willReturn(Optional.of(TRIP_HAS_SHARED_TRIP)); // when final SharedTripCodeResponse actual = sharedTripService.updateSharedTripStatus(1L, sharedTripStatusRequest); diff --git a/backend/src/test/java/hanglog/trip/fixture/DayLogFixture.java b/backend/src/test/java/hanglog/trip/fixture/DayLogFixture.java index 81bc44884..937b8dbcc 100644 --- a/backend/src/test/java/hanglog/trip/fixture/DayLogFixture.java +++ b/backend/src/test/java/hanglog/trip/fixture/DayLogFixture.java @@ -6,6 +6,7 @@ import static hanglog.trip.fixture.ItemFixture.LONDON_EYE_ITEM; import hanglog.community.domain.type.PublishedStatusType; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.DayLog; import hanglog.trip.domain.Trip; import java.time.LocalDate; @@ -24,6 +25,7 @@ public final class DayLogFixture { "", null, new ArrayList<>(), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); diff --git a/backend/src/test/java/hanglog/trip/fixture/TripFixture.java b/backend/src/test/java/hanglog/trip/fixture/TripFixture.java index 4a84a651b..5fda51509 100644 --- a/backend/src/test/java/hanglog/trip/fixture/TripFixture.java +++ b/backend/src/test/java/hanglog/trip/fixture/TripFixture.java @@ -8,6 +8,7 @@ import static hanglog.trip.fixture.DayLogFixture.LONDON_DAYLOG_EXTRA; import hanglog.community.domain.type.PublishedStatusType; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.Trip; import java.time.LocalDate; import java.util.ArrayList; @@ -25,6 +26,7 @@ public final class TripFixture { "", null, new ArrayList<>(List.of(LONDON_DAYLOG_1, LONDON_DAYLOG_2, LONDON_DAYLOG_EXTRA)), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); @@ -38,6 +40,7 @@ public final class TripFixture { "", null, List.of(EXPENSE_LONDON_DAYLOG, EXPENSE_JAPAN_DAYLOG), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); } diff --git a/backend/src/test/java/hanglog/trip/service/TripServiceTest.java b/backend/src/test/java/hanglog/trip/service/TripServiceTest.java index 93e900f7a..5b29cd16a 100644 --- a/backend/src/test/java/hanglog/trip/service/TripServiceTest.java +++ b/backend/src/test/java/hanglog/trip/service/TripServiceTest.java @@ -18,6 +18,7 @@ import hanglog.community.domain.type.PublishedStatusType; import hanglog.global.exception.BadRequestException; import hanglog.member.domain.repository.MemberRepository; +import hanglog.share.domain.type.SharedStatusType; import hanglog.trip.domain.DayLog; import hanglog.trip.domain.Trip; import hanglog.trip.domain.TripCity; @@ -273,6 +274,7 @@ void setUp() { "", null, new ArrayList<>(List.of(dayLog1, dayLog2, dayLog3, extraDayLog)), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); } @@ -297,6 +299,7 @@ void changeDate(final int startDay, final int endDay) { updateRequest.getDescription(), null, trip.getDayLogs(), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ); @@ -348,6 +351,7 @@ void update_DecreasePeriod() { dayLog2, extraDayLog ), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ) ) @@ -389,6 +393,7 @@ void update_IncreasePeriod() { DayLog.generateEmpty(5, trip), extraDayLog ), + SharedStatusType.UNSHARED, PublishedStatusType.UNPUBLISHED ) )