Skip to content

Commit

Permalink
Merge pull request #408 from softeerbootcamp-2nd/dev
Browse files Browse the repository at this point in the history
MAIN MERGE
  • Loading branch information
tank3a authored Aug 22, 2023
2 parents 849b062 + f7b001e commit d4e1c46
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 3 deletions.
Binary file modified backend/src/.DS_Store
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package autoever2.cartag.controller;

import autoever2.cartag.domain.car.BoughtCarDto;
import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.quote.HistoryRequestDto;
import autoever2.cartag.domain.quote.HistoryShortDto;
import autoever2.cartag.domain.quote.QuoteDataDto;
import autoever2.cartag.domain.quote.QuoteInfoDto;
Expand All @@ -16,6 +18,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/quote")
Expand Down Expand Up @@ -57,4 +60,15 @@ public QuoteInfoDto getQuoteDetail(@Parameter(description = "선택한 id 리스
QuoteInfoDto data = carService.findShareInfoDto(idList);
return data;
}

@Operation(summary = "유사견적 상세 데이터 제공 API", description = "유사 견적 ID 제공 시 상세 데이터 제공")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = HistoryShortDto.class)))
})
@PostMapping("/histories/detail")
public List<List<QuoteSubOptionDto>> getRecommendedOptions(@RequestBody HistoryRequestDto historyRequestDto) {
List<Integer> myOptionIds = quoteService.getHistoryById(historyRequestDto.getQuoteId());

return historyRequestDto.getHistoryIds().stream().map(historyId -> quoteService.getOptionDifference(myOptionIds, historyId)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package autoever2.cartag.domain.quote;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;

@Getter
@NoArgsConstructor
public class HistoryRequestDto {

private Long quoteId;
private List<Long> historyIds;

@Builder
public HistoryRequestDto(Long quoteId, List<Long> historyIds) {
this.quoteId = quoteId;
this.historyIds = historyIds;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public QuoteDataDto(int carId, int powerTrainId, int bodyTypeId, int operationId
this.operationId = operationId;
this.outerColorId = outerColorId;
this.innerColorId = innerColorId;
this.optionIdList = optionIdList;
this.optionIdList = new ArrayList<>();
this.optionIdList.addAll(optionIdList);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
@RequiredArgsConstructor
public enum ErrorCode {
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "잘못된 파라미터 형식입니다."),
INVALID_PARAMETER(HttpStatus.BAD_REQUEST, "잘못된 파라미터입니다."),
INVALID_OPTIONS_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 옵션으로 접근하였습니다."),
DATA_NOT_EXISTS(HttpStatus.NOT_FOUND, "데이터가 존재하지 않습니다."),
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버오류입니다. 관리자에게 문의하세요."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public List<List<Integer>> request(QuoteDataDto quoteDataDto) {
}


public String getJsonFromEstimate(QuoteDataDto quoteDataDto) {
private String getJsonFromEstimate(QuoteDataDto quoteDataDto) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("carId", quoteDataDto.getCarId());
jsonObject.put("powerTrain", quoteDataDto.getPowerTrainId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import autoever2.cartag.domain.quote.HistorySearchDto;
import autoever2.cartag.domain.quote.HistoryShortDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
Expand All @@ -11,7 +14,10 @@
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.StringTokenizer;

@Repository
public class QuoteRepository {
Expand Down Expand Up @@ -43,4 +49,33 @@ public Optional<HistoryShortDto> findShortData(HistorySearchDto historySearchDto
private RowMapper<HistoryShortDto> historyShortRowMapper() {
return BeanPropertyRowMapper.newInstance(HistoryShortDto.class);
}

public List<Integer> findOptionListFromHistoryId(Long historyId) {
String sql = "select sold_options_id " +
"from SalesHistory " +
"where history_id = :historyId";

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

String optionIds = null;
try {
optionIds = template.queryForObject(sql, param, String.class);
} catch (DataAccessException e) {
throw new EmptyDataException(ErrorCode.INVALID_PARAMETER);
}

return mapToList(optionIds);

}

private List<Integer> mapToList(String optionIds) {
StringTokenizer token = new StringTokenizer(",");
List<Integer> optionIdList = new ArrayList<>();
while (token.hasMoreTokens()) {
optionIdList.add(Integer.parseInt(token.nextToken()));
}

return optionIdList;
}
}
17 changes: 17 additions & 0 deletions backend/src/main/java/autoever2/cartag/service/QuoteService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package autoever2.cartag.service;

import autoever2.cartag.domain.option.QuoteSubOptionDto;
import autoever2.cartag.domain.quote.HistorySearchDto;
import autoever2.cartag.domain.quote.HistoryShortDto;
import autoever2.cartag.domain.quote.QuoteDataDto;
import autoever2.cartag.exception.EmptyDataException;
import autoever2.cartag.exception.ErrorCode;
import autoever2.cartag.exception.InvalidDataException;
import autoever2.cartag.recommend.RecommendConnector;
Expand Down Expand Up @@ -38,6 +40,10 @@ public HistoryShortDto findMyQuote(QuoteDataDto quoteDataDto) {
public List<HistoryShortDto> findTopHistory(QuoteDataDto quoteDataDto) {

List<Integer> optionIds = quoteDataDto.getOptionIdList();

if(optionIds.isEmpty()) {
throw new InvalidDataException(ErrorCode.INVALID_PARAMETER);
}
if(optionIds.size() != optionRepository.countExistOptions(quoteDataDto.getCarId(), optionIds)) {
throw new InvalidDataException(ErrorCode.INVALID_OPTIONS_REQUEST);
}
Expand All @@ -61,4 +67,15 @@ public List<HistoryShortDto> findTopHistory(QuoteDataDto quoteDataDto) {
quoteRepository.findShortData(historySearchDto).orElse(null)
).collect(Collectors.toList());
}

public List<Integer> getHistoryById(Long historyId) {
return quoteRepository.findOptionListFromHistoryId(historyId);
}

public List<QuoteSubOptionDto> getOptionDifference(List<Integer> optionsToCompare, Long historyId) {
List<Integer> historyOptions = quoteRepository.findOptionListFromHistoryId(historyId);
historyOptions = historyOptions.stream().filter(id -> !optionsToCompare.contains(id)).collect(Collectors.toList());

return historyOptions.stream().map(id -> optionRepository.findSubOptionByOptionId(id).orElseThrow(() -> new EmptyDataException(ErrorCode.INTERNAL_SERVER_ERROR))).collect(Collectors.toList());
}
}

0 comments on commit d4e1c46

Please sign in to comment.