Skip to content

Commit

Permalink
Merge pull request #270 from softeerbootcamp-2nd/bugfix/model-response
Browse files Browse the repository at this point in the history
Bugfix/model response
  • Loading branch information
dydwo0740 committed Aug 16, 2023
2 parents ee53979 + 09d4e23 commit 942179b
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ public ModelDetailMappedDto getModelDetail(@Parameter(description = "모델 타
return modelTypeService.getModelDetail(modelId);
}

@Operation(summary = "파워트레인 HMG 데이터 호출", description = "파워트레인의 마력과 토크값 반환")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = PowerTrainMappedDto.class)))
})
@GetMapping("/hmg-powertrain")
public PowerTrainMappedDto getPowerTrainData(@Parameter(description = "파워트레인 모델 타입 ID") @RequestParam("powertrain") int powerTrainId) {
return modelTypeService.getPowerTrainHmgData(powerTrainId);
}

@Operation(summary = "효율 HMG 데이터 호출", description = "파워트레인과 구동방식의 조합으로 나온 효츌 HMG값 반환")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = ModelEfficiencyDataDto.class)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package autoever2.cartag.domain.model;

import io.swagger.v3.oas.annotations.media.ArraySchema;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -19,4 +20,10 @@ public class ModelShortDataDto {
private Long modelPrice;
@Schema(description = "선택 비율 퍼센트 값(정수), 데이터가 없다면 0", example = "38")
private int percentage;
@ArraySchema(schema = @Schema(implementation = PowerTrainDataDto.class, description = "파워트레인의 HMG 데이터"))
private PowerTrainDataDto hmgData;

public void setHmgData(PowerTrainDataDto hmgData) {
this.hmgData = hmgData;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@ public class ModelShortMappedDto {
private Long modelPrice;
private Long modelBoughtCount;
private boolean isDefaultModel;
private int modelTypeId;
private String maxPs;
private String maxKgfm;

@Builder
public ModelShortMappedDto(int modelId, String modelName, String modelTypeName, Long modelPrice, Long modelBoughtCount, boolean isDefaultModel) {
public ModelShortMappedDto(int modelId, String modelName, String modelTypeName, Long modelPrice, Long modelBoughtCount, boolean isDefaultModel, int modelTypeId, String maxPs, String maxKgfm) {
this.modelId = modelId;
this.modelName = modelName;
this.modelTypeName = modelTypeName;
this.modelPrice = modelPrice;
this.modelBoughtCount = modelBoughtCount;
this.isDefaultModel = isDefaultModel;
this.modelTypeId = modelTypeId;
this.maxPs = maxPs;
this.maxKgfm = maxKgfm;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ModelShortMappedDto that = (ModelShortMappedDto) o;
return modelId == that.modelId && isDefaultModel == that.isDefaultModel && Objects.equals(modelName, that.modelName) && Objects.equals(modelTypeName, that.modelTypeName) && Objects.equals(modelPrice, that.modelPrice) && Objects.equals(modelBoughtCount, that.modelBoughtCount);
return modelId == that.modelId && isDefaultModel == that.isDefaultModel && modelTypeId == that.modelTypeId && Objects.equals(modelName, that.modelName) && Objects.equals(modelTypeName, that.modelTypeName) && Objects.equals(modelPrice, that.modelPrice) && Objects.equals(modelBoughtCount, that.modelBoughtCount);
}

public void setIsDefaultModel(int isDefaultModel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package autoever2.cartag.domain.model;

import lombok.Builder;

public class PowerTrainDataDto {

private String maxPs;
private String maxKgfm;
private Double ratioPs;
private Double ratioKgfm;

@Builder
public PowerTrainDataDto(String maxPs, String maxKgfm, Double ratioPs, Double ratioKgfm) {
this.maxPs = maxPs;
this.maxKgfm = maxKgfm;
this.ratioPs = ratioPs;
this.ratioKgfm = ratioKgfm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ public ModelRepository(DataSource dataSource) {
}

public List<ModelShortMappedDto> findAllModelTypeData(int carId) {
String sql = "select m.model_id, m.model_name, t.model_type_name, m.model_price, mm.model_bought_count, mm.is_default_model " +
String sql = "select m.model_id, m.model_name, t.model_type_id, t.model_type_name, m.model_price, mm.model_bought_count, mm.is_default_model " +
"from ModelCarMapper mm " +
"inner join Model m " +
"on mm.model_id = m.model_id " +
"inner join ModelType t " +
"on m.model_type_id = t.model_type_id " +
"left join PowerTrainData pd " +
"on pd.power_train_id = m.model_id " +
"where mm.car_id = :carId";

SqlParameterSource param = new MapSqlParameterSource()
Expand Down Expand Up @@ -61,21 +63,6 @@ private RowMapper<ModelDetailMappedDto> modelDetailRowMapper() {
return BeanPropertyRowMapper.newInstance(ModelDetailMappedDto.class);
}

public Optional<PowerTrainMappedDto> findPowerTrainData(int powerTrainId) {
String sql = "select max_ps, max_kgfm " +
"from PowerTrainData " +
"where power_train_id = :powerTrainId";

SqlParameterSource param = new MapSqlParameterSource()
.addValue("powerTrainId", powerTrainId);

return Optional.ofNullable(DataAccessUtils.singleResult(template.query(sql, param, powerTrainRowMapper())));
}

private RowMapper<PowerTrainMappedDto> powerTrainRowMapper() {
return BeanPropertyRowMapper.newInstance(PowerTrainMappedDto.class);
}

public Optional<ModelEfficiencyDataDto> findEfficiencyData(int powerTrainId, int operationId) {
String sql = "select average_fuel, displacement " +
"from PowerTrainOperationEfficiency " +
Expand Down
76 changes: 69 additions & 7 deletions backend/src/main/java/autoever2/cartag/service/ModelService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ public class ModelService {
public List<ModelShortDataDto> getModelTypeData(int carId) {
List<ModelShortMappedDto> modelData = modelRepository.findAllModelTypeData(carId);
Long carBoughtCount = carRepository.findCarBoughtCountByCarId(carId).orElse(0L);
List<ModelShortMappedDto> powerTrainData = modelData.stream().filter(modelShortMappedDto -> modelShortMappedDto.getModelTypeId() == 1).collect(Collectors.toList());

return modelData.stream().map(modelTypeMappedDto -> {
List<ModelShortDataDto> result = getPowerTrainDataWithRatio(powerTrainData, carBoughtCount);

result.addAll(modelData.stream().filter(modelShortMappedDto -> modelShortMappedDto.getModelTypeId() != 1).map(modelTypeMappedDto -> {
int percentage = 0;
if (carBoughtCount != 0) {
percentage = (int) (modelTypeMappedDto.getModelBoughtCount() * 100 / carBoughtCount);
Expand All @@ -36,17 +39,76 @@ public List<ModelShortDataDto> getModelTypeData(int carId) {
}
)

.collect(Collectors.toList());
.collect(Collectors.toList()));

return result;
}

//TODO: RuntimeException 처리
public ModelDetailMappedDto getModelDetail(int modelId) {
return modelRepository.findModelDetailData(modelId).orElseThrow(() -> new RuntimeException("데이터가 존재하지 않습니다."));
private List<ModelShortDataDto> getPowerTrainDataWithRatio(List<ModelShortMappedDto> powerTrainData, Long carBoughtCount) {
double maxPs = 0.0;
double maxKgfm = 0.0;

for(ModelShortMappedDto data : powerTrainData) {
if(data.getMaxPs() == null) {
continue;
}
maxPs = Double.max(maxPs, calculateHmgString(data.getMaxPs()));
maxKgfm = Double.max(maxKgfm, calculateHmgString(data.getMaxKgfm()));
}

final double finalPs = maxPs;
final double finalKgfm = maxKgfm;

return powerTrainData.stream().map(data -> {
int percentage = 0;
if(carBoughtCount != 0) {
percentage = (int) (data.getModelBoughtCount() * 100 / carBoughtCount);
}

ModelShortDataDto result = ModelShortDataDto.builder()
.modelId(data.getModelId())
.modelName(data.getModelName())
.modelPrice(data.getModelPrice())
.modelTypeName(data.getModelTypeName())
.percentage(percentage)
.build();

if(finalPs <= 0 || finalKgfm <= 0) {
return result;
}
result.setHmgData(PowerTrainDataDto.builder()
.maxPs(data.getMaxPs())
.maxKgfm(data.getMaxKgfm())
.ratioPs(calculateHmgString(data.getMaxPs()) / finalPs)
.ratioKgfm(calculateHmgString(data.getMaxKgfm()) / finalKgfm)
.build());

return result;
}).collect(Collectors.toList());
}

private Double calculateHmgString(String input) {
String[] token = input.split("/");

int denominator = 0;
if(token[1].contains("~")) {
String[] subToken = token[1].split("~");
denominator = Integer.parseInt(subToken[0]) + Integer.parseInt(subToken[1]) / 2;
}
if(!token[1].contains("~")) {
denominator = Integer.parseInt(token[1]);
}

if(denominator == 0) {
return 0.0;
}

return Double.parseDouble(token[0]) / denominator;
}

//TODO: RuntimeException 처리
public PowerTrainMappedDto getPowerTrainHmgData(int powerTrainId) {
return modelRepository.findPowerTrainData(powerTrainId).orElse(null);
public ModelDetailMappedDto getModelDetail(int modelId) {
return modelRepository.findModelDetailData(modelId).orElseThrow(() -> new RuntimeException("데이터가 존재하지 않습니다."));
}

//TODO: RuntimeException 처리
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,6 @@ void getModelDetail() throws Exception {
.andExpect(jsonPath("$.modelImage").value("/model/diesel2-2.jpg"));
}

@Test
@DisplayName("파워트레인의 HMG 데이터 호출 API")
void getPowerTrainHmgData() throws Exception {
int powerTrainId = 1;

PowerTrainMappedDto data = PowerTrainMappedDto.builder()
.maxPs("202/3,800PS/rpm")
.maxKgfm("45.0/1,750~2,750kgf-m/rpm")
.build();

given(modelService.getPowerTrainHmgData(powerTrainId)).willReturn(data);

ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/modeltypes/hmg-powertrain").param("powertrain", String.valueOf(powerTrainId)));

resultActions.andExpect(status().isOk())
.andExpect(jsonPath("$.maxPs").value("202/3,800PS/rpm"))
.andExpect(jsonPath("$.maxKgfm").value("45.0/1,750~2,750kgf-m/rpm"));
}

@Test
@DisplayName("연비와 cc HMG 데이터 호출 API")
void getEfficiencyData() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,19 @@ void findAllModelTypeData() {
ModelShortMappedDto firstModel = ModelShortMappedDto.builder()
.modelId(1)
.modelName("디젤2.2")
.modelTypeId(1)
.modelTypeName("파워트레인")
.isDefaultModel(true)
.modelBoughtCount(800L)
.modelPrice(1480000L)
.maxPs("202/3800")
.maxKgfm("45.0/1750~2750")
.build();

ModelShortMappedDto sixthModel = ModelShortMappedDto.builder()
.modelId(6)
.modelName("8인승")
.modelTypeId(3)
.modelTypeName("바디타입")
.isDefaultModel(false)
.modelBoughtCount(1800L)
Expand Down Expand Up @@ -100,32 +104,6 @@ void findModelDetail() {
assertEquals(model2, result2.get());
}

@Test
@DisplayName("파워트레인의 경우 HMG 데이터를 가져온다.")
void findPowerTrainData() {
//given
int powerTrainId1 = 1;
PowerTrainMappedDto powerTrain1 = PowerTrainMappedDto.builder()
.maxPs("202/3,800PS/rpm")
.maxKgfm("45.0/1,750~2,750kgf-m/rpm")
.build();
int powerTrainId2 = 2;
PowerTrainMappedDto powerTrain2 = PowerTrainMappedDto.builder()
.maxPs("295/6,000PS/rpm")
.maxKgfm("36.2/5,200kgf-m/rpm")
.build();

//when
Optional<PowerTrainMappedDto> result1 = modelRepository.findPowerTrainData(powerTrainId1);
Optional<PowerTrainMappedDto> result2 = modelRepository.findPowerTrainData(powerTrainId2);

//then
assertTrue(result1.isPresent());
assertTrue(result2.isPresent());
assertEquals(powerTrain1, result1.get());
assertEquals(powerTrain2, result2.get());
}

@Test
@DisplayName("파워트레인과 구동방식이 조합된 HMG 데이터를 가져온다.")
void getPowerTrainOperationEfficiency() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,33 @@ void getModelTypeData() {
trimModelList.add(ModelShortMappedDto.builder()
.modelId(1)
.modelName("디젤 2.2")
.modelTypeId(1)
.modelPrice(0L)
.isDefaultModel(true)
.modelTypeName("파워트레인")
.modelBoughtCount(1800L)
.maxPs("202/3800")
.maxKgfm("45.0/1750~2750")
.build());

//가솔린 3.8 데이터
trimModelList.add(ModelShortMappedDto.builder()
.modelId(2)
.modelName("가솔린 3.8")
.modelTypeId(1)
.modelPrice(280000L)
.isDefaultModel(false)
.modelTypeName("파워트레인")
.modelBoughtCount(1900L)
.maxKgfm("36.2/5200")
.maxPs("295/6000")
.build());

//7인승 데이터
trimModelList.add(ModelShortMappedDto.builder()
.modelId(3)
.modelName("7인승")
.modelTypeId(3)
.modelPrice(0L)
.isDefaultModel(true)
.modelTypeName("바디타입")
Expand All @@ -76,6 +83,7 @@ void getModelTypeData() {
trimModelList.add(ModelShortMappedDto.builder()
.modelId(4)
.modelName("8인승")
.modelTypeId(3)
.modelPrice(130000L)
.isDefaultModel(false)
.modelTypeName("바디타입")
Expand All @@ -86,6 +94,7 @@ void getModelTypeData() {
trimModelList.add(ModelShortMappedDto.builder()
.modelId(5)
.modelName("2WD")
.modelTypeId(2)
.modelPrice(0L)
.isDefaultModel(true)
.modelTypeName("구동방식")
Expand All @@ -96,6 +105,7 @@ void getModelTypeData() {
trimModelList.add(ModelShortMappedDto.builder()
.modelId(6)
.modelName("4WD")
.modelTypeId(2)
.modelPrice(237000L)
.isDefaultModel(false)
.modelTypeName("구동방식")
Expand Down Expand Up @@ -140,25 +150,6 @@ void getModelDetailData() {
softAssertions.assertThatThrownBy(() -> modelService.getModelDetail(4)).isInstanceOf(RuntimeException.class);
}

@Test
@DisplayName("파워트레인의 HMG 데이터 반환")
void getPowerTrainData() {
int powerTrainId1 = 1;
PowerTrainMappedDto powerTrain1 = PowerTrainMappedDto.builder()
.maxPs("202/3,800PS/rpm")
.maxKgfm("45.0/1,750~2,750kgf-m/rpm")
.build();

int powerTrainId2 = 4;

when(modelRepository.findPowerTrainData(powerTrainId1)).thenReturn(Optional.of(powerTrain1));

PowerTrainMappedDto result1 = modelService.getPowerTrainHmgData(powerTrainId1);

softAssertions.assertThat(result1).usingRecursiveComparison().isEqualTo(powerTrain1);
softAssertions.assertThatThrownBy(() -> modelService.getPowerTrainHmgData(powerTrainId2)).isInstanceOf(RuntimeException.class);
}

@Test
@DisplayName("파워트레인과 구동방식의 조합으로 HMG 데이터 반환")
void getEfficiencyData() {
Expand All @@ -168,7 +159,6 @@ void getEfficiencyData() {
.averageFuel("12.16km/s")
.displacement("2,199cc")
.build();

when(modelRepository.findEfficiencyData(powerTrainId, operationId)).thenReturn(Optional.of(data));

softAssertions.assertThat(modelService.getEfficiencyData(powerTrainId, operationId)).usingRecursiveComparison().isEqualTo(data);
Expand Down
Loading

0 comments on commit 942179b

Please sign in to comment.