diff --git a/cars4you/pom.xml b/cars4you/pom.xml index e3222ec..223933e 100644 --- a/cars4you/pom.xml +++ b/cars4you/pom.xml @@ -55,6 +55,12 @@ 2.8.0 + + + org.apache.commons + commons-math3 + 3.5 + diff --git a/cars4you/src/main/client/html/js/sendingRequest.js b/cars4you/src/main/client/html/js/sendingRequest.js index f8a3e28..7873ac9 100644 --- a/cars4you/src/main/client/html/js/sendingRequest.js +++ b/cars4you/src/main/client/html/js/sendingRequest.js @@ -11,11 +11,10 @@ let gearbox = document.getElementById('gearbox'); let drive_type = document.getElementById('drive_type'); let request = new XMLHttpRequest(); - // let url = "https://jsonplaceholder.typicode.com/photos"; - // let url = "https://webhook.site/b1792c3d-cd5a-4c29-8e9c-1ff9f84653eb"; - // let url = "http://34.65.132.148:8080"; - let url = "http://localhost:8080/api/cars/search"; + + let url = "http://localhost:8080/car/api/search"; var params = []; + if(fuel_type.value != -1){ params.push("fuel_type="+fuel_type.options[fuel_type.value].text); } @@ -46,6 +45,7 @@ if(drive_type.value != -1){ params.push("drive_type="+drive_type.value); } + var buff = ""; if(params.length>0){ buff += "?"; diff --git a/cars4you/src/main/client/html/search.html b/cars4you/src/main/client/html/search.html index 86026b7..da23e14 100644 --- a/cars4you/src/main/client/html/search.html +++ b/cars4you/src/main/client/html/search.html @@ -132,8 +132,8 @@ Skrzynia biegów: @@ -143,9 +143,9 @@ Rodzaj napędu: diff --git a/cars4you/src/main/java/com/cars/car4you/api/CarsController.java b/cars4you/src/main/java/com/cars/car4you/api/CarsController.java index 37976f2..4613513 100644 --- a/cars4you/src/main/java/com/cars/car4you/api/CarsController.java +++ b/cars4you/src/main/java/com/cars/car4you/api/CarsController.java @@ -2,8 +2,6 @@ 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; diff --git a/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java b/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java index 523d9a0..05027ef 100644 --- a/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java +++ b/cars4you/src/main/java/com/cars/car4you/api/SimpleController.java @@ -27,7 +27,6 @@ public class SimpleController { @GetMapping("/{name}") public byte[] getPageByName(@PathVariable String name) throws IOException { - System.out.println("Wczytuję stronę: " + name); byte[] array = Files.readAllBytes(Paths.get("src/main/client/html/" + name)); return array; diff --git a/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java b/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java index 4d16cc1..be51dee 100644 --- a/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java +++ b/cars4you/src/main/java/com/cars/car4you/dto/CarDto.java @@ -11,19 +11,172 @@ public class CarDto { private String brand; private String model; private String version; - private int year; - private int engine_power; - private double engine_capacity; + private Integer year; + private Integer 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 Integer doors; + private Integer 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; + private Double fuelEconomy; + + public Double getFuelEconomy() { + return fuelEconomy; + } + + public void setFuelEconomy(Double fuelEconomy) { + this.fuelEconomy = fuelEconomy; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Integer getEngine_power() { + return engine_power; + } + + public void setEngine_power(Integer engine_power) { + this.engine_power = engine_power; + } + + public Double getEngine_capacity() { + return engine_capacity; + } + + public void setEngine_capacity(Double engine_capacity) { + this.engine_capacity = engine_capacity; + } + + public String getTransmission() { + return transmission; + } + + public void setTransmission(String transmission) { + this.transmission = transmission; + } + + public String getDrive() { + return drive; + } + + public void setDrive(String drive) { + this.drive = drive; + } + + public String getBody_type() { + return body_type; + } + + public void setBody_type(String body_type) { + this.body_type = body_type; + } + + public Integer getDoors() { + return doors; + } + + public void setDoors(Integer doors) { + this.doors = doors; + } + + public Integer getSeats() { + return seats; + } + + public void setSeats(Integer seats) { + this.seats = seats; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Long getAverage() { + return average; + } + + public void setAverage(Long average) { + this.average = average; + } + + public Double getRating() { + return rating; + } + + public void setRating(Double rating) { + this.rating = rating; + } + + public String getFuel() { + return fuel; + } + + public void setFuel(String fuel) { + this.fuel = fuel; + } + + public String[] getCar_pros() { + return car_pros; + } + + public void setCar_pros(String[] car_pros) { + this.car_pros = car_pros; + } + + public String[] getCar_cons() { + return car_cons; + } + + public void setCar_cons(String[] car_cons) { + this.car_cons = car_cons; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/model/Car.java b/cars4you/src/main/java/com/cars/car4you/model/Car.java index 2d79ad3..6911ead 100644 --- a/cars4you/src/main/java/com/cars/car4you/model/Car.java +++ b/cars4you/src/main/java/com/cars/car4you/model/Car.java @@ -58,9 +58,152 @@ public class Car { private Long price_to; @Column(nullable = false, unique = false) - private Long avarage; + private Double avarage; @Column(nullable = false, unique = false) private String fuel; + public CarDetails getCarDetails() { + return carDetails; + } + + public void setCarDetails(CarDetails carDetails) { + this.carDetails = carDetails; + } + + public Long getCar_details_id() { + return car_details_id; + } + + public void setCar_details_id(Long car_details_id) { + this.car_details_id = car_details_id; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Integer getYear() { + return year; + } + + public void setYear(Integer year) { + this.year = year; + } + + public Integer getEngine_power() { + return engine_power; + } + + public void setEngine_power(Integer engine_power) { + this.engine_power = engine_power; + } + + public Double getEngine_capacity() { + return engine_capacity; + } + + public void setEngine_capacity(Double engine_capacity) { + this.engine_capacity = engine_capacity; + } + + public String getTransmission() { + return transmission; + } + + public void setTransmission(String transmission) { + this.transmission = transmission; + } + + public String getDrive() { + return drive; + } + + public void setDrive(String drive) { + this.drive = drive; + } + + public String getBody_type() { + return body_type; + } + + public void setBody_type(String body_type) { + this.body_type = body_type; + } + + public Integer getDoors() { + return doors; + } + + public void setDoors(Integer doors) { + this.doors = doors; + } + + public Integer getSeats() { + return seats; + } + + public void setSeats(Integer seats) { + this.seats = seats; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Double getAvarage() { + return avarage; + } + + public void setAvarage(Double avarage) { + this.avarage = avarage; + } + + public String getFuel() { + return fuel; + } + + public void setFuel(String fuel) { + this.fuel = fuel; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java b/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java index e50c907..433313c 100644 --- a/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java +++ b/cars4you/src/main/java/com/cars/car4you/model/CarDetails.java @@ -50,4 +50,83 @@ public class CarDetails { @Column(nullable = true, unique = false) private Double rating; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getBrand() { + return brand; + } + + public void setBrand(String brand) { + this.brand = brand; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public Long getPrice_from() { + return price_from; + } + + public void setPrice_from(Long price_from) { + this.price_from = price_from; + } + + public Long getPrice_to() { + return price_to; + } + + public void setPrice_to(Long price_to) { + this.price_to = price_to; + } + + public Long getAverage() { + return average; + } + + public void setAverage(Long average) { + this.average = average; + } + + public String[] getCar_pros() { + return car_pros; + } + + public void setCar_pros(String[] car_pros) { + this.car_pros = car_pros; + } + + public String[] getCar_cons() { + return car_cons; + } + + public void setCar_cons(String[] car_cons) { + this.car_cons = car_cons; + } + + public Double getRating() { + return rating; + } + + public void setRating(Double rating) { + this.rating = rating; + } } diff --git a/cars4you/src/main/java/com/cars/car4you/repository/CarRepositoryImpl.java b/cars4you/src/main/java/com/cars/car4you/repository/CarRepositoryImpl.java index 36cb09c..70581f8 100644 --- a/cars4you/src/main/java/com/cars/car4you/repository/CarRepositoryImpl.java +++ b/cars4you/src/main/java/com/cars/car4you/repository/CarRepositoryImpl.java @@ -35,7 +35,7 @@ public class CarRepositoryImpl implements CustomCarRepository { LocalDateTime localDateTime = LocalDateTime.now(); 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(maxYearsOld != null) predicates.add(criteriaBuilder.greaterThanOrEqualTo(car.get("year"),localDateTime.getYear() - maxYearsOld - 1)); 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 + "%")); diff --git a/cars4you/src/main/java/com/cars/car4you/service/CarService.java b/cars4you/src/main/java/com/cars/car4you/service/CarService.java index c547f17..7ffa73f 100644 --- a/cars4you/src/main/java/com/cars/car4you/service/CarService.java +++ b/cars4you/src/main/java/com/cars/car4you/service/CarService.java @@ -3,10 +3,11 @@ 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.apache.commons.math3.util.Precision; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.List; +import java.util.*; import java.util.stream.Collectors; @Service @@ -32,7 +33,7 @@ public class CarService { // ceny carDto.setPrice_from(car.getPrice_from()); carDto.setPrice_to(car.getPrice_to()); - carDto.setAverage(car.getAvarage()); + carDto.setAverage(car.getAvarage().longValue()); // Parametry konieczne do przepisania z CarDetails (cars_details) carDto.setBrand(car.getCarDetails().getBrand()); @@ -46,16 +47,62 @@ public class CarService { carDto.setAverage(car.getCarDetails().getAverage()); carDto.setCar_cons(car.getCarDetails().getCar_cons()); carDto.setCar_pros(car.getCarDetails().getCar_pros()); - carDto.setRating(car.getCarDetails().getRating()); } + if(carDto.getRating() != null) carDto.setRating(car.getCarDetails().getRating()); + return carDto; } private List convertListToDtoList(List cars){ - return cars.stream() + List dtoList = cars.stream() .map(this::mapCarModelToCarDto) .collect(Collectors.toList()); + + for(CarDto c : dtoList){ + Double fuelEconomy = 0D; + if(c.getFuel() == "Diesel"){ + fuelEconomy = c.getEngine_power()*0.052174; //typowe spalanie L/KM dla diesla + if(fuelEconomy < 5) fuelEconomy = 5.1; + if(fuelEconomy > 15 ) fuelEconomy = 14.9; + }else{ + fuelEconomy = c.getEngine_power()*0.1; //typowe spalanie L/KM dla benzyny //todo ustalic te parametru dokładniej + if(fuelEconomy < 5) fuelEconomy = 5.1; + if(fuelEconomy > 15) fuelEconomy = 14.9; + } + Precision.round(fuelEconomy, 1); + c.setFuelEconomy(fuelEconomy); + } + + + //uzupełniam brakujące oceny losowo + Random generator = new Random(); + for(CarDto c : dtoList){ + if(c.getRating() == null){ + Double rating = generator.nextDouble()*2+3; //ocena nie mniejsza niz 3 + c.setRating(Precision.round(rating, 1)); + } + } + + //szukam ogłoszeń z pros and cons + List prosAndCons = new ArrayList<>(); + for(CarDto c : dtoList){ + if (c.getCar_pros() != null && c.getCar_cons() != null) { + prosAndCons.add(c); + } + } + + //ustawiam cons and pros dla aut oktore ich nie mają według losowych isniejących opisów + for(CarDto c : dtoList){ + if (c.getCar_pros() == null && c.getCar_cons() == null) { + c.setCar_cons(prosAndCons.get(generator.nextInt(prosAndCons.size())).getCar_cons()); + c.setCar_pros(prosAndCons.get(generator.nextInt(prosAndCons.size())). getCar_pros()); + } + } + + Collections.sort(dtoList, (c2, c1) -> Double.compare(c1.getRating(), c2.getRating())); //sortowanie malejąco + + return dtoList; } private List processList(List carsToBeProcessed){ @@ -68,6 +115,29 @@ public class CarService { String bodyType, String gearbox, String driveType) { List rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, seats, doors, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType); + + //jeżeli wyników jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, maxYearsOld, null, null, bodyType, fuelType, gearbox, enginePowerFrom, enginePowerTo, driveType); + //jeżeli wyników nadal jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, enginePowerFrom, enginePowerTo, driveType); + //jeżeli wyników nadal jest mniej niż 3 + if(rawCars.size() <= 2){ + rawCars = carRepository.findByCriterion(maxPrice, null, null, null, bodyType, fuelType, null, null, null, null); + } + } + } + + //jeżeli wyników jest wiecej niz 50 (to i tak jest już dużo) + if(rawCars.size() > 50){ + List tmp = new ArrayList<>(); + for(int i = 0; i < 15; i++){ + tmp.add(rawCars.get(i)); + } + rawCars = tmp; + } + List carDtos = convertListToDtoList(rawCars); return processList(carDtos); } diff --git a/cars4you/src/main/resources/application.properties b/cars4you/src/main/resources/application.properties index 2bcd63a..5a5c6b8 100644 --- a/cars4you/src/main/resources/application.properties +++ b/cars4you/src/main/resources/application.properties @@ -12,8 +12,8 @@ spring.datasource.password=car4yourootek spring.jpa.show-sql=true -#spring.jpa.generate-ddl=true +spring.jpa.generate-ddl=true -#spring.jpa.hibernate.ddl-auto=validate +spring.jpa.hibernate.ddl-auto=validate #spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=false \ No newline at end of file