diff --git a/backend/src/main/java/com/kaspi/backend/controller/UserGasRecordController.java b/backend/src/main/java/com/kaspi/backend/controller/UserGasRecordController.java index b5c4154..11ef9f1 100644 --- a/backend/src/main/java/com/kaspi/backend/controller/UserGasRecordController.java +++ b/backend/src/main/java/com/kaspi/backend/controller/UserGasRecordController.java @@ -14,10 +14,13 @@ import com.kaspi.backend.service.UserRecordService; import com.kaspi.backend.util.response.CommonResponseDto; import com.kaspi.backend.util.response.code.DefaultCode; +import com.kaspi.backend.util.response.code.ErrorCode; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindingResult; +import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; @@ -37,12 +40,15 @@ public class UserGasRecordController { @PostMapping("/user/gas-record") public ResponseEntity postUserGasRecord(@RequestBody UserGasRecordReqDto userGasRecordReqDto) { - +// if (bindingResult.hasErrors()) { +// return ResponseEntity.status(HttpStatus.BAD_REQUEST) +// .body(CommonResponseDto.toResponse(ErrorCode.PARAMETER_ERROR)); +// } //주유소 객체 얻기 GasStation gasStation = gasStationService.getGasStationByNo(userGasRecordReqDto.getGasStationNo()); //유저의 주유량 - Long userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation); + double userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation); //전국 유가 평균 받기 Long nationalAvgOilPrice = opinetService.nationalAvgOilPrice(userGasRecordReqDto.getGasType()); @@ -57,7 +63,7 @@ public ResponseEntity postUserGasRecord(@RequestBody UserGasR //유저 최종 저장 userRecordService.saveUserGasRecord(userGasRecordReqDto, gasStation, - userGasAmount, + (long) userGasAmount, usersSavingPrice); return ResponseEntity.status(HttpStatus.CREATED) diff --git a/backend/src/main/java/com/kaspi/backend/dto/UserGasRecordReqDto.java b/backend/src/main/java/com/kaspi/backend/dto/UserGasRecordReqDto.java index 0335c1f..1e9a110 100644 --- a/backend/src/main/java/com/kaspi/backend/dto/UserGasRecordReqDto.java +++ b/backend/src/main/java/com/kaspi/backend/dto/UserGasRecordReqDto.java @@ -3,6 +3,7 @@ import com.kaspi.backend.enums.GasType; import lombok.Builder; import lombok.Getter; +import org.checkerframework.checker.index.qual.Positive; import java.util.Date; @@ -11,6 +12,7 @@ public class UserGasRecordReqDto { private GasType gasType; + @Positive private Long refuelingPrice; private Long gasStationNo; } diff --git a/backend/src/main/java/com/kaspi/backend/service/UserRecordService.java b/backend/src/main/java/com/kaspi/backend/service/UserRecordService.java index 8b950ca..f3307ea 100644 --- a/backend/src/main/java/com/kaspi/backend/service/UserRecordService.java +++ b/backend/src/main/java/com/kaspi/backend/service/UserRecordService.java @@ -38,21 +38,25 @@ public class UserRecordService { /** * 사용자로 부터 받은 주유금액, 유종과 주유소 정보로 얼마만큼 넣었는지 계산하는 로직 */ - public Long calTodayUserGasAmount(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation) { + public double calTodayUserGasAmount(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation) { Optional todayGasPrice = gasDetailDao.findTodayGasPrice(gasStation.getStationNo(), userGasRecordReqDto.getGasType().name(), GasDetail.getNowDateToStr());//오늘 날짜로 계산 + validTodayGasPrice(userGasRecordReqDto, gasStation, todayGasPrice); + double userGasAmount = (double)userGasRecordReqDto.getRefuelingPrice() / todayGasPrice.get(); + log.info("사용자가 주유한 가스타입:{}, 주유량:{}", userGasRecordReqDto.getGasType().name(), userGasAmount); + return userGasAmount; + } + + private static void validTodayGasPrice(UserGasRecordReqDto userGasRecordReqDto, GasStation gasStation, Optional todayGasPrice) { if (todayGasPrice.isEmpty() || todayGasPrice.get() == 0) { log.error("DB에 오늘날짜에 해당되는 주유 가격 정보가 존재하지 않음 가스타입:{}, 주유소PK:{}", userGasRecordReqDto.getGasType().name(), gasStation.getStationNo()); throw new SqlNotFoundException(ErrorCode.SQL_NOT_FOUND); } - Long userGasAmount = (long) Math.round(userGasRecordReqDto.getRefuelingPrice() / todayGasPrice.get()); - log.info("사용자가 주유한 가스타입:{}, 주유량:{}", userGasRecordReqDto.getGasType().name(), userGasAmount); - return userGasAmount; } - public Long calUserSavingAmount(Long userRefuelingPrice, Long userGasAmount, Long nationalAvgOilPrice) { - return nationalAvgOilPrice * userGasAmount-userRefuelingPrice; + public Long calUserSavingAmount(Long userRefuelingPrice, double userGasAmount, Long nationalAvgOilPrice) { + return (long) (nationalAvgOilPrice * userGasAmount-userRefuelingPrice); } diff --git a/backend/src/test/java/com/kaspi/backend/controller/UserGasRecordControllerTest.java b/backend/src/test/java/com/kaspi/backend/controller/UserGasRecordControllerTest.java index 8c22f1e..5f6047d 100644 --- a/backend/src/test/java/com/kaspi/backend/controller/UserGasRecordControllerTest.java +++ b/backend/src/test/java/com/kaspi/backend/controller/UserGasRecordControllerTest.java @@ -77,7 +77,7 @@ void postUserGasRecord() throws Exception { when(gasStationService.getGasStationByNo(userGasRecordReqDto.getGasStationNo())).thenReturn(gasStation); when(userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation)) - .thenReturn(userGasAmount);// 유저가 총 넣은 가스 리터 수 + .thenReturn(Double.valueOf(userGasAmount));// 유저가 총 넣은 가스 리터 수 when(opinetService.nationalAvgOilPrice(userGasRecordReqDto.getGasType())).thenReturn( nationalGasAvg);//전국 리터당 평균 가격 when(userRecordService.calUserSavingAmount(userGasRecordReqDto.getRefuelingPrice(), diff --git a/backend/src/test/java/com/kaspi/backend/service/UserRecordServiceTest.java b/backend/src/test/java/com/kaspi/backend/service/UserRecordServiceTest.java index e18741d..1f4ff56 100644 --- a/backend/src/test/java/com/kaspi/backend/service/UserRecordServiceTest.java +++ b/backend/src/test/java/com/kaspi/backend/service/UserRecordServiceTest.java @@ -73,7 +73,7 @@ void calTodayUserGasAmount() { GasDetail.getNowDateToStr())) // 항상 오늘날짜로 기준 .thenReturn(Optional.of(gasolinePerLiterPrice));//가솔린 1L당 1000원 //when - Long userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation); + double userGasAmount = userRecordService.calTodayUserGasAmount(userGasRecordReqDto, gasStation); //then assertEquals(5L, userGasAmount); }