-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from softeerbootcamp-2nd/dev
MAIN MERGE
- Loading branch information
Showing
48 changed files
with
997 additions
and
1,105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package autoever2.cartag; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.cache.CacheManager; | ||
import org.springframework.cache.annotation.EnableCaching; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.data.redis.cache.RedisCacheConfiguration; | ||
import org.springframework.data.redis.cache.RedisCacheManager; | ||
import org.springframework.data.redis.connection.RedisConnectionFactory; | ||
import org.springframework.data.redis.connection.RedisStandaloneConfiguration; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
|
||
import java.time.Duration; | ||
|
||
@EnableCaching | ||
@Configuration | ||
@PropertySource("classpath:application.yml") | ||
public class RedisConfig { | ||
|
||
@Value("${spring.redis.host}") // application.properties 에서 불러옴 | ||
private String host; | ||
|
||
@Value("${spring.redis.port}") // application.properties 에서 불러옴 | ||
private int port; | ||
|
||
@Value("${spring.redis.password}") | ||
private String password; | ||
|
||
@Bean | ||
public RedisConnectionFactory redisConnectionFactory() { | ||
RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); | ||
redisStandaloneConfiguration.setHostName(host); | ||
redisStandaloneConfiguration.setPort(port); | ||
redisStandaloneConfiguration.setPassword(password); | ||
return new LettuceConnectionFactory(redisStandaloneConfiguration); | ||
} | ||
|
||
@Bean | ||
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) { | ||
RedisCacheManager.RedisCacheManagerBuilder builder = RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory()); | ||
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig() | ||
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new GenericJackson2JsonRedisSerializer())) | ||
.entryTtl(Duration.ofHours(12)); // TTL 12시간으로 지정 | ||
builder.cacheDefaults(configuration); | ||
|
||
return builder.build(); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
backend/src/main/java/autoever2/cartag/cars/CarController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package autoever2.cartag.cars; | ||
|
||
import autoever2.cartag.cars.dto.CarDefaultDto; | ||
import autoever2.cartag.cars.dto.CarVo; | ||
import autoever2.cartag.cars.dto.CarTypeDto; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("api/cars") | ||
@RequiredArgsConstructor | ||
@Tag(name = "차량 관련 API", description = "차종 및 차량 트림, 차량의 기본 정보를 제공합니다.") | ||
public class CarController { | ||
|
||
private final CarService service; | ||
|
||
|
||
@Operation(summary = "차종에 속한 모든 트림을 조회하는 API", description = "차종 ID를 통해 르블랑, 익스클루시브 등 차종에 속한 모든 트림을 반환하는 API입니다.") | ||
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = CarVo.class))) | ||
@GetMapping("/types") | ||
public List<CarVo> carTrimInfo(@Parameter(description = "차종 ID") @RequestParam("cartype") int carType) { | ||
return service.getCarDtoByCarType(carType); | ||
} | ||
|
||
@Operation(summary = "차종 리스트를 조회하는 API", description = "팰리세이드, 베뉴 등 차종 리스트 및 이미지를 반환하는 API입니다.") | ||
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = CarTypeDto.class))) | ||
@GetMapping("/list") | ||
public List<CarTypeDto> getCarTypeList() { | ||
return service.getAllCarTypes(); | ||
} | ||
|
||
@Operation(summary = "트림의 기본 정보를 조회하는 API", description = "트림의 기본 옵션, 기본 모델타입을 반환하는 API입니다.") | ||
@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = CarDefaultDto.class))) | ||
@GetMapping("/infos/defaults") | ||
public CarDefaultDto carDefaultDto(@Parameter(description = "트림 ID") @RequestParam("carid") int carId) { | ||
return service.getCarDefaultDtoByCarId(carId); | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
backend/src/main/java/autoever2/cartag/cars/CarRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package autoever2.cartag.cars; | ||
|
||
import autoever2.cartag.cars.dto.CarInfoDto; | ||
import autoever2.cartag.cars.dto.CarTypeDto; | ||
import autoever2.cartag.cars.dto.TrimInfoDto; | ||
import org.springframework.dao.support.DataAccessUtils; | ||
import org.springframework.jdbc.core.BeanPropertyRowMapper; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; | ||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; | ||
import org.springframework.jdbc.core.namedparam.SqlParameterSource; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import javax.sql.DataSource; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Repository | ||
public class CarRepository { | ||
|
||
private final NamedParameterJdbcTemplate template; | ||
|
||
public CarRepository(DataSource dataSource) { | ||
template = new NamedParameterJdbcTemplate(dataSource); | ||
} | ||
|
||
public List<CarInfoDto> findCarByCarType(int carType) { | ||
String sql = "select car_id, trim, car_default_price, outer_image, inner_image, wheel_image, car_description " + | ||
"from Car " + | ||
"where car_type_id = :carType"; | ||
|
||
SqlParameterSource param = new MapSqlParameterSource() | ||
.addValue("carType", carType); | ||
|
||
return template.query(sql, param, carRowMapper()); | ||
} | ||
|
||
public Optional<Long> findCarBoughtCountByCarId(int carId) { | ||
String sql = "select bought_count " + | ||
"from Car " + | ||
"where car_id = :carId"; | ||
|
||
SqlParameterSource param = new MapSqlParameterSource() | ||
.addValue("carId", carId); | ||
|
||
return Optional.ofNullable(DataAccessUtils.singleResult(template.query(sql, param, longMapper()))); | ||
} | ||
|
||
public List<CarTypeDto> findAllCarType() { | ||
String sql = "select car_type_id, car_type_name, car_type_image " + | ||
"from CarType"; | ||
|
||
return template.query(sql, carTypeDtoRowMapper()); | ||
} | ||
|
||
public Optional<TrimInfoDto> findTrimInfoByCarId(int carId){ | ||
String sql = "select car_id, trim, car_default_price " + | ||
"from Car " + | ||
"where car_id = :carId"; | ||
|
||
SqlParameterSource param = new MapSqlParameterSource() | ||
.addValue("carId", carId); | ||
|
||
return Optional.ofNullable(DataAccessUtils.singleResult(template.query(sql, param, trimInfoRowMapper()))); | ||
} | ||
|
||
public Optional<Integer> findCarPriceByCarId(int carId) { | ||
String sql = "select car_default_price " + | ||
"from Car " + | ||
"where car_id = :carId"; | ||
|
||
SqlParameterSource param = new MapSqlParameterSource() | ||
.addValue("carId", carId); | ||
|
||
return Optional.ofNullable(DataAccessUtils.singleResult(template.query(sql, param, intMapper()))); | ||
} | ||
|
||
private RowMapper<CarTypeDto> carTypeDtoRowMapper() { | ||
return BeanPropertyRowMapper.newInstance(CarTypeDto.class); | ||
} | ||
|
||
private RowMapper<CarInfoDto> carRowMapper() { | ||
return BeanPropertyRowMapper.newInstance(CarInfoDto.class); | ||
} | ||
|
||
private RowMapper<Long> longMapper() { | ||
return (rs, rowNum) -> rs.getLong("bought_count"); | ||
} | ||
|
||
private RowMapper<Integer> intMapper() { | ||
return (rs, rowNum) -> rs.getInt("car_default_price"); | ||
} | ||
|
||
private RowMapper<TrimInfoDto> trimInfoRowMapper() { | ||
return BeanPropertyRowMapper.newInstance(TrimInfoDto.class); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
backend/src/main/java/autoever2/cartag/cars/CarService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package autoever2.cartag.cars; | ||
|
||
import autoever2.cartag.cars.dto.*; | ||
import autoever2.cartag.domain.color.InnerColorDto; | ||
import autoever2.cartag.domain.color.OuterColorDto; | ||
import autoever2.cartag.domain.model.ModelDefaultDto; | ||
import autoever2.cartag.exception.EmptyDataException; | ||
import autoever2.cartag.exception.ErrorCode; | ||
import autoever2.cartag.repository.ColorRepository; | ||
import autoever2.cartag.repository.ModelRepository; | ||
import autoever2.cartag.repository.OptionRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static autoever2.cartag.parser.ImageUrlParser.changeUrl; | ||
|
||
@Service | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class CarService { | ||
|
||
private final CarRepository carRepository; | ||
private final OptionRepository optionRepository; | ||
private final ColorRepository colorRepository; | ||
private final ModelRepository modelRepository; | ||
|
||
public List<CarTypeDto> getAllCarTypes() { | ||
return carRepository.findAllCarType(); | ||
} | ||
|
||
public List<CarVo> getCarDtoByCarType(int carType) { | ||
List<CarInfoDto> carInfos = carRepository.findCarByCarType(carType); | ||
if (carInfos.isEmpty()) { | ||
throw new EmptyDataException(ErrorCode.DATA_NOT_EXISTS); | ||
} | ||
|
||
return carInfos.stream() | ||
.map(carInfoDto -> CarVo.toVo(carInfoDto, optionRepository.findDefaultOptionByCarId(carInfoDto.getCarId()))) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public CarDefaultDto getCarDefaultDtoByCarId(int carId) { | ||
List<OuterColorDto> outerColorList = colorRepository.findOuterColorCarByCarId(carId); | ||
List<InnerColorDto> innerColorList = colorRepository.findInnerColorCarByCarId(carId); | ||
List<ModelDefaultDto> modelList = modelRepository.findModelDefaultDtoByCarId(carId); | ||
if (outerColorList.isEmpty() || innerColorList.isEmpty() || modelList.isEmpty()) { | ||
throw new EmptyDataException(ErrorCode.DATA_NOT_EXISTS); | ||
} | ||
String outerImageUrl = changeUrl(outerColorList.get(0).getColorCarImage()); | ||
|
||
return CarDefaultDto.toDefault(outerColorList.get(0), innerColorList.get(0), modelList, outerImageUrl); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.