Obsługa strony w SimpleController. Aktualizacja kodu JavaScript

This commit is contained in:
Damian Michalski 2020-01-19 17:36:44 +01:00
parent 33f95c77bb
commit 894693907f
9 changed files with 138 additions and 38 deletions

View File

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

View File

@ -1,4 +1,4 @@
function sendJSON() { function sendGetRequest() {
let result = document.querySelector('.result'); let result = document.querySelector('.result');
let fuel_type = document.getElementById('fuel_type'); let fuel_type = document.getElementById('fuel_type');
let seats = document.getElementsByClassName('seats')[0]; let seats = document.getElementsByClassName('seats')[0];
@ -10,14 +10,12 @@
let body_type = document.getElementById('body_type'); let body_type = document.getElementById('body_type');
let gearbox = document.getElementById('gearbox'); let gearbox = document.getElementById('gearbox');
let drive_type = document.getElementById('drive_type'); let drive_type = document.getElementById('drive_type');
let request = new XMLHttpRequest();
let req = new XMLHttpRequest();
// let url = "https://jsonplaceholder.typicode.com/photos"; // let url = "https://jsonplaceholder.typicode.com/photos";
// let url = "https://webhook.site/b1792c3d-cd5a-4c29-8e9c-1ff9f84653eb"; // 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 = []; var params = [];
if(fuel_type.value != -1){ if(fuel_type.value != -1){
params.push("fuel_type="+fuel_type.options[fuel_type.value].text); params.push("fuel_type="+fuel_type.options[fuel_type.value].text);
} }
@ -48,7 +46,6 @@
if(drive_type.value != -1){ if(drive_type.value != -1){
params.push("drive_type="+drive_type.value); params.push("drive_type="+drive_type.value);
} }
var buff = ""; var buff = "";
if(params.length>0){ if(params.length>0){
buff += "?"; buff += "?";
@ -60,19 +57,14 @@
} }
console.log("Debug: " + buff); console.log("Debug: " + buff);
request.open("GET", url+buff, true);
// Create a state change callback request.setRequestHeader("Content-Type", "text/html");
req.open("GET", url+buff, true); request.send();
req.setRequestHeader("Content-Type", "text/html"); request.onreadystatechange = function () {
req.send(); if (request.readyState === 4 && request.status === 200) {
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
// Print received data from server // Print received data from server
result.innerHTML = this.responseText; result.innerHTML = this.responseText;
} }
}; };
} }

View File

@ -151,7 +151,7 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<input value="Szukaj" class="primary-btn" onclick="sendJSON()" /> <input value="Szukaj" class="primary-btn" onclick="sendGetRequest()" />
<div class="result"> <div class="result">
<!-- result list--> <!-- result list-->
</div> </div>

View File

@ -8,6 +8,7 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@ -28,6 +29,16 @@ public class CarsController {
return carRepository.findByModel(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;
}
@GetMapping("/carmodel/{maxPrice}/{maxYearsOld}") @GetMapping("/carmodel/{maxPrice}/{maxYearsOld}")
public List<Car> findByModel(@PathVariable int maxPrice, @PathVariable int maxYearsOld) { public List<Car> findByModel(@PathVariable int maxPrice, @PathVariable int maxYearsOld) {
return carRepository.findByCriterion(maxPrice, maxYearsOld, 0,null,null,null,0,null); return carRepository.findByCriterion(maxPrice, maxYearsOld, 0,null,null,null,0,null);

View File

@ -1,13 +1,55 @@
package com.cars.car4you.api; 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.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 { public class SimpleController {
@GetMapping("/error")
public String errorPage() {
return "error";
}
@GetMapping("/") @GetMapping("/")
public String homePage() { public byte[] homePage() throws IOException {
return "home"; 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);
} }
} }

View File

@ -2,14 +2,19 @@ package com.cars.car4you.model;
import lombok.Data; import lombok.Data;
import javax.persistence.Column; import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity @Entity
@Data @Data
public class Car { 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 @Id
private long id; private long id;

View File

@ -1,10 +1,53 @@
package com.cars.car4you.model; package com.cars.car4you.model;
import com.vladmihalcea.hibernate.type.array.StringArrayType;
import lombok.Data; 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 @Entity
@Data @Data
@Table(name = "car_details")
@TypeDefs(@TypeDef(name = "string-array", typeClass = StringArrayType.class))
public class CarDetails { 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

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