Skip to content

Commit

Permalink
refactor: petPlant 정원 VO 래핑
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Dec 28, 2023
1 parent 90f8ce8 commit 2a8bd71
Show file tree
Hide file tree
Showing 30 changed files with 901 additions and 776 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.official.pium.domain;

import com.official.pium.domain.vo.PlantState;
import com.official.pium.domain.vo.GardenPlantState;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
Expand Down Expand Up @@ -51,7 +51,7 @@ public class Garden extends BaseEntity {
private String imageUrl;

@Embedded
private PlantState plantState;
private GardenPlantState gardenPlantState;

@NotNull
@Column(name = "day_since", nullable = false)
Expand All @@ -74,13 +74,13 @@ public class Garden extends BaseEntity {

@Builder
private Garden(DictionaryPlant dictionaryPlant, Member member, String nickname, String imageUrl,
PlantState plantState,
GardenPlantState gardenPlantState,
Long daySince, Integer waterCycle, String content, String manageLevel) {
this.dictionaryPlant = dictionaryPlant;
this.member = member;
this.nickname = nickname;
this.imageUrl = imageUrl;
this.plantState = plantState;
this.gardenPlantState = gardenPlantState;
this.daySince = daySince;
this.waterCycle = waterCycle;
this.content = content;
Expand Down
90 changes: 32 additions & 58 deletions backend/pium/src/main/java/com/official/pium/domain/PetPlant.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.official.pium.domain;

import com.official.pium.domain.vo.PetPlantState;
import com.official.pium.domain.vo.WaterDate;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -51,33 +54,15 @@ public class PetPlant extends BaseEntity {
@Column(name = "image_url", nullable = false)
private String imageUrl;

@NotBlank
@Column(name = "location", nullable = false)
private String location;

@NotBlank
@Column(name = "flowerpot", nullable = false)
private String flowerpot;

@NotBlank
@Column(name = "light", nullable = false)
private String light;

@NotBlank
@Column(name = "wind", nullable = false)
private String wind;
@Embedded
private PetPlantState petPlantState;

@NotNull
@Column(name = "birth_date", nullable = false)
private LocalDate birthDate;

@NotNull
@Column(name = "next_water_date", nullable = false)
private LocalDate nextWaterDate;

@NotNull
@Column(name = "last_water_date", nullable = false)
private LocalDate lastWaterDate;
@Embedded
private WaterDate waterDate;

@Min(MIN_WATER_CYCLE)
@Max(MAX_WATER_CYCLE)
Expand All @@ -86,20 +71,23 @@ public class PetPlant extends BaseEntity {
private Integer waterCycle;

@Builder
private PetPlant(DictionaryPlant dictionaryPlant, Member member, String nickname, String imageUrl, String location,
String flowerpot, String light, String wind, @NotNull LocalDate birthDate,
@NotNull LocalDate nextWaterDate, @NotNull LocalDate lastWaterDate, @NotNull Integer waterCycle) {
private PetPlant(
DictionaryPlant dictionaryPlant,
Member member,
String nickname,
String imageUrl,
PetPlantState petPlantState,
LocalDate birthDate,
WaterDate waterDate,
Integer waterCycle
) {
this.dictionaryPlant = dictionaryPlant;
this.member = member;
this.nickname = nickname;
this.imageUrl = imageUrl;
this.location = location;
this.flowerpot = flowerpot;
this.light = light;
this.wind = wind;
this.petPlantState = petPlantState;
this.birthDate = birthDate;
this.nextWaterDate = nextWaterDate;
this.lastWaterDate = lastWaterDate;
this.waterDate = waterDate;
this.waterCycle = waterCycle;
}

Expand All @@ -114,33 +102,30 @@ public Long calculateDaySince(LocalDate currentDate) {
* - 0 : 오늘 할 일 - 음수 : 할 일 - 양수 : 지각
*/
public Long calculateDday(LocalDate currentDate) {
return ChronoUnit.DAYS.between(nextWaterDate, currentDate);
return waterDate.calculateDday(currentDate);
}

public void updatePetPlant(
String nickname, String location, String flowerpot, String light,
String wind, Integer waterCycle, LocalDate birthDate, LocalDate lastWaterDate, String imageUrl
String nickname,
PetPlantState petPlantState,
Integer waterCycle,
LocalDate birthDate,
LocalDate lastWaterDate,
String imageUrl
) {
validateEmptyValue(nickname);
validateEmptyValue(location);
validateEmptyValue(flowerpot);
validateEmptyValue(light);
validateEmptyValue(wind);
validateWaterCycle(waterCycle);
validateLocalDate(birthDate);
validateLocalDate(lastWaterDate);
validateImageUrl(imageUrl);
if (!Objects.equals(waterCycle, this.waterCycle)) {
this.nextWaterDate = lastWaterDate.plusDays(waterCycle);
waterDate.plusNextWaterDate(waterCycle);
}
waterDate.changeLastWaterDate(lastWaterDate);
this.nickname = nickname;
this.location = location;
this.flowerpot = flowerpot;
this.light = light;
this.wind = wind;
this.petPlantState = petPlantState;
this.waterCycle = waterCycle;
this.birthDate = birthDate;
this.lastWaterDate = lastWaterDate;
this.imageUrl = imageUrl;
}

Expand Down Expand Up @@ -169,30 +154,19 @@ private void validateImageUrl(String imageUrl) {
}

public void water(LocalDate newWaterDate) {
if (newWaterDate.isAfter(LocalDate.now())) {
throw new IllegalArgumentException("오늘 이후 날짜에 물을 줄 수는 없습니다. date: " + newWaterDate);
}

if (newWaterDate.isEqual(lastWaterDate) || newWaterDate.isBefore(lastWaterDate)) {
throw new IllegalArgumentException("마지막으로 물을 준 날짜와 그 이전 날짜에는 물을 줄 수는 없습니다. date: " + newWaterDate);
}
this.nextWaterDate = newWaterDate.plusDays(waterCycle);
this.lastWaterDate = newWaterDate;
waterDate.water(newWaterDate, this.waterCycle);
}

public void changeNextWaterDate(LocalDate newWaterDate) {
if (newWaterDate.isEqual(LocalDate.now()) || newWaterDate.isBefore(LocalDate.now())) {
throw new IllegalArgumentException("오늘과 그 이전 날짜로 물주기 날짜를 변경할 수는 없습니다. date: " + newWaterDate);
}
this.nextWaterDate = newWaterDate;
waterDate.changeNextWaterDate(newWaterDate);
}

public boolean isNotOwnerOf(Member member) {
return !Objects.equals(this.member, member);
}

public boolean isDifferentLastWaterDate(LocalDate lastWaterDate) {
return !this.lastWaterDate.isEqual(lastWaterDate);
return waterDate.isDifferentLastWaterDate(lastWaterDate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PlantState {
public class GardenPlantState {

@NotBlank
@Column(name = "location", nullable = false)
Expand All @@ -30,7 +30,7 @@ public class PlantState {
private String wind;

@Builder
private PlantState(String location, String flowerpot, String light, String wind) {
private GardenPlantState(String location, String flowerpot, String light, String wind) {
this.location = location;
this.flowerpot = flowerpot;
this.light = light;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.official.pium.domain.vo;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.NotBlank;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PetPlantState {

@NotBlank
@Column(name = "location", nullable = false)
private String location;

@NotBlank
@Column(name = "flowerpot", nullable = false)
private String flowerpot;

@NotBlank
@Column(name = "light", nullable = false)
private String light;

@NotBlank
@Column(name = "wind", nullable = false)
private String wind;

@Builder
private PetPlantState(String location, String flowerpot, String light, String wind) {
validateEmptyValue(location);
validateEmptyValue(flowerpot);
validateEmptyValue(light);
validateEmptyValue(wind);
this.location = location;
this.flowerpot = flowerpot;
this.light = light;
this.wind = wind;
}

private void validateEmptyValue(String value) {
if (value == null || value.isBlank()) {
throw new IllegalArgumentException("반려 식물 속성에는 빈 값 들어올 수 없습니다. value: " + value);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.official.pium.domain.vo;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.validation.constraints.NotNull;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Embeddable
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class WaterDate {

@NotNull
@Column(name = "next_water_date", nullable = false)
private LocalDate nextWaterDate;

@NotNull
@Column(name = "last_water_date", nullable = false)
private LocalDate lastWaterDate;

@Builder
private WaterDate(LocalDate nextWaterDate, LocalDate lastWaterDate) {
this.nextWaterDate = nextWaterDate;
this.lastWaterDate = lastWaterDate;
}

public Long calculateDday(LocalDate currentDate) {
return ChronoUnit.DAYS.between(nextWaterDate, currentDate);
}

public void plusNextWaterDate(Integer waterCycle) {
this.nextWaterDate = lastWaterDate.plusDays(waterCycle);
}

public void changeNextWaterDate(LocalDate newWaterDate) {
if (newWaterDate.isEqual(LocalDate.now()) || newWaterDate.isBefore(LocalDate.now())) {
throw new IllegalArgumentException("오늘과 그 이전 날짜로 물주기 날짜를 변경할 수는 없습니다. date: " + newWaterDate);
}
this.nextWaterDate = newWaterDate;
}

public void changeLastWaterDate(LocalDate lastWaterDate) {
this.lastWaterDate = lastWaterDate;
}

public void water(LocalDate newWaterDate, Integer waterCycle) {
if (newWaterDate.isAfter(LocalDate.now())) {
throw new IllegalArgumentException("오늘 이후 날짜에 물을 줄 수는 없습니다. date: " + newWaterDate);
}

if (newWaterDate.isEqual(lastWaterDate) || newWaterDate.isBefore(lastWaterDate)) {
throw new IllegalArgumentException("마지막으로 물을 준 날짜와 그 이전 날짜에는 물을 줄 수는 없습니다. date: " + newWaterDate);
}
this.nextWaterDate = newWaterDate.plusDays(waterCycle);
this.lastWaterDate = newWaterDate;
}

public boolean isDifferentLastWaterDate(LocalDate lastWaterDate) {
return !this.lastWaterDate.isEqual(lastWaterDate);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.official.pium.event.history;

import com.official.pium.domain.HistoryType;
import com.official.pium.domain.PetPlant;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -30,6 +31,17 @@ private PetPlantHistory(String location, String flowerpot, String light, String
this.lastWaterDate = lastWaterDate;
}

public static PetPlantHistory from(PetPlant petPlant) {
return PetPlantHistory.builder()
.location(petPlant.getPetPlantState().getLocation())
.flowerpot(petPlant.getPetPlantState().getFlowerpot())
.light(petPlant.getPetPlantState().getLight())
.wind(petPlant.getPetPlantState().getWind())
.waterCycle(petPlant.getWaterCycle().toString())
.lastWaterDate(petPlant.getWaterDate().getLastWaterDate().toString())
.build();
}

public List<HistoryEvent> generateCreateHistoryEvents(Long petPlantId, LocalDate date) {
List<HistoryEvent> events = new ArrayList<>();
events.add(HistoryEvent.of(petPlantId, EMPTY, location, HistoryType.LOCATION, date));
Expand Down
Loading

0 comments on commit 2a8bd71

Please sign in to comment.