Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 모델에 타입, 가격 정보 추가 #386

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package softeer.be_my_car_master.application.model.dto.response;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -23,11 +26,31 @@ public class ModelDto {
@Schema(description = "모델 이미지", example = "s3 url")
private String imgUrl;

@Schema(description = "최소 가격", example = "100000")
private Integer price;

@Schema(description = "타입", example = "SUV")
private String type;

@Schema(description = "신 차 여부", example = "true")
private Boolean isNew;

public static ModelDto from(Model model) {
Boolean isNew = isNewModel(model);
return ModelDto.builder()
.id(model.getId())
.name(model.getName())
.imgUrl(model.getImgUrl())
.price(model.getPrice())
.type(model.getType())
.isNew(isNew)
.build();
}

private static Boolean isNewModel(Model model) {
LocalDateTime createdAt = model.getCreatedAt();
LocalDateTime now = LocalDateTime.now();
long daysBetween = ChronoUnit.DAYS.between(createdAt, now);
return daysBetween < 30;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import softeer.be_my_car_master.domain.model.Model;
import softeer.be_my_car_master.global.annotation.Adaptor;
Expand All @@ -16,8 +18,9 @@ public class GetModelsJpaAdaptor implements GetModelsPort {
private final ModelJpaRepository modelJpaRepository;

@Override
@Transactional(readOnly = true)
public List<Model> findModels() {
return modelJpaRepository.findAll().stream()
return modelJpaRepository.findAllByOrderByCreatedAtDesc().stream()
.map(ModelEntity::toModel)
.collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package softeer.be_my_car_master.domain.model;

import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -14,8 +16,15 @@ public class Model {
private final Long id;
private final String name;
private final String imgUrl;
private final Integer price;
private final Type type;
private final LocalDateTime createdAt;

public boolean isRightModel(Long modelId) {
return modelId.equals(id);
}

public String getType() {
return type.getValue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package softeer.be_my_car_master.domain.model;

import lombok.Getter;

@Getter
public enum Type {

HYDROGEN_ELECTRIC("수소 / 전기차"),
N("N"),
PASSENGER("승용차"),
SUV("SUV"),
MPV("MPV"),
LIGHT_TRUCK_AND_TAXI("소형 트럭&택시"),
TRUCK("트럭"),
BUS("버스");

private String value;

Type(String value) {
this.value = value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package softeer.be_my_car_master.global.config;

import java.time.LocalDateTime;

import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import lombok.Getter;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTime {

@CreatedDate
protected LocalDateTime createdAt;

@LastModifiedDate
protected LocalDateTime updatedAt;
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package softeer.be_my_car_master.infrastructure.jpa.model.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import softeer.be_my_car_master.domain.model.Model;
import softeer.be_my_car_master.domain.model.Type;
import softeer.be_my_car_master.global.config.BaseTime;
import softeer.be_my_car_master.infrastructure.jpa.trim.entity.TrimEntity;

@Entity
@Table(name = "model")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ModelEntity {
public class ModelEntity extends BaseTime {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -26,11 +35,26 @@ public class ModelEntity {
@Column(name = "img_url", nullable = false)
private String imgUrl;

@Column(name = "type", nullable = false)
@Enumerated(EnumType.STRING)
private Type type;

@OneToMany(mappedBy = "model")
private List<TrimEntity> trims = new ArrayList<>();

public Model toModel() {
Integer minPrice = trims.stream()
.map(TrimEntity::getPrice)
.min(Integer::compareTo)
.orElse(0);

return Model.builder()
.id(id)
.name(name)
.imgUrl(imgUrl)
.type(type)
.price(minPrice)
.createdAt(createdAt)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package softeer.be_my_car_master.infrastructure.jpa.model.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;

import softeer.be_my_car_master.infrastructure.jpa.model.entity.ModelEntity;

public interface ModelJpaRepository extends JpaRepository<ModelEntity, Long> {

List<ModelEntity> findAllByOrderByCreatedAtDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.*;

import java.util.ArrayList;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.UUID;

Expand All @@ -24,6 +24,7 @@
import softeer.be_my_car_master.domain.color_interior.InteriorColor;
import softeer.be_my_car_master.domain.engine.Engine;
import softeer.be_my_car_master.domain.model.Model;
import softeer.be_my_car_master.domain.model.Type;
import softeer.be_my_car_master.domain.option.Option;
import softeer.be_my_car_master.domain.trim.Trim;
import softeer.be_my_car_master.domain.wheel_dirve.WheelDrive;
Expand Down Expand Up @@ -67,7 +68,10 @@ void execute() {
.totalPrice(4000)
.build();

given(port.findModels()).willReturn(Arrays.asList(new Model(1L, "model", "url")));
given(port.findModels())
.willReturn(Arrays.asList(
new Model(1L, "model", "url", 100, Type.SUV, LocalDateTime.now())
));
given(port.findTrimsByModel(any()))
.willReturn(Arrays.asList(
new Trim(1L, "model", "description", 22, 500, "url")
Expand Down Expand Up @@ -167,7 +171,10 @@ void invalidOptions() {
.totalPrice(4000)
.build();

given(port.findModels()).willReturn(Arrays.asList(new Model(1L, "model", "url")));
given(port.findModels())
.willReturn(Arrays.asList(
new Model(1L, "model", "url", 1000, Type.SUV, LocalDateTime.now())
));
given(port.findTrimsByModel(any()))
.willReturn(Arrays.asList(
new Trim(1L, "model", "description", 22, 500, "url")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import softeer.be_my_car_master.application.model.dto.response.GetModelsResponse;
import softeer.be_my_car_master.application.model.dto.response.ModelDto;
import softeer.be_my_car_master.application.model.usecase.get_models.GetModelsUseCase;
import softeer.be_my_car_master.domain.model.Type;
import softeer.be_my_car_master.global.response.Response;

@WebMvcTest(ModelController.class)
Expand All @@ -42,6 +43,8 @@ void getModels() throws Exception {
.id(1L)
.name("model name")
.imgUrl("imgUrl")
.price(1000)
.type(Type.SUV.getValue())
.build();
response.setModels(Arrays.asList(modelDto));
given(getModelsUseCase.execute()).willReturn(response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.mockito.BDDMockito.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

Expand All @@ -18,6 +19,7 @@
import softeer.be_my_car_master.application.model.usecase.get_models.GetModelsPort;
import softeer.be_my_car_master.application.model.usecase.get_models.GetModelsUseCase;
import softeer.be_my_car_master.domain.model.Model;
import softeer.be_my_car_master.domain.model.Type;

@ExtendWith(MockitoExtension.class)
@DisplayName("GetModelsUseCase Test")
Expand All @@ -37,6 +39,9 @@ void execute() {
.id(1L)
.name("name")
.imgUrl("image")
.price(10000)
.type(Type.SUV)
.createdAt(LocalDateTime.now())
.build();
given(getModelsPort.findModels()).willReturn(Arrays.asList(model));

Expand All @@ -53,6 +58,9 @@ void execute() {
softAssertions.assertThat(expected.getId()).isEqualTo(model.getId());
softAssertions.assertThat(expected.getName()).isEqualTo(model.getName());
softAssertions.assertThat(expected.getImgUrl()).isEqualTo(model.getImgUrl());
softAssertions.assertThat(expected.getPrice()).isEqualTo(model.getPrice());
softAssertions.assertThat(expected.getType()).isEqualTo(model.getType());
softAssertions.assertThat(expected.getIsNew()).isEqualTo(true);
});
}
}
Loading