Skip to content

Commit

Permalink
refactor: 모두의 정원 VO 래핑
Browse files Browse the repository at this point in the history
  • Loading branch information
Kim0914 committed Dec 28, 2023
1 parent 988a3d8 commit 90f8ce8
Show file tree
Hide file tree
Showing 16 changed files with 170 additions and 115 deletions.
32 changes: 8 additions & 24 deletions backend/pium/src/main/java/com/official/pium/domain/Garden.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.official.pium.domain;

import com.official.pium.domain.vo.PlantState;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
Expand Down Expand Up @@ -48,21 +50,8 @@ public class Garden 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 PlantState plantState;

@NotNull
@Column(name = "day_since", nullable = false)
Expand All @@ -84,19 +73,14 @@ public class Garden extends BaseEntity {
private String manageLevel;

@Builder
public Garden(final DictionaryPlant dictionaryPlant, final Member member, final String nickname,
final String imageUrl, final String location,
final String flowerpot, final String light, final String wind, final Long daySince,
final Integer waterCycle, final String content,
final String manageLevel) {
private Garden(DictionaryPlant dictionaryPlant, Member member, String nickname, String imageUrl,
PlantState plantState,
Long daySince, Integer waterCycle, String content, String manageLevel) {
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.plantState = plantState;
this.daySince = daySince;
this.waterCycle = waterCycle;
this.content = content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.official.pium.domain.vo;

import com.official.pium.domain.WaterCycle;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import jakarta.persistence.Embedded;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
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 PlantState {

@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 PlantState(String location, String flowerpot, String light, String wind) {
this.location = location;
this.flowerpot = flowerpot;
this.light = light;
this.wind = wind;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.official.pium.domain;
package com.official.pium.domain.vo;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
import com.official.pium.domain.Garden;
import com.official.pium.domain.Member;
import com.official.pium.domain.PetPlant;
import com.official.pium.mapper.GardenMapper;
import com.official.pium.domain.vo.PlantState;
import com.official.pium.repository.GardenRepository;
import com.official.pium.repository.PetPlantRepository;
import com.official.pium.service.dto.GardenCreateRequest;
import com.official.pium.service.dto.GardenResponse;
import java.time.LocalDate;
import java.util.List;
import java.util.NoSuchElementException;
import lombok.RequiredArgsConstructor;
Expand All @@ -29,11 +30,35 @@ public void create(GardenCreateRequest request, Member member) {
PetPlant petPlant = petPlantRepository.findById(request.getPetPlantId())
.orElseThrow(() -> new NoSuchElementException("반려 식물이 존재하지 않습니다. id: " + request.getPetPlantId()));
checkOwner(petPlant, member);

Garden garden = GardenMapper.toGarden(request, petPlant);
Garden garden = createGarden(petPlant, request);
gardenRepository.save(garden);
}

private Garden createGarden(PetPlant petPlant, GardenCreateRequest request) {
return Garden.builder()
.dictionaryPlant(petPlant.getDictionaryPlant())
.member(petPlant.getMember())
.nickname(petPlant.getNickname())
.imageUrl(petPlant.getImageUrl())
.plantState(
toPlantState(petPlant)
)
.daySince(petPlant.calculateDaySince(LocalDate.now()))
.waterCycle(petPlant.getWaterCycle())
.content(request.getContent())
.manageLevel(request.getManageLevel())
.build();
}

private static PlantState toPlantState(PetPlant petPlant) {
return PlantState.builder()
.location(petPlant.getLocation())
.flowerpot(petPlant.getFlowerpot())
.light(petPlant.getLight())
.wind(petPlant.getWind())
.build();
}

private void checkOwner(PetPlant petPlant, Member member) {
if (petPlant.isNotOwnerOf(member)) {
throw new IllegalArgumentException("요청 사용자와 반려 식물의 사용자가 일치하지 않습니다. memberId: " + member.getId());
Expand All @@ -42,6 +67,6 @@ private void checkOwner(PetPlant petPlant, Member member) {

public GardenResponse readAll(Pageable pageable, List<Long> filters) {
Page<Garden> gardens = gardenRepository.findAllByDictionaryPlantIds(pageable, filters);
return GardenMapper.toGardenResponse(gardens);
return GardenResponse.from(gardens);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.official.pium.service.dto;

import com.official.pium.domain.DictionaryPlant;
import com.official.pium.domain.WaterCycle;
import com.official.pium.domain.vo.WaterCycle;
import com.official.pium.domain.vo.CareDetail;
import com.official.pium.domain.vo.Classification;
import com.official.pium.domain.vo.Property;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.official.pium.service.dto;

import com.official.pium.domain.WaterCycle;
import com.official.pium.domain.vo.WaterCycle;
import com.official.pium.domain.vo.CareDetail;
import com.official.pium.domain.vo.Classification;
import com.official.pium.domain.vo.Property;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.official.pium.service.dto;

import com.official.pium.domain.Garden;
import com.official.pium.domain.PetPlant;
import com.official.pium.domain.vo.PlantState;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import java.time.LocalDate;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -24,4 +28,25 @@ public class GardenCreateRequest {

@NotBlank
private String manageLevel;

public Garden toGarden(PetPlant petPlant) {
return Garden.builder()
.dictionaryPlant(petPlant.getDictionaryPlant())
.member(petPlant.getMember())
.nickname(petPlant.getNickname())
.imageUrl(petPlant.getImageUrl())
.plantState(
PlantState.builder()
.location(petPlant.getLocation())
.flowerpot(petPlant.getFlowerpot())
.light(petPlant.getLight())
.wind(petPlant.getWind())
.build()
)
.daySince(petPlant.calculateDaySince(LocalDate.now()))
.waterCycle(petPlant.getWaterCycle())
.content(this.content)
.manageLevel(this.manageLevel)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.official.pium.service.dto;

import com.official.pium.domain.Garden;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.domain.Page;

@Getter
@Builder
Expand All @@ -18,4 +21,17 @@ public class GardenResponse {
private Long elementSize;
private boolean hasNext;
private List<SingleGardenResponse> data;

public static GardenResponse from(Page<Garden> gardens) {
return GardenResponse.builder()
.page(gardens.getPageable().getPageNumber())
.size(gardens.getSize())
.elementSize(gardens.getTotalElements())
.hasNext(gardens.hasNext())
.data(gardens.getContent().stream()
.map(SingleGardenResponse::from)
.collect(Collectors.toList())
).build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.official.pium.service.dto;

import com.official.pium.domain.Garden;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -21,6 +22,32 @@ public class SingleGardenResponse {
private String manageLevel;
private PetPlantInfo petPlant;

public static SingleGardenResponse from(Garden garden) {
return SingleGardenResponse.builder()
.id(garden.getId())
.createdAt(garden.getCreatedAt())
.updatedAt(garden.getUpdatedAt())
.dictionaryPlantName(garden.getDictionaryPlant().getClassification()
.getName())
.content(garden.getContent())
.manageLevel(garden.getManageLevel())
.petPlant(toPetPlantInfo(garden))
.build();
}

private static SingleGardenResponse.PetPlantInfo toPetPlantInfo(Garden garden) {
return SingleGardenResponse.PetPlantInfo.builder()
.imageUrl(garden.getImageUrl())
.nickname(garden.getNickname())
.location(garden.getPlantState().getLocation())
.flowerpot(garden.getPlantState().getFlowerpot())
.light(garden.getPlantState().getLight())
.wind(garden.getPlantState().getWind())
.daySince(garden.getDaySince())
.waterCycle(garden.getWaterCycle())
.build();
}

@Getter
@NoArgsConstructor
public static class PetPlantInfo {
Expand All @@ -35,7 +62,8 @@ public static class PetPlantInfo {
private Integer waterCycle;

@Builder
private PetPlantInfo(String imageUrl, String nickname, String location, String flowerpot, String light, String wind, Long daySince, Integer waterCycle) {
private PetPlantInfo(String imageUrl, String nickname, String location, String flowerpot, String light,
String wind, Long daySince, Integer waterCycle) {
this.imageUrl = imageUrl;
this.nickname = nickname;
this.location = location;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.official.pium.fixture;

import com.official.pium.domain.DictionaryPlant;
import com.official.pium.domain.WaterCycle;
import com.official.pium.domain.vo.WaterCycle;
import com.official.pium.domain.vo.CareDetail;
import com.official.pium.domain.vo.Classification;
import com.official.pium.domain.vo.Property;
Expand Down
Loading

0 comments on commit 90f8ce8

Please sign in to comment.