3 Лабораторная с доп заданием 2
This commit is contained in:
parent
c8a4f68073
commit
478aea525a
11
src/main/java/ru/ulstu/is/lab1/models/CarStatistic.java
Normal file
11
src/main/java/ru/ulstu/is/lab1/models/CarStatistic.java
Normal file
@ -0,0 +1,11 @@
|
||||
package ru.ulstu.is.lab1.models;
|
||||
|
||||
public class CarStatistic {
|
||||
private Brand brand;
|
||||
private Long count;
|
||||
|
||||
public CarStatistic(Brand brand, Long count) {
|
||||
this.brand = brand;
|
||||
this.count = count;
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
package ru.ulstu.is.lab1.service;
|
||||
|
||||
import ru.ulstu.is.lab1.models.Brand;
|
||||
import ru.ulstu.is.lab1.models.Car;
|
||||
import ru.ulstu.is.lab1.models.Buyer;
|
||||
import ru.ulstu.is.lab1.models.Store;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.ulstu.is.lab1.models.*;
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityNotFoundException;
|
||||
import javax.persistence.PersistenceContext;
|
||||
@ -15,6 +14,11 @@ import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CarService {
|
||||
|
||||
private final CarRepository repository;
|
||||
public CarService(CarRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
@PersistenceContext
|
||||
private EntityManager em;
|
||||
|
||||
@ -98,27 +102,17 @@ public class CarService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Car> getCarsByStore(Store p) {
|
||||
List<Car> cars = em.createQuery("select e from Car e INNER JOIN e.stores p WHERE p.Id=:store",
|
||||
Car.class)
|
||||
.setParameter("store", p.getId())
|
||||
.getResultList();
|
||||
|
||||
for (Car e : cars) {
|
||||
System.out.println(e);
|
||||
}
|
||||
return cars;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Car> getCarsByAllStore(Store p) {
|
||||
List<Car> cars = em.createQuery("select e from Car e INNER JOIN e.stores",
|
||||
Car.class)
|
||||
.getResultList();
|
||||
|
||||
for (Car e : cars) {
|
||||
System.out.println(e);
|
||||
}
|
||||
return cars;
|
||||
public int getBrandByStore(Store p) {
|
||||
return repository.findCarCount().size();
|
||||
}
|
||||
}
|
||||
|
||||
interface CarRepository extends CrudRepository<Car, Long> {
|
||||
@Query("SELECT " +
|
||||
" new ru.ulstu.is.lab1.models.CarStatistic(v.brand, COUNT(v)) " +
|
||||
"FROM " +
|
||||
" Car v " +
|
||||
"GROUP BY " +
|
||||
" v.brand")
|
||||
List<CarStatistic> findCarCount();
|
||||
}
|
@ -115,33 +115,8 @@ public class CarServiceTests {
|
||||
}
|
||||
}
|
||||
|
||||
Assertions.assertEquals(n / 2, carService.getCarsByStore(p).size());
|
||||
Assertions.assertEquals(n / 2, carService.getCarsByStore(p2).size());
|
||||
|
||||
carService.deleteAllCars();
|
||||
storeService.deleteAllStores();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetByAllStore() {
|
||||
carService.deleteAllCars();
|
||||
storeService.deleteAllStores();
|
||||
|
||||
Store p = storeService.addStore("Store 1");
|
||||
Store p2 = storeService.addStore("Store 2");
|
||||
|
||||
final int n = 10;
|
||||
for (int i = 0; i < n; i++) {
|
||||
Car e = carService.addCar("Priora" + i, Brand.valueOf("Lada"));
|
||||
if (i % 2 == 0) {
|
||||
carService.addStore(e.getId(), p);
|
||||
}
|
||||
else {
|
||||
carService.addStore(e.getId(), p2);
|
||||
}
|
||||
}
|
||||
|
||||
Assertions.assertEquals(n , carService.getCarsByAllStore(p).size());
|
||||
Assertions.assertEquals(1, carService.getBrandByStore(p));
|
||||
Assertions.assertEquals(1, carService.getBrandByStore(p2));
|
||||
|
||||
carService.deleteAllCars();
|
||||
storeService.deleteAllStores();
|
||||
|
Loading…
x
Reference in New Issue
Block a user