Obsługa strony w SimpleController. Aktualizacja kodu JavaScript
This commit is contained in:
parent
33f95c77bb
commit
894693907f
@ -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>
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
function sendJSON() {
|
||||
function sendGetRequest() {
|
||||
let result = document.querySelector('.result');
|
||||
let fuel_type = document.getElementById('fuel_type');
|
||||
let seats = document.getElementsByClassName('seats')[0];
|
||||
@ -10,14 +10,12 @@
|
||||
let body_type = document.getElementById('body_type');
|
||||
let gearbox = document.getElementById('gearbox');
|
||||
let drive_type = document.getElementById('drive_type');
|
||||
|
||||
|
||||
let req = new XMLHttpRequest();
|
||||
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://34.65.132.148:8080";
|
||||
let url = "http://localhost:8080/api/cars/search";
|
||||
var params = [];
|
||||
|
||||
if(fuel_type.value != -1){
|
||||
params.push("fuel_type="+fuel_type.options[fuel_type.value].text);
|
||||
}
|
||||
@ -48,7 +46,6 @@
|
||||
if(drive_type.value != -1){
|
||||
params.push("drive_type="+drive_type.value);
|
||||
}
|
||||
|
||||
var buff = "";
|
||||
if(params.length>0){
|
||||
buff += "?";
|
||||
@ -60,19 +57,14 @@
|
||||
}
|
||||
|
||||
console.log("Debug: " + buff);
|
||||
|
||||
// Create a state change callback
|
||||
req.open("GET", url+buff, true);
|
||||
req.setRequestHeader("Content-Type", "text/html");
|
||||
req.send();
|
||||
|
||||
req.onreadystatechange = function () {
|
||||
if (req.readyState === 4 && req.status === 200) {
|
||||
request.open("GET", url+buff, true);
|
||||
request.setRequestHeader("Content-Type", "text/html");
|
||||
request.send();
|
||||
request.onreadystatechange = function () {
|
||||
if (request.readyState === 4 && request.status === 200) {
|
||||
|
||||
// Print received data from server
|
||||
result.innerHTML = this.responseText;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
|
@ -151,7 +151,7 @@
|
||||
</div>
|
||||
|
||||
<div class="form-group">
|
||||
<input value="Szukaj" class="primary-btn" onclick="sendJSON()" />
|
||||
<input value="Szukaj" class="primary-btn" onclick="sendGetRequest()" />
|
||||
<div class="result">
|
||||
<!-- result list-->
|
||||
</div>
|
||||
|
@ -8,6 +8,7 @@ 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;
|
||||
|
||||
@RestController
|
||||
@ -28,6 +29,16 @@ public class CarsController {
|
||||
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;
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@ -1,13 +1,55 @@
|
||||
package com.cars.car4you.api;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@RestController
|
||||
public class SimpleController {
|
||||
|
||||
@GetMapping("/error")
|
||||
public String errorPage() {
|
||||
return "error";
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String homePage() {
|
||||
return "home";
|
||||
public byte[] homePage() throws IOException {
|
||||
byte[] array = Files.readAllBytes(Paths.get("src/main/client/html/search.html"));
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@GetMapping("/{dir}/{name}")
|
||||
public ResponseEntity<byte[]> getPageContents(
|
||||
@PathVariable String name,
|
||||
@PathVariable String dir) throws IOException {
|
||||
|
||||
byte[] array = Files.readAllBytes(Paths.get("src/main/client/html/"+dir+"/"+name));
|
||||
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
|
||||
if(name.matches("(.*\\.js)")) {
|
||||
responseHeaders.set("Content-Type", "text/javascript");
|
||||
}else{
|
||||
//dodatkowe content-types
|
||||
}
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(responseHeaders)
|
||||
.body(array);
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user