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:
Damian Michalski 2020-01-19 20:09:31 +01:00
commit 2f9327efa7
7 changed files with 149 additions and 49 deletions

View File

@ -1,27 +1,25 @@
package com.cars.car4you.api; package com.cars.car4you.api;
import com.cars.car4you.dto.CarDto;
import com.cars.car4you.model.Car; import com.cars.car4you.model.Car;
import com.cars.car4you.model.CarDetails; import com.cars.car4you.model.CarDetails;
import com.cars.car4you.repository.CarDetailRepository; import com.cars.car4you.repository.CarDetailRepository;
import com.cars.car4you.repository.CarRepository; import com.cars.car4you.repository.CarRepository;
import com.cars.car4you.service.CarService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/api/cars") @RequestMapping("/car/api")
public class CarsController { public class CarsController {
@Autowired @Autowired
private CarRepository carRepository; private CarRepository carRepository;
@Autowired
private CarDetailRepository carDetailRepository;
@Autowired
private CarService carService;
@GetMapping("/{id}") @GetMapping("/{id}")
public Car findOne(@PathVariable Long id) { public Car findOne(@PathVariable Long id) {
@ -29,23 +27,23 @@ public class CarsController {
.orElseThrow(NullPointerException::new); .orElseThrow(NullPointerException::new);
} }
@GetMapping("/carmodel/{model}") @GetMapping("/model/{model}")
public List<Car> findByModel(@PathVariable String model) { public List<Car> findByModel(@PathVariable String model) {
return carRepository.findByModel(model); return carRepository.findByModel(model);
} }
@GetMapping("/search") @GetMapping("/search")
public List<Car> findByParams() { public List<CarDto> findByCriterion(@RequestParam(value = "fuel_type", required = false) String fuelType,
List<Car> carList = new ArrayList<>(); @RequestParam(value = "seats", required = false) Integer seats,
carList.add(new Car()); @RequestParam(value = "doors", required = false) Integer doors,
carList.add(new Car()); @RequestParam(value = "maximum_price", required = false) Integer maxPrice,
carList.add(new Car()); @RequestParam(value = "max_years_old", required = false) Integer maxYearsOld,
carList.add(new Car()); @RequestParam(value = "engine_power_from", required = false) Integer enginePowerFrom,
return carList; @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}") return carService.searchForCars(fuelType, seats, doors, maxPrice, maxYearsOld, enginePowerFrom, enginePowerTo, bodyType, gearbox, driveType);
public List<Car> findByModel(@PathVariable int maxPrice, @PathVariable int maxYearsOld) {
return carRepository.findByCriterion(maxPrice, maxYearsOld, 0,null,null,null,0,null);
} }
} }

View File

@ -1,4 +1,29 @@
package com.cars.car4you.dto; package com.cars.car4you.dto;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CarDto { 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;
} }

View File

@ -13,10 +13,10 @@ public class Car {
private CarDetails carDetails; private CarDetails carDetails;
@Column(nullable = false, unique = false, insertable = false, updatable = false) @Column(nullable = false, unique = false, insertable = false, updatable = false)
private long car_details_id; private Long car_details_id;
@Id @Id
private long id; private Long id;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private String brand; private String brand;
@ -28,13 +28,13 @@ public class Car {
private String version; private String version;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private int year; private Integer year;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private int engine_power; private Integer engine_power;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private double engine_capacity; private Double engine_capacity;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private String transmission; private String transmission;
@ -46,18 +46,21 @@ public class Car {
private String body_type; private String body_type;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private int doors; private Integer doors;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private int seats; private Integer seats;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private long price_from; private Long price_from;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private long price_to; private Long price_to;
@Column(nullable = false, unique = false) @Column(nullable = false, unique = false)
private double avarage; private Long avarage;
@Column(nullable = false, unique = false)
private String fuel;
} }

View File

@ -37,7 +37,7 @@ public class CarDetails {
private Long price_to; private Long price_to;
@Column(nullable = true, unique = false) @Column(nullable = true, unique = false)
private Double average; private Long average;
@Type(type = "string-array" ) @Type(type = "string-array" )
@Column(name = "car_pros") @Column(name = "car_pros")

View File

@ -20,10 +20,11 @@ public class CarRepositoryImpl implements CustomCarRepository {
private EntityManager em; private EntityManager em;
@Override @Override
public List<Car> findByCriterion(int maxPrice, int maxYearsOld, public List<Car> findByCriterion(Integer maxPrice, Integer maxYearsOld,
int seats, CarType carType, Integer seats, Integer doors, String bodyType,
String fuelType, String fuelType, String transmission,
String transmission, int enginePower, String driveType) { Integer enginePowerFrom, Integer enginePowerTo,
String driveType) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Car> cq = criteriaBuilder.createQuery(Car.class); CriteriaQuery<Car> cq = criteriaBuilder.createQuery(Car.class);
@ -33,19 +34,17 @@ public class CarRepositoryImpl implements CustomCarRepository {
LocalDateTime localDateTime = LocalDateTime.now(); LocalDateTime localDateTime = LocalDateTime.now();
if(maxPrice != 0) { if(maxPrice != null) predicates.add(criteriaBuilder.lessThanOrEqualTo(car.get("price_to"), maxPrice));
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));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld)); 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 + "%"));
predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("engine_power"), enginePower)); if(driveType != null && !driveType.isEmpty()) predicates.add(criteriaBuilder.like(car.get("drivetype"), "%" + driveType + "%"));
predicates.add(criteriaBuilder.like(car.get("transmission"), "%" + transmission + "%")); if(seats != null) predicates.add(criteriaBuilder.equal(car.get("seats"), seats));
predicates.add(criteriaBuilder.like(car.get("fuel"), "%" + fuelType + "%")); if(doors != null) predicates.add(criteriaBuilder.equal(car.get("doors"), doors));
predicates.add(criteriaBuilder.like(car.get("drivetype"), "%" + fuelType + "%"));
cq.where(predicates.toArray(new Predicate[0])); cq.where(predicates.toArray(new Predicate[0]));
return em.createQuery(cq).getResultList(); return em.createQuery(cq).getResultList();
} }
} }

View File

@ -8,9 +8,9 @@ import java.util.List;
@Repository @Repository
public interface CustomCarRepository { public interface CustomCarRepository {
List<Car> findByCriterion(int maxPrice, int maxYearsOld, List<Car> findByCriterion(Integer maxPrice, Integer maxYearsOld,
int seats, CarType carType, Integer seats, Integer doors, String bodyType,
String fuelTypes, String fuelTypes,
String transmission, int enginePower, String drivetype); String transmission, Integer enginePowerFrom, Integer enginePowerTo, String drivetype);
} }

View File

@ -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 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);
}
}