Skip to content

Commit

Permalink
Merge pull request #279 from softeerbootcamp-2nd/dev
Browse files Browse the repository at this point in the history
main merge
  • Loading branch information
dydwo0740 authored Aug 16, 2023
2 parents 170ec05 + 3551014 commit 75fbc08
Show file tree
Hide file tree
Showing 19 changed files with 106 additions and 25 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ name: Deploy to Amazon EC2
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
paths:
- '**.java'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ColorController {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = OuterColorDto.class))),
})
@GetMapping("/colors/outer")
public List<OuterColorDto> carOuterColorInfo(@Parameter(description = "선택한 car_id") @RequestParam int carId) {
public List<OuterColorDto> carOuterColorInfo(@Parameter(description = "선택한 car_id") @RequestParam("carid") int carId) {
return service.findOuterColorByCarId(carId);
}

Expand All @@ -40,7 +40,7 @@ public List<OuterColorDto> carOuterColorInfo(@Parameter(description = "선택한
@ApiResponse(responseCode = "200", description = "조회 성공")
})
@GetMapping("/colors/outer/images")
public List<String> carOuterColorImageInfo(@Parameter(description = "선택한 color_id") @RequestParam int colorId) {
public List<String> carOuterColorImageInfo(@Parameter(description = "선택한 color_id") @RequestParam("colorid") int colorId) {
return service.changeImageToImages(colorId);
}

Expand All @@ -49,7 +49,7 @@ public List<String> carOuterColorImageInfo(@Parameter(description = "선택한 c
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = InnerColorDto.class))),
})
@GetMapping("/colors/inner")
public List<InnerColorDto> carInnerColorInfo(@Parameter(description = "선택한 car_id") @RequestParam int carId) {
public List<InnerColorDto> carInnerColorInfo(@Parameter(description = "선택한 car_id") @RequestParam("carid") int carId) {
return service.findInnerColorByCarId(carId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class TrimController {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = CarDto.class))),
})
@GetMapping("/types")
public List<CarDto> carTrimInfo(@Parameter(description = "선택한 car_type") @RequestParam int carType) {
public List<CarDto> carTrimInfo(@Parameter(description = "선택한 car_type") @RequestParam("cartype") int carType) {
return service.findCarByCarType(carType);
}

Expand Down
3 changes: 3 additions & 0 deletions backend/src/main/java/autoever2/cartag/domain/car/CarDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
@Builder
@Schema(description = "차량 반환 DTO")
public class CarDto {
@Schema(description = "car_id 명")
private int carId;
@Schema(description = "trim 명", example = "Le Blanc")
private String trim;
@Schema(description = "차량의 기본 가격")
Expand All @@ -27,6 +29,7 @@ public class CarDto {

public static CarDto toDto(CarInfoDto carInfoDto, List<TrimDefaultOptionDto> optionDtos) {
return CarDto.builder()
.carId(carInfoDto.getCarId())
.trim(carInfoDto.getTrim())
.carDefaultPrice(carInfoDto.getCarDefaultPrice())
.outerImage(carInfoDto.getOuterImage())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@NoArgsConstructor
@Schema(description = "차량 외부 색상 반환 DTO")
public class OuterColorDto {
@Schema(description = "외부 색상 이미지 id")
private int colorId;
@Schema(description = "외부 색상 이름")
private String colorName;
@Schema(description = "외부 색상 이미지 url")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package autoever2.cartag.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public class EmptyDataException extends RuntimeException {
private final ErrorCode errorCode;
}
16 changes: 16 additions & 0 deletions backend/src/main/java/autoever2/cartag/exception/ErrorCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package autoever2.cartag.exception;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;

@Getter
@RequiredArgsConstructor
public enum ErrorCode {
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "Invalid parameter included"),
RESOURCE_NOT_FOUND(HttpStatus.NOT_FOUND, "Resource not exists"),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "Unknown error");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package autoever2.cartag.exception;

import lombok.Builder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@Builder
@RequiredArgsConstructor
public class ErrorResponse {
private final String code;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package autoever2.cartag.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler(EmptyDataException.class)
public ResponseEntity<ErrorResponse> handleEmptyDataException(EmptyDataException e) {
ErrorCode errorCode = e.getErrorCode();
return ResponseEntity.status(errorCode.getHttpStatus()).body(ErrorResponse.builder()
.code(errorCode.toString())
.message(errorCode.getMessage())
.build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public List<InnerColorDto> findInnerColorCarByCarId(int carId) {
}

public List<OuterColorDto> findOuterColorCarByCarId(int carId) {
String sql = "select color_name, color_image, color_price, color_bought_count " +
String sql = "select c.color_id, color_name, color_image, color_price, color_bought_count " +
"from ColorCarMapper as cm inner join Color as c " +
"on cm.color_id = c.color_id where car_id = :carId and c.is_outer_color = 1";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import autoever2.cartag.domain.car.CarDto;
import autoever2.cartag.domain.car.CarInfoDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.CarRepository;
import autoever2.cartag.repository.OptionRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -23,7 +25,7 @@ public class CarService {
public List<CarDto> findCarByCarType(int carType) {
List<CarInfoDto> carInfos = carRepository.findCarByCarType(carType);
if(carInfos.isEmpty()){
throw new RuntimeException("미정");
throw new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND);
}

return carInfos.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import autoever2.cartag.domain.color.InnerColorDto;
import autoever2.cartag.domain.color.OuterColorDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.ColorRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -22,7 +24,7 @@ public class ColorService {
public List<String> changeImageToImages(int colorId) {
Optional<String> images = repository.findOuterColorImagesByColorId(colorId);
if (images.isEmpty()) {
throw new RuntimeException("미정");
throw new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND);
}
List<String> outerColorCarImages = new ArrayList<>();
String value = images.get();
Expand All @@ -36,7 +38,7 @@ public List<String> changeImageToImages(int colorId) {
public List<OuterColorDto> findOuterColorByCarId(int carId) {
List<OuterColorDto> outerColors = repository.findOuterColorCarByCarId(carId);
if (outerColors.isEmpty()) {
throw new RuntimeException("미정");
throw new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND);
}

return outerColors;
Expand All @@ -45,7 +47,7 @@ public List<OuterColorDto> findOuterColorByCarId(int carId) {
public List<InnerColorDto> findInnerColorByCarId(int carId) {
List<InnerColorDto> innerColors = repository.findInnerColorCarByCarId(carId);
if (innerColors.isEmpty()) {
throw new RuntimeException("미정");
throw new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND);
}

return innerColors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package autoever2.cartag.service;

import autoever2.cartag.domain.model.*;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.CarRepository;
import autoever2.cartag.repository.ModelRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -108,11 +110,11 @@ private Double calculateHmgString(String input) {

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

//TODO: RuntimeException 처리
public ModelEfficiencyDataDto getEfficiencyData(int powerTrainId, int operationId) {
return modelRepository.findEfficiencyData(powerTrainId, operationId).orElseThrow(() -> new RuntimeException("ID가 일치하지 않습니다,"));
return modelRepository.findEfficiencyData(powerTrainId, operationId).orElseThrow(() -> new EmptyDataException(ErrorCode.INVALID_PARAMETER));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package autoever2.cartag.service;

import autoever2.cartag.domain.option.*;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.CarRepository;
import autoever2.cartag.repository.OptionRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -55,7 +57,7 @@ public List<DefaultOptionDto> getDefaultOptionList(int carId) {

//TODO: RuntimeException 처리
public OptionDetailDto getOptionDetailData(int carId, int optionId, boolean isDefault) {
OptionDetailMappedDto detail = optionRepository.findOptionDetail(carId, optionId, isDefault).orElseThrow(() -> new RuntimeException("데이터가 존재하지 않습니다."));
OptionDetailMappedDto detail = optionRepository.findOptionDetail(carId, optionId, isDefault).orElseThrow(() -> new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND));

List<OptionDetailMappedDto> packageSubOptions = optionRepository.findPackageSubOptions(optionId);

Expand All @@ -68,7 +70,7 @@ public OptionDetailDto getOptionDetailData(int carId, int optionId, boolean isDe
.build();

if(detail.getOptionBoughtCount() != null) {
Long totalBoughtCount = carRepository.findCarBoughtCountByCarId(carId).orElseThrow(() -> new RuntimeException("알수 없는 에러"));
Long totalBoughtCount = carRepository.findCarBoughtCountByCarId(carId).orElseThrow(() -> new EmptyDataException(ErrorCode.INTERNAL_SERVER_ERROR));
result.getHmgData().setOverHalf(detail.getOptionBoughtCount() * 2 > totalBoughtCount);
}
if(!packageSubOptions.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ void getTrimColor() throws Exception {
given(service.changeImageToImages(colorId)).willReturn(images);

//when
ResultActions resultActionsOuter = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/outer/").param("carId", String.valueOf(carId)));
ResultActions resultActionsInner = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/inner/").param("carId", String.valueOf(carId)));
ResultActions resultActionsImages = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/outer/images").param("colorId", String.valueOf(colorId)));
ResultActions resultActionsOuter = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/outer/").param("carid", String.valueOf(carId)));
ResultActions resultActionsInner = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/inner/").param("carid", String.valueOf(carId)));
ResultActions resultActionsImages = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/colors/outer/images").param("colorid", String.valueOf(colorId)));
//then
resultActionsOuter.andExpect(status().isOk())
.andExpect(jsonPath("$[0].colorName").value("어비스 블랙펄"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void getTrimList() throws Exception {
given(service.findCarByCarType(carType)).willReturn(carDtoList);

//when
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/types").param("carType", String.valueOf(carType)));
ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders.get("/api/cars/types").param("cartype", String.valueOf(carType)));

System.out.println("jsonPath(\"$[3].options[0]\") = " + jsonPath("$[3].options[0]"));
//then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import autoever2.cartag.domain.car.CarDto;
import autoever2.cartag.domain.car.CarInfoDto;
import autoever2.cartag.domain.car.TrimDefaultOptionDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.CarRepository;
import autoever2.cartag.repository.OptionRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand All @@ -16,6 +19,7 @@
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -116,13 +120,13 @@ void getCarType() {

when(carRepository.findCarByCarType(carType)).thenReturn(carInfoDtoList);
when(optionRepository.findDefaultOptionByCarId(carId)).thenReturn(trimDefaultOptionDtoList);
when(carRepository.findCarByCarType(2)).thenThrow(new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND));

List<CarDto> carByCarType = service.findCarByCarType(carType);

assertEquals(carByCarType.size(), 4);
assertEquals(carByCarType.get(0).getOptions().size(), 3);


assertThatThrownBy(() -> service.findCarByCarType(2)).isInstanceOf(EmptyDataException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import autoever2.cartag.domain.color.InnerColorDto;
import autoever2.cartag.domain.color.OuterColorDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.repository.ColorRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
Expand All @@ -15,6 +17,7 @@
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -125,7 +128,9 @@ void getModelTypeData() {
when(repository.findInnerColorCarByCarId(carId)).thenReturn(innerColors);
when(repository.findOuterColorCarByCarId(carId)).thenReturn(outerColors);
when(repository.findOuterColorImagesByColorId(colorId)).thenReturn(Optional.of("red_image_*.jpg"));

when(repository.findOuterColorImagesByColorId(2)).thenThrow(new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND));
when(repository.findOuterColorCarByCarId(2)).thenThrow(new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND));
when(repository.findInnerColorCarByCarId(2)).thenThrow(new EmptyDataException(ErrorCode.RESOURCE_NOT_FOUND));
//when
List<OuterColorDto> result_outer = service.findOuterColorByCarId(carId);
List<InnerColorDto> result_inner = service.findInnerColorByCarId(carId);
Expand All @@ -134,7 +139,10 @@ void getModelTypeData() {
//then
assertEquals(result_outer.size(), 5);
assertEquals(result_inner.size(), 5);
assertEquals(images.size(), 60);
assertEquals(imageFiles.size(), 60);
assertThatThrownBy(() -> service.changeImageToImages(2)).isInstanceOf(EmptyDataException.class);
assertThatThrownBy(() -> service.findInnerColorByCarId(2)).isInstanceOf(EmptyDataException.class);
assertThatThrownBy(() -> service.findOuterColorByCarId(2)).isInstanceOf(EmptyDataException.class);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package autoever2.cartag.service;

import autoever2.cartag.domain.model.*;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.repository.CarRepository;
import autoever2.cartag.repository.ModelRepository;
import org.assertj.core.api.SoftAssertions;
Expand Down Expand Up @@ -149,7 +150,7 @@ void getModelDetailData() {
ModelDetailMappedDto result1 = modelService.getModelDetail(modelId1);

softAssertions.assertThat(result1).usingRecursiveComparison().isEqualTo(model1);
softAssertions.assertThatThrownBy(() -> modelService.getModelDetail(4)).isInstanceOf(RuntimeException.class);
softAssertions.assertThatThrownBy(() -> modelService.getModelDetail(4)).isInstanceOf(EmptyDataException.class);
}

@Test
Expand All @@ -164,6 +165,6 @@ void getEfficiencyData() {
when(modelRepository.findEfficiencyData(powerTrainId, operationId)).thenReturn(Optional.of(data));

softAssertions.assertThat(modelService.getEfficiencyData(powerTrainId, operationId)).usingRecursiveComparison().isEqualTo(data);
softAssertions.assertThatThrownBy(() -> modelService.getEfficiencyData(2, 4)).isInstanceOf(RuntimeException.class);
softAssertions.assertThatThrownBy(() -> modelService.getEfficiencyData(2, 4)).isInstanceOf(EmptyDataException.class);
}
}

0 comments on commit 75fbc08

Please sign in to comment.