Skip to content

Commit

Permalink
Shared Status 이동 (#639)
Browse files Browse the repository at this point in the history
* chore: shared_status 이동 쿼리 작성

Co-authored-by: hgo641 <hgo641@gmail.com>

* refactor: shared_status 이동에 따른 프로덕션 코드 수정

* refactor: boolean primitive 타입으로 변경

* fix: if 조건 수정

---------

Co-authored-by: hgo641 <hgo641@gmail.com>
  • Loading branch information
2 people authored and jjongwa committed Oct 8, 2023
1 parent 9467a88 commit b1fb3e1
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 37 deletions.
19 changes: 5 additions & 14 deletions backend/src/main/java/hanglog/share/domain/SharedTrip.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down
24 changes: 20 additions & 4 deletions backend/src/main/java/hanglog/trip/domain/Trip.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<DayLog> dayLogs = new ArrayList<>();
Expand All @@ -87,7 +92,8 @@ public Trip(
final String description,
final SharedTrip sharedTrip,
final List<DayLog> dayLogs,
final PublishedStatusType publishedStatusType
final SharedStatusType sharedStatus,
final PublishedStatusType publishedStatus
) {
super(USABLE);
this.id = id;
Expand All @@ -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) {
Expand All @@ -113,6 +120,7 @@ public static Trip of(final Member member, final String title, final LocalDate s
"",
null,
new ArrayList<>(),
SharedStatusType.UNSHARED,
PublishedStatusType.UNPUBLISHED
);
}
Expand All @@ -136,13 +144,21 @@ public Optional<String> 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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,7 @@ public class TripExpenseFixture {
"",
null,
new ArrayList<>(),
SharedStatusType.UNSHARED,
PublishedStatusType.UNPUBLISHED
);

Expand Down
46 changes: 37 additions & 9 deletions backend/src/test/java/hanglog/share/fixture/ShareFixture.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,32 +16,47 @@
public class ShareFixture {


public static final Trip TRIP = new Trip(
public static final Trip TRIP_SHARE = new Trip(
1L,
MEMBER,
"test",
"default-image.png",
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
);

public static final DayLog LONDON_DAYLOG_1 = new DayLog(
1L,
"런던 여행 1일차",
1,
TRIP,
TRIP_SHARE,
List.of()
);

public static final DayLog LONDON_DAYLOG_2 = new DayLog(
2L,
"런던 여행 2일차",
2,
TRIP,
TRIP_SHARE,
List.of()
);

Expand All @@ -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<DayLog> dayLogs = TRIP.getDayLogs();
final List<DayLog> dayLogs = TRIP_SHARE.getDayLogs();
dayLogs.add(LONDON_DAYLOG_1);
dayLogs.add(LONDON_DAYLOG_2);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions backend/src/test/java/hanglog/trip/fixture/DayLogFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ public final class DayLogFixture {
"",
null,
new ArrayList<>(),
SharedStatusType.UNSHARED,
PublishedStatusType.UNPUBLISHED
);

Expand Down
3 changes: 3 additions & 0 deletions backend/src/test/java/hanglog/trip/fixture/TripFixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
);

Expand All @@ -38,6 +40,7 @@ public final class TripFixture {
"",
null,
List.of(EXPENSE_LONDON_DAYLOG, EXPENSE_JAPAN_DAYLOG),
SharedStatusType.UNSHARED,
PublishedStatusType.UNPUBLISHED
);
}
Loading

0 comments on commit b1fb3e1

Please sign in to comment.