Merge remote-tracking branch 'origin/master' into frontend
# Conflicts: # cars4you/src/main/java/com/cars/car4you/api/CarsController.java # cars4you/src/main/java/com/cars/car4you/api/SimpleController.java
This commit is contained in:
commit
2f9327efa7
@ -1,27 +1,25 @@
|
||||
package com.cars.car4you.api;
|
||||
|
||||
import com.cars.car4you.dto.CarDto;
|
||||
import com.cars.car4you.model.Car;
|
||||
import com.cars.car4you.model.CarDetails;
|
||||
import com.cars.car4you.repository.CarDetailRepository;
|
||||
import com.cars.car4you.repository.CarRepository;
|
||||
import com.cars.car4you.service.CarService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/cars")
|
||||
@RequestMapping("/car/api")
|
||||
public class CarsController {
|
||||
|
||||
@Autowired
|
||||
private CarRepository carRepository;
|
||||
@Autowired
|
||||
private CarDetailRepository carDetailRepository;
|
||||
|
||||
@Autowired
|
||||
private CarService carService;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Car findOne(@PathVariable Long id) {
|
||||
@ -29,23 +27,23 @@ public class CarsController {
|
||||
.orElseThrow(NullPointerException::new);
|
||||
}
|
||||
|
||||
@GetMapping("/carmodel/{model}")
|
||||
@GetMapping("/model/{model}")
|
||||
public List<Car> findByModel(@PathVariable String model) {
|
||||
return carRepository.findByModel(model);
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
public List<Car> findByParams() {
|
||||
List<Car> carList = new ArrayList<>();
|
||||
carList.add(new Car());
|
||||
carList.add(new Car());
|
||||
carList.add(new Car());
|
||||
carList.add(new Car());
|
||||
return carList;
|
||||
}
|
||||
public List<CarDto> findByCriterion(@RequestParam(value = "fuel_type", required = false) String fuelType,
|
||||
@RequestParam(value = "seats", required = false) Integer seats,
|
||||
@RequestParam(value = "doors", required = false) Integer doors,
|
||||
@RequestParam(value = "maximum_price", required = false) Integer maxPrice,
|
||||
@RequestParam(value = "max_years_old", required = false) Integer maxYearsOld,
|
||||
@RequestParam(value = "engine_power_from", required = false) Integer enginePowerFrom,
|
||||
@RequestParam(value = "engine_power_to", required = false) Integer enginePowerTo,
|
||||
@RequestParam(value = "body_type", required = false) String bodyType,
|
||||
@RequestParam(value = "gearbox", required = false) String gearbox,
|
||||
@RequestParam(value = "engine_power_to", required = false) String driveType) {
|
||||
|
||||
@GetMapping("/carmodel/{maxPrice}/{maxYearsOld}")
|
||||
public List<Car> findByModel(@PathVariable int maxPrice, @PathVariable int maxYearsOld) {
|
||||
return carRepository.findByCriterion(maxPrice, maxYearsOld, 0,null,null,null,0,null);
|
||||
return carService.searchForCars(fuelType, seats, doors, maxPrice, maxYearsOld, enginePowerFrom, enginePowerTo, bodyType, gearbox, driveType);
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,29 @@
|
||||
package com.cars.car4you.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
|
||||
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class CarDto {
|
||||
|
||||
private String brand;
|
||||
private String model;
|
||||
private String version;
|
||||
private int year;
|
||||
private int engine_power;
|
||||
private double engine_capacity;
|
||||
private String transmission;
|
||||
private String drive;
|
||||
private String body_type;
|
||||
private int doors;
|
||||
private int seats;
|
||||
private long price_from;
|
||||
private long price_to;
|
||||
private long average;
|
||||
private double rating;
|
||||
private String fuel;
|
||||
private String[] car_pros;
|
||||
private String[] car_cons;
|
||||
}
|
||||
|
@ -13,10 +13,10 @@ public class Car {
|
||||
private CarDetails carDetails;
|
||||
|
||||
@Column(nullable = false, unique = false, insertable = false, updatable = false)
|
||||
private long car_details_id;
|
||||
private Long car_details_id;
|
||||
|
||||
@Id
|
||||
private long id;
|
||||
private Long id;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private String brand;
|
||||
@ -28,13 +28,13 @@ public class Car {
|
||||
private String version;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private int year;
|
||||
private Integer year;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private int engine_power;
|
||||
private Integer engine_power;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private double engine_capacity;
|
||||
private Double engine_capacity;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private String transmission;
|
||||
@ -46,18 +46,21 @@ public class Car {
|
||||
private String body_type;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private int doors;
|
||||
private Integer doors;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private int seats;
|
||||
private Integer seats;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private long price_from;
|
||||
private Long price_from;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private long price_to;
|
||||
private Long price_to;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private double avarage;
|
||||
private Long avarage;
|
||||
|
||||
@Column(nullable = false, unique = false)
|
||||
private String fuel;
|
||||
|
||||
}
|
||||
|
@ -37,7 +37,7 @@ public class CarDetails {
|
||||
private Long price_to;
|
||||
|
||||
@Column(nullable = true, unique = false)
|
||||
private Double average;
|
||||
private Long average;
|
||||
|
||||
@Type(type = "string-array" )
|
||||
@Column(name = "car_pros")
|
||||
|
@ -20,10 +20,11 @@ public class CarRepositoryImpl implements CustomCarRepository {
|
||||
private EntityManager em;
|
||||
|
||||
@Override
|
||||
public List<Car> findByCriterion(int maxPrice, int maxYearsOld,
|
||||
int seats, CarType carType,
|
||||
String fuelType,
|
||||
String transmission, int enginePower, String driveType) {
|
||||
public List<Car> findByCriterion(Integer maxPrice, Integer maxYearsOld,
|
||||
Integer seats, Integer doors, String bodyType,
|
||||
String fuelType, String transmission,
|
||||
Integer enginePowerFrom, Integer enginePowerTo,
|
||||
String driveType) {
|
||||
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Car> cq = criteriaBuilder.createQuery(Car.class);
|
||||
|
||||
@ -33,19 +34,17 @@ public class CarRepositoryImpl implements CustomCarRepository {
|
||||
|
||||
LocalDateTime localDateTime = LocalDateTime.now();
|
||||
|
||||
if(maxPrice != 0) {
|
||||
predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("price_to"), maxPrice));
|
||||
}
|
||||
|
||||
predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld));
|
||||
|
||||
predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("engine_power"), enginePower));
|
||||
predicates.add(criteriaBuilder.like(car.get("transmission"), "%" + transmission + "%"));
|
||||
predicates.add(criteriaBuilder.like(car.get("fuel"), "%" + fuelType + "%"));
|
||||
predicates.add(criteriaBuilder.like(car.get("drivetype"), "%" + fuelType + "%"));
|
||||
if(maxPrice != null) predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("price_to"), maxPrice));
|
||||
if(maxYearsOld != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld));
|
||||
if(enginePowerFrom != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("engine_power"), enginePowerFrom));
|
||||
if(enginePowerTo != null) predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("engine_power"), enginePowerTo));
|
||||
if(transmission != null && !transmission.isEmpty()) predicates.add(criteriaBuilder.like(car.get("transmission"), "%" + transmission + "%"));
|
||||
if(fuelType != null && !fuelType.isEmpty()) predicates.add(criteriaBuilder.like(car.get("fuel"), "%" + fuelType + "%"));
|
||||
if(driveType != null && !driveType.isEmpty()) predicates.add(criteriaBuilder.like(car.get("drivetype"), "%" + driveType + "%"));
|
||||
if(seats != null) predicates.add(criteriaBuilder.equal(car.get("seats"), seats));
|
||||
if(doors != null) predicates.add(criteriaBuilder.equal(car.get("doors"), doors));
|
||||
|
||||
cq.where(predicates.toArray(new Predicate[0]));
|
||||
|
||||
return em.createQuery(cq).getResultList();
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface CustomCarRepository {
|
||||
List<Car> findByCriterion(int maxPrice, int maxYearsOld,
|
||||
int seats, CarType carType,
|
||||
List<Car> findByCriterion(Integer maxPrice, Integer maxYearsOld,
|
||||
Integer seats, Integer doors, String bodyType,
|
||||
String fuelTypes,
|
||||
String transmission, int enginePower, String drivetype);
|
||||
String transmission, Integer enginePowerFrom, Integer enginePowerTo, String drivetype);
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,75 @@
|
||||
package com.cars.car4you.service;
|
||||
|
||||
import com.cars.car4you.dto.CarDto;
|
||||
import com.cars.car4you.model.Car;
|
||||
import com.cars.car4you.repository.CarRepository;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
public class CarService {
|
||||
|
||||
@Autowired
|
||||
CarRepository carRepository;
|
||||
|
||||
public CarDto mapCarModelToCarDto(Car car){
|
||||
CarDto carDto = new CarDto();
|
||||
|
||||
// Parametry konieczne do przeniesienia z modelu Car (tabela car)
|
||||
carDto.setYear(car.getYear());
|
||||
carDto.setEngine_power(car.getEngine_power());
|
||||
carDto.setEngine_capacity(car.getEngine_capacity());
|
||||
carDto.setFuel(car.getFuel());
|
||||
carDto.setTransmission(car.getTransmission());
|
||||
carDto.setDrive(car.getDrive());
|
||||
carDto.setBody_type(car.getBody_type());
|
||||
carDto.setDoors(car.getDoors());
|
||||
carDto.setSeats(car.getSeats());
|
||||
|
||||
// ceny
|
||||
carDto.setPrice_from(car.getPrice_from());
|
||||
carDto.setPrice_to(car.getPrice_to());
|
||||
carDto.setAverage(car.getAvarage());
|
||||
|
||||
// Parametry konieczne do przepisania z CarDetails (cars_details)
|
||||
carDto.setBrand(car.getCarDetails().getBrand());
|
||||
carDto.setModel(car.getCarDetails().getModel());
|
||||
carDto.setVersion(car.getCarDetails().getVersion());
|
||||
|
||||
// Jeśli w (cars_details) kolumny dodatkowe są wypełnione to aktualizujemy model o nowe dane
|
||||
if(car.getCarDetails().getPrice_from() != null ) {
|
||||
carDto.setPrice_from(car.getCarDetails().getPrice_from());
|
||||
carDto.setPrice_to(car.getCarDetails().getPrice_to());
|
||||
carDto.setAverage(car.getCarDetails().getAverage());
|
||||
carDto.setCar_cons(car.getCarDetails().getCar_cons());
|
||||
carDto.setCar_pros(car.getCarDetails().getCar_pros());
|
||||
carDto.setRating(car.getCarDetails().getRating());
|
||||
}
|
||||
|
||||
return carDto;
|
||||
}
|
||||
|
||||
private List<CarDto> convertListToDtoList(List<Car> cars){
|
||||
return cars.stream()
|
||||
.map(this::mapCarModelToCarDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private List<CarDto> processList(List<CarDto> carsToBeProcessed){
|
||||
// TODO do the magic, preprocess list to get better values and sort elements
|
||||
return carsToBeProcessed;
|
||||
}
|
||||
|
||||
public List<CarDto> searchForCars(String fuelType, Integer seats, Integer doors, Integer maxPrice,
|
||||
Integer maxYearsOld, Integer enginePowerFrom, Integer enginePowerTo,
|
||||
String bodyType, String gearbox, String driveType) {
|
||||
|
||||
List<Car> rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, seats, doors, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType);
|
||||
List<CarDto> carDtos = convertListToDtoList(rawCars);
|
||||
return processList(carDtos);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user