Recommendation model

This commit is contained in:
s452111 2020-01-19 16:24:34 +01:00
parent 73c980f8e4
commit 2e12e65fc1
8 changed files with 94 additions and 16 deletions

View File

@ -49,6 +49,12 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

View File

@ -1,6 +1,8 @@
package com.cars.car4you.api;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@ -16,6 +18,9 @@ public class CarsController {
@Autowired
private CarRepository carRepository;
@Autowired
private CarDetailRepository carDetailRepository;
@GetMapping("/{id}")
public Car findOne(@PathVariable Long id) {

View File

@ -0,0 +1,4 @@
package com.cars.car4you.dto;
public class CarDto {
}

View File

@ -2,14 +2,19 @@ package com.cars.car4you.model;
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.*;
@Entity
@Data
public class Car {
@ManyToOne
// @JoinTable(name = "car_details")
private CarDetails carDetails;
@Column(nullable = false, unique = false, insertable = false, updatable = false)
private long car_details_id;
@Id
private long id;

View File

@ -1,10 +1,53 @@
package com.cars.car4you.model;
import com.vladmihalcea.hibernate.type.array.StringArrayType;
import lombok.Data;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.annotations.TypeDefs;
import javax.persistence.Entity;
import javax.persistence.*;
import java.util.List;
@Entity
@Data
@Table(name = "car_details")
@TypeDefs(@TypeDef(name = "string-array", typeClass = StringArrayType.class))
public class CarDetails {
// @OneToMany(mappedBy = "car_details_id")
// private List<Car> cars;
@Id
private Long id;
@Column(nullable = false, unique = false)
private String brand;
@Column(nullable = false, unique = false)
private String model;
@Column(nullable = false, unique = false)
private String version;
@Column(nullable = true, unique = false)
private Long price_from;
@Column(nullable = true, unique = false)
private Long price_to;
@Column(nullable = true, unique = false)
private Double average;
@Type(type = "string-array" )
@Column(name = "car_pros")
private String[] car_pros;
@Type(type = "string-array" )
@Column(name = "car_cons")
private String[] car_cons;
@Column(nullable = true, unique = false)
private Double rating;
}

View File

@ -0,0 +1,14 @@
package com.cars.car4you.repository;
import com.cars.car4you.model.Car;
import com.cars.car4you.model.CarDetails;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CarDetailRepository extends CrudRepository<CarDetails, Long> {
List<CarDetails> findByModel(String model);
}

View File

@ -1,9 +1,6 @@
package com.cars.car4you.repository;
import com.cars.car4you.model.Car;
import com.cars.car4you.model.CarType;
import com.cars.car4you.model.DriveType;
import com.cars.car4you.model.FuelType;
import com.cars.car4you.model.*;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
@ -12,7 +9,6 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.script.ScriptEngine;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@ -26,11 +22,12 @@ public class CarRepositoryImpl implements CustomCarRepository {
@Override
public List<Car> findByCriterion(int maxPrice, int maxYearsOld,
int seats, CarType carType,
ArrayList<FuelType> fuelTypes,
String transmission, int enginePower, DriveType drivetype) {
String fuelType,
String transmission, int enginePower, String driveType) {
CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
CriteriaQuery<Car> cq = criteriaBuilder.createQuery(Car.class);
Root<Car> car = cq.from(Car.class);
List<Predicate> predicates = new ArrayList<>();
@ -42,6 +39,11 @@ public class CarRepositoryImpl implements CustomCarRepository {
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 + "%"));
cq.where(predicates.toArray(new Predicate[0]));
return em.createQuery(cq).getResultList();

View File

@ -2,16 +2,15 @@ package com.cars.car4you.repository;
import com.cars.car4you.model.Car;
import com.cars.car4you.model.CarType;
import com.cars.car4you.model.DriveType;
import com.cars.car4you.model.FuelType;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public interface CustomCarRepository {
List<Car> findByCriterion(int maxPrice, int maxYearsOld,
int seats, CarType carType,
ArrayList<FuelType> fuelTypes,
String transmission, int enginePower, DriveType drivetype);
String fuelTypes,
String transmission, int enginePower, String drivetype);
}