lab7 main

This commit is contained in:
parap 2023-05-22 06:30:06 +04:00
parent 81b67b463d
commit 49303e1efb
21 changed files with 246 additions and 91 deletions

View File

@ -45,6 +45,10 @@
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.hibernate.validator</groupId> <groupId>org.hibernate.validator</groupId>

View File

@ -4,6 +4,8 @@ import org.dbms.dto.ClientLoginDto;
import org.dbms.dto.ClientSignupDto; import org.dbms.dto.ClientSignupDto;
import org.dbms.dto.DriverDTO; import org.dbms.dto.DriverDTO;
import org.dbms.exceptions.UserExistsException; import org.dbms.exceptions.UserExistsException;
import org.dbms.models.Client;
import org.dbms.repos.ClientRepo;
import org.dbms.searchModels.DriverSearch; import org.dbms.searchModels.DriverSearch;
import org.dbms.service.ClientService; import org.dbms.service.ClientService;
import org.dbms.storageImpl.ClientStorage; import org.dbms.storageImpl.ClientStorage;
@ -19,9 +21,11 @@ import java.util.stream.Collectors;
@RestController @RestController
public class ClientController { public class ClientController {
private final ClientService clientService; private final ClientService clientService;
private final ClientRepo repo;
public ClientController(ClientService clientService) { public ClientController(ClientService clientService, ClientRepo repo) {
this.clientService = clientService; this.clientService = clientService;
this.repo = repo;
} }
@PostMapping("/client") @PostMapping("/client")
@ -47,5 +51,10 @@ public class ClientController {
return "Неверный логин или пароль"; return "Неверный логин или пароль";
} }
} }
@GetMapping("/all-clients")
public List<Client> getAll() {
return repo.findAll();
}
} }
; ;

View File

@ -1,6 +1,7 @@
package org.dbms.controllers; package org.dbms.controllers;
import org.dbms.dto.DriverDTO; import org.dbms.dto.DriverDTO;
import org.dbms.repos.DriverRepo;
import org.dbms.searchModels.DriverSearch; import org.dbms.searchModels.DriverSearch;
import org.dbms.service.DriverService; import org.dbms.service.DriverService;
import org.dbms.storageImpl.DriverStorage; import org.dbms.storageImpl.DriverStorage;
@ -12,9 +13,11 @@ import java.util.stream.Collectors;
@RestController @RestController
public class DriverController { public class DriverController {
private final DriverService driverService; private final DriverService driverService;
private final DriverRepo repo;
public DriverController(DriverService driverService) { public DriverController(DriverService driverService, DriverRepo repo) {
this.driverService = driverService; this.driverService = driverService;
this.repo = repo;
} }
@GetMapping("/drivers") @GetMapping("/drivers")
@ -33,4 +36,9 @@ public class DriverController {
return driverService.getFilteredCount(expFrom, expTo, size); return driverService.getFilteredCount(expFrom, expTo, size);
} }
@GetMapping("/all-drivers")
public List<DriverDTO> getAll() {
return repo.findAll().stream().map(DriverDTO::new).toList();
}
} }

View File

@ -5,6 +5,8 @@ import org.dbms.dto.ClientSignupDto;
import org.dbms.dto.CreateOrderDTO; import org.dbms.dto.CreateOrderDTO;
import org.dbms.dto.OrderDTO; import org.dbms.dto.OrderDTO;
import org.dbms.exceptions.UserExistsException; import org.dbms.exceptions.UserExistsException;
import org.dbms.models.Order;
import org.dbms.repos.OrderRepo;
import org.dbms.service.ClientService; import org.dbms.service.ClientService;
import org.dbms.service.OrderService; import org.dbms.service.OrderService;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -17,9 +19,11 @@ import java.util.List;
@RestController @RestController
public class OrderController { public class OrderController {
private final OrderService orderService; private final OrderService orderService;
private final OrderRepo repo;
public OrderController(OrderService orderService) { public OrderController(OrderService orderService, OrderRepo repo) {
this.orderService = orderService; this.orderService = orderService;
this.repo = repo;
} }
@GetMapping("/orders") @GetMapping("/orders")
@ -37,5 +41,10 @@ public class OrderController {
public int getPagesCount(@RequestParam String login, @RequestParam int size) { public int getPagesCount(@RequestParam String login, @RequestParam int size) {
return orderService.getPagesCount(login, size); return orderService.getPagesCount(login, size);
} }
@GetMapping("/all-orders")
public List<Order> getAll() {
return repo.findAll();
}
} }
; ;

View File

@ -5,6 +5,10 @@ import org.dbms.models.Car;
import org.dbms.models.Client; import org.dbms.models.Client;
import org.dbms.models.Driver; import org.dbms.models.Driver;
import org.dbms.models.Parking; import org.dbms.models.Parking;
import org.dbms.repos.CarRepo;
import org.dbms.repos.ClientRepo;
import org.dbms.repos.DriverRepo;
import org.dbms.repos.ParkingRepo;
import org.dbms.service.OrderService; import org.dbms.service.OrderService;
import org.dbms.storageContracts.ICarStorage; import org.dbms.storageContracts.ICarStorage;
import org.dbms.storageContracts.IClientStorage; import org.dbms.storageContracts.IClientStorage;
@ -16,11 +20,11 @@ import java.util.*;
@Service @Service
public class DataGenerator { public class DataGenerator {
private ICarStorage carStorage; private CarRepo carRepo;
private IParkingStorage parkingStorage; private ParkingRepo parkingRepo;
private IClientStorage clientStorage; private ClientRepo clientRepo;
private OrderService orderService; private OrderService orderService;
private IDriverStorage driverStorage; private DriverRepo driverRepo;
private static String surnames[] = {"Иванов", "Петров", "Сидоров", "Александров", "Панов", "Ежов", "Зотов", "Котов", "зощенков"}; private static String surnames[] = {"Иванов", "Петров", "Сидоров", "Александров", "Панов", "Ежов", "Зотов", "Котов", "зощенков"};
@ -29,13 +33,13 @@ public class DataGenerator {
private static String cities[] = {"Ульяновск", "Самара", "Москва", "Уфа", "Казань", "Нижний новгород", "Санкт петербург"}; private static String cities[] = {"Ульяновск", "Самара", "Москва", "Уфа", "Казань", "Нижний новгород", "Санкт петербург"};
private static String streets[] = {"пр. Нариманова", "ул. Ленина", "ул. Карла Маркса", "ул. Радищева", "ул. Гагарина"}; private static String streets[] = {"пр. Нариманова", "ул. Ленина", "ул. Карла Маркса", "ул. Радищева", "ул. Гагарина"};
public DataGenerator(ICarStorage carStorage, IParkingStorage parkingStorage, public DataGenerator(CarRepo carRepo, ParkingRepo parkingRepo,
IClientStorage clientStorage, OrderService orderService, IDriverStorage driverStorage) { ClientRepo clientRepo, OrderService orderService, DriverRepo driverRepo) {
this.carStorage = carStorage; this.carRepo = carRepo;
this.parkingStorage = parkingStorage; this.parkingRepo = parkingRepo;
this.clientStorage = clientStorage; this.clientRepo = clientRepo;
this.orderService = orderService; this.orderService = orderService;
this.driverStorage = driverStorage; this.driverRepo = driverRepo;
} }
private String generateStr() { private String generateStr() {
@ -76,7 +80,7 @@ public class DataGenerator {
} }
private void generateCars() { private void generateCars() {
List<Parking> parkings = parkingStorage.readAll(); List<Parking> parkings = parkingRepo.findAll();
Random rnd = new Random(); Random rnd = new Random();
String models[] = {"Газель", "Камаз", "Volvo", "Трактор"}; String models[] = {"Газель", "Камаз", "Volvo", "Трактор"};
@ -85,7 +89,7 @@ public class DataGenerator {
double cost = rnd.nextDouble(400000) + 50000; double cost = rnd.nextDouble(400000) + 50000;
Car car = new Car(models[modelIndex], generateDate(), cost, parkings.get(rnd.nextInt(parkings.size()))); Car car = new Car(models[modelIndex], generateDate(), cost, parkings.get(rnd.nextInt(parkings.size())));
carStorage.insert(car); carRepo.insert(car);
} }
} }
@ -107,7 +111,7 @@ public class DataGenerator {
} }
Client client = new Client(name, phone, login, pass); Client client = new Client(name, phone, login, pass);
clientStorage.insert(client); clientRepo.insert(client);
} }
} }
@ -117,12 +121,12 @@ public class DataGenerator {
for(int i = 0; i < 600; ++i) { for(int i = 0; i < 600; ++i) {
String address = generateAddress(); String address = generateAddress();
Parking parking = new Parking(address, rnd.nextInt(20) + 10); Parking parking = new Parking(address, rnd.nextInt(20) + 10);
parkingStorage.insert(parking); parkingRepo.insert(parking);
} }
} }
private void generateDrivers() { private void generateDrivers() {
List<Car> cars = carStorage.readAll(); List<Car> cars = carRepo.findAll();
Random rnd = new Random(); Random rnd = new Random();
for(int i = 0; i < 1000; ++i) { for(int i = 0; i < 1000; ++i) {
@ -130,13 +134,13 @@ public class DataGenerator {
cars.remove(car); cars.remove(car);
Driver driver = new Driver(generateName(), generateDate(), car); Driver driver = new Driver(generateName(), generateDate(), car);
driverStorage.insert(driver); driverRepo.insert(driver);
} }
} }
private void generateOrders() { private void generateOrders() {
List<Client> clients = clientStorage.readAll(); List<Client> clients = clientRepo.findAll();
List<Driver> drivers = driverStorage.readAll(); List<Driver> drivers = driverRepo.findAll();
Random rnd = new Random(); Random rnd = new Random();
for(int i = 0; i < 2000; ++i) { for(int i = 0; i < 2000; ++i) {
@ -145,7 +149,7 @@ public class DataGenerator {
int deliveryWeight = rnd.nextInt(30) + 5; int deliveryWeight = rnd.nextInt(30) + 5;
String clientLogin = clients.get(rnd.nextInt(clients.size())).getLogin(); String clientLogin = clients.get(rnd.nextInt(clients.size())).getLogin();
Long driverId = drivers.get(rnd.nextInt(drivers.size())).getId(); String driverId = drivers.get(rnd.nextInt(drivers.size())).getMongoId();
CreateOrderDTO dto = new CreateOrderDTO(from, to, deliveryWeight, driverId, clientLogin, rnd.nextBoolean()); CreateOrderDTO dto = new CreateOrderDTO(from, to, deliveryWeight, driverId, clientLogin, rnd.nextBoolean());
orderService.createOrder(dto); orderService.createOrder(dto);

View File

@ -5,7 +5,7 @@ import org.dbms.models.Car;
import java.util.Date; import java.util.Date;
public class CarDTO { public class CarDTO {
private Long id; private String id;
private String model; private String model;
private Date year; private Date year;
private double cost; private double cost;
@ -13,13 +13,17 @@ public class CarDTO {
public CarDTO(Car car) { public CarDTO(Car car) {
if(car == null) return; if(car == null) return;
this.id = car.getId(); this.id = car.getMongoId();
this.model = car.getModel(); this.model = car.getModel();
this.year = car.getYear(); this.year = car.getYear();
this.cost = car.getCost(); this.cost = car.getCost();
} }
public Long getId() { public Long getId() {
return 1l;
}
public String getMongoId() {
return id; return id;
} }
@ -36,7 +40,6 @@ public class CarDTO {
} }
public void setId(Long id) { public void setId(Long id) {
this.id = id;
} }
public void setCost(double cost) { public void setCost(double cost) {

View File

@ -8,12 +8,12 @@ public class CreateOrderDTO {
private String from; private String from;
private String to; private String to;
private int deliveryWeight; private int deliveryWeight;
private Long driverId; private String driverId;
private String login; private String login;
private boolean isFragile; private boolean isFragile;
public CreateOrderDTO() {} public CreateOrderDTO() {}
public CreateOrderDTO(String from, String to, int deliveryWeight, Long driverId, String login, boolean isFragile) { public CreateOrderDTO(String from, String to, int deliveryWeight, String driverId, String login, boolean isFragile) {
this.deliveryWeight = deliveryWeight; this.deliveryWeight = deliveryWeight;
this.driverId = driverId; this.driverId = driverId;
this.from = from; this.from = from;
@ -34,7 +34,7 @@ public class CreateOrderDTO {
public String getLogin() { public String getLogin() {
return login; return login;
} }
public Long getDriverId() { public String getDriverId() {
return driverId; return driverId;
} }
public boolean isFragile() { public boolean isFragile() {

View File

@ -6,14 +6,14 @@ import org.dbms.models.Driver;
import java.util.Date; import java.util.Date;
public class DriverDTO { public class DriverDTO {
Long id; String id;
String name; String name;
Date licenseYear; Date licenseYear;
CarDTO car; CarDTO car;
public DriverDTO(Driver driver) { public DriverDTO(Driver driver) {
if(driver == null) return; if(driver == null) return;
this.id = driver.getId(); this.id = driver.getMongoId();
this.name = driver.getName(); this.name = driver.getName();
this.licenseYear = driver.getLicenseYear(); this.licenseYear = driver.getLicenseYear();
this.car = new CarDTO(driver.getCar()); this.car = new CarDTO(driver.getCar());
@ -28,11 +28,16 @@ public class DriverDTO {
} }
public Long getId() { public Long getId() {
return id; return 1l;
}
public String getMongoId() {return id;}
public void setMongoId(String id) {
this.id = id;
} }
public void setId(Long id) { public void setId(Long id) {
this.id = id;
} }
public void setLicenseYear(Date licenseYear) { public void setLicenseYear(Date licenseYear) {

View File

@ -1,17 +1,20 @@
package org.dbms.models; package org.dbms.models;
import org.springframework.data.annotation.Id;
import java.util.Date; import java.util.Date;
import java.util.Objects; import java.util.Objects;
public class Car { public class Car {
private Long id; @Id
private String id;
private String model; private String model;
private Date year; private Date year;
private double cost; private double cost;
private Parking parking; private Parking parking;
public Car() {} public Car() {}
public Car(Long id, String model, Date year, double cost, Parking parking) { public Car(String id, String model, Date year, double cost, Parking parking) {
this.id = id; this.id = id;
this.model = model; this.model = model;
this.year = year; this.year = year;
@ -19,6 +22,13 @@ public class Car {
this.parking = parking; this.parking = parking;
} }
public Car(Long id, String model, Date year, double cost, Parking parking) {
this.model = model;
this.year = year;
this.cost = cost;
this.parking = parking;
}
public Car(String model, Date year, double cost, Parking parking) { public Car(String model, Date year, double cost, Parking parking) {
this.id = id; this.id = id;
this.model = model; this.model = model;
@ -28,8 +38,9 @@ public class Car {
} }
public Long getId() { public Long getId() {
return id; return 1l;
} }
public String getMongoId() {return id; }
public Date getYear() { public Date getYear() {
return year; return year;

View File

@ -1,14 +1,17 @@
package org.dbms.models; package org.dbms.models;
import org.springframework.data.annotation.Id;
public class Client { public class Client {
private Long id; @Id
private String id;
private String name; private String name;
private String phone; private String phone;
private String login; private String login;
private String password; private String password;
public Client() {} public Client() {}
public Client(Long id, String name, String phone, String login, String password) { public Client(String id, String name, String phone, String login, String password) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.phone = phone; this.phone = phone;
@ -16,6 +19,13 @@ public class Client {
this.password = password; this.password = password;
} }
public Client(Long id, String name, String phone, String login, String password) {
this.name = name;
this.phone = phone;
this.login = login;
this.password = password;
}
public Client(String name, String phone, String login, String password) { public Client(String name, String phone, String login, String password) {
this.name = name; this.name = name;
this.phone = phone; this.phone = phone;
@ -24,9 +34,11 @@ public class Client {
} }
public Long getId() { public Long getId() {
return id; return 1l;
} }
public String getMongoId() {return id;}
public String getName() { public String getName() {
return name; return name;
} }

View File

@ -1,20 +1,28 @@
package org.dbms.models; package org.dbms.models;
import org.springframework.data.annotation.Id;
import java.util.Date; import java.util.Date;
public class Driver { public class Driver {
Long id; @Id
String id;
String name; String name;
Date licenseYear; Date licenseYear;
Car car; Car car;
public Driver() {} public Driver() {}
public Driver(Long id, String name, Date licenseYear, Car car) { public Driver(String id, String name, Date licenseYear, Car car) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.licenseYear = licenseYear; this.licenseYear = licenseYear;
this.car = car; this.car = car;
} }
public Driver(Long id, String name, Date licenseYear, Car car) {
this.name = name;
this.licenseYear = licenseYear;
this.car = car;
}
public Driver(String name, Date licenseYear, Car car) { public Driver(String name, Date licenseYear, Car car) {
this.name = name; this.name = name;
@ -23,6 +31,9 @@ public class Driver {
} }
public Long getId() { public Long getId() {
return 1l;
}
public String getMongoId() {
return id; return id;
} }

View File

@ -1,7 +1,10 @@
package org.dbms.models; package org.dbms.models;
import org.springframework.data.annotation.Id;
public class Order { public class Order {
private Long id; @Id
private String id;
private String from; private String from;
private String to; private String to;
private double price; private double price;
@ -9,7 +12,9 @@ public class Order {
private Driver driver; private Driver driver;
private Client client; private Client client;
public Order(Long id, String from, String to, double price, int deliveryWeight, Driver driver, Client client) { public Order() {}
public Order(String id, String from, String to, double price, int deliveryWeight, Driver driver, Client client) {
this.id = id; this.id = id;
this.from = from; this.from = from;
this.price = price; this.price = price;
@ -19,6 +24,15 @@ public class Order {
this.client = client; this.client = client;
} }
public Order(Long id, String from, String to, double price, int deliveryWeight, Driver driver, Client client) {
this.from = from;
this.price = price;
this.to = to;
this.deliveryWeight = deliveryWeight;
this.driver = driver;
this.client = client;
}
public Order(String from, String to, double price, int deliveryWeight, Driver driver, Client client) { public Order(String from, String to, double price, int deliveryWeight, Driver driver, Client client) {
this.from = from; this.from = from;
this.to = to; this.to = to;
@ -29,6 +43,9 @@ public class Order {
} }
public Long getId() { public Long getId() {
return 1l;
}
public String getMongoId() {
return id; return id;
} }

View File

@ -1,24 +1,36 @@
package org.dbms.models; package org.dbms.models;
import org.springframework.data.annotation.Id;
public class Parking { public class Parking {
private Long id; @Id
private String id;
private String address; private String address;
private int capacity; private int capacity;
public Parking() {} public Parking() {}
public Parking(Long id, String address, int capacity) { public Parking(String id, String address, int capacity) {
this.id = id; this.id = id;
this.address = address; this.address = address;
this.capacity = capacity; this.capacity = capacity;
} }
public Parking(Long id, String address, int capacity) {
this.address = address;
this.capacity = capacity;
}
public Parking(String address, int capacity) { public Parking(String address, int capacity) {
this.address = address; this.address = address;
this.capacity = capacity; this.capacity = capacity;
} }
public Long getId() { public Long getId() {
return 1l;
}
public String getMongoId() {
return id; return id;
} }

View File

@ -0,0 +1,7 @@
package org.dbms.repos;
import org.dbms.models.Car;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface CarRepo extends MongoRepository<Car, String> {
}

View File

@ -0,0 +1,10 @@
package org.dbms.repos;
import org.dbms.models.Client;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface ClientRepo extends MongoRepository<Client, String> {
List<Client> findByLogin(String login);
}

View File

@ -0,0 +1,14 @@
package org.dbms.repos;
import org.dbms.models.Driver;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.Date;
import java.util.List;
public interface DriverRepo extends MongoRepository<Driver, String> {
Page<Driver> findBylicenseYearBetween(Date from, Date to, Pageable pg);
List<Driver> findBylicenseYearBetween(Date from, Date to);
}

View File

@ -0,0 +1,13 @@
package org.dbms.repos;
import org.dbms.models.Client;
import org.dbms.models.Order;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface OrderRepo extends MongoRepository<Order, String> {
Page<Order> findByClient(Client id, Pageable pg);
}

View File

@ -0,0 +1,7 @@
package org.dbms.repos;
import org.dbms.models.Parking;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface ParkingRepo extends MongoRepository<Parking, String> {
}

View File

@ -4,6 +4,7 @@ import org.dbms.dto.ClientLoginDto;
import org.dbms.dto.ClientSignupDto; import org.dbms.dto.ClientSignupDto;
import org.dbms.exceptions.UserExistsException; import org.dbms.exceptions.UserExistsException;
import org.dbms.models.Client; import org.dbms.models.Client;
import org.dbms.repos.ClientRepo;
import org.dbms.storageContracts.IClientStorage; import org.dbms.storageContracts.IClientStorage;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -13,9 +14,10 @@ import java.util.Objects;
@Service @Service
public class ClientService { public class ClientService {
IClientStorage clientStorage; IClientStorage clientStorage;
ClientRepo clientRepo;
public ClientService(IClientStorage clientStorage) { public ClientService(IClientStorage clientStorage, ClientRepo clientRepo) {
this.clientStorage = clientStorage; this.clientStorage = clientStorage;
this.clientRepo = clientRepo;
} }
@Transactional @Transactional
public String signup(ClientSignupDto client) { public String signup(ClientSignupDto client) {
@ -23,12 +25,12 @@ public class ClientService {
throw new UserExistsException(client.getLogin()); throw new UserExistsException(client.getLogin());
} }
clientStorage.insert(new Client(client.getName(), client.getPhone(), client.getLogin(), client.getPassword())); Client res = clientRepo.insert(new Client(client.getName(), client.getPhone(), client.getLogin(), client.getPassword()));
return clientStorage.getIdByLogin(client.getLogin()); return res.getId() + "";
} }
@Transactional @Transactional
public boolean login(ClientLoginDto client) { public boolean login(ClientLoginDto client) {
Client c = clientStorage.getByLogin(client.getLogin()); Client c = clientRepo.findByLogin(client.getLogin()).get(0);
if(c == null) return false; if(c == null) return false;
if(!Objects.equals(c.getPassword(), client.getPassword())) return false; if(!Objects.equals(c.getPassword(), client.getPassword())) return false;
@ -37,6 +39,6 @@ public class ClientService {
@Transactional @Transactional
public Client getClientByLogin(String login) { public Client getClientByLogin(String login) {
return clientStorage.getByLogin(login); return clientRepo.findByLogin(login).get(0);
} }
} }

View File

@ -2,56 +2,57 @@ package org.dbms.service;
import org.dbms.dto.DriverDTO; import org.dbms.dto.DriverDTO;
import org.dbms.models.Driver; import org.dbms.models.Driver;
import org.dbms.repos.DriverRepo;
import org.dbms.searchModels.DriverSearch; import org.dbms.searchModels.DriverSearch;
import org.dbms.storageContracts.IDriverStorage; import org.dbms.storageContracts.IDriverStorage;
import org.dbms.storageImpl.DriverStorage; import org.dbms.storageImpl.DriverStorage;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List; import java.util.List;
@Service @Service
public class DriverService { public class DriverService {
private IDriverStorage driverStorage; private DriverRepo driverRepo;
public DriverService(IDriverStorage driverStorage) { public DriverService(DriverRepo driverRepo) {
this.driverStorage = driverStorage; this.driverRepo = driverRepo;
}
@Transactional
public List<Driver> getFiltered(int expFrom, int expTo) {
DriverSearch ds = new DriverSearch();
if(expFrom != -1) ds.expirienceFrom = expFrom;
if(expTo != -1) ds.expirienceTo = expTo;
return driverStorage.getFilteredList(ds);
} }
@Transactional @Transactional
public int getFilteredCount(int expFrom, int expTo, int size) { public int getFilteredCount(int expFrom, int expTo, int size) {
DriverSearch ds = new DriverSearch(); if(expFrom == -1) expFrom = 0;
if(expTo == -1) expTo = 100;
if(expFrom != -1) ds.expirienceFrom = expFrom; Date from = new Date();
if(expTo != -1) ds.expirienceTo = expTo; Date to = new Date();
int count = driverStorage.getFilteredCount(ds); from.setYear(from.getYear() - expTo);
int res = count / size; to.setYear(to.getYear() - expFrom);
if(count % size != 0) res++;
return res; Page<Driver> res = driverRepo.findBylicenseYearBetween(from, to, PageRequest.of(1, size));
return res.getTotalPages();
} }
@Transactional @Transactional
public List<Driver> getFilteredPage(int expFrom, int expTo, int size, int page) { public List<Driver> getFilteredPage(int expFrom, int expTo, int size, int page) {
DriverSearch ds = new DriverSearch(); if(expFrom == -1) expFrom = 0;
if(expTo == -1) expTo = 100;
if(expFrom != -1) ds.expirienceFrom = expFrom; Date from = new Date();
if(expTo != -1) ds.expirienceTo = expTo; Date to = new Date();
return driverStorage.getFilteredPage(ds, size, page); from.setYear(from.getYear() - expTo);
to.setYear(to.getYear() - expFrom);
Page<Driver> res = driverRepo.findBylicenseYearBetween(from, to, PageRequest.of(page - 1, size));
return res.getContent();
} }
@Transactional @Transactional
public Driver getDriverById(Long driverId) { public Driver getDriverById(String driverId) {
return driverStorage.getElementById(driverId); return driverRepo.findById(driverId).get();
} }
} }

View File

@ -4,7 +4,9 @@ import org.dbms.dto.CreateOrderDTO;
import org.dbms.models.Client; import org.dbms.models.Client;
import org.dbms.models.Driver; import org.dbms.models.Driver;
import org.dbms.models.Order; import org.dbms.models.Order;
import org.dbms.repos.OrderRepo;
import org.dbms.storageContracts.IOrderStorage; import org.dbms.storageContracts.IOrderStorage;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
@ -15,26 +17,22 @@ import java.util.List;
@Service @Service
public class OrderService { public class OrderService {
private IOrderStorage orderStorage;
private ClientService clientService; private ClientService clientService;
private DriverService driverService; private DriverService driverService;
private OrderRepo orderRepo;
public OrderService(IOrderStorage orderStorage, ClientService clientService, DriverService driverService) { public OrderService(ClientService clientService, DriverService driverService, OrderRepo orderRepo) {
this.orderStorage = orderStorage;
this.clientService = clientService; this.clientService = clientService;
this.driverService = driverService; this.driverService = driverService;
} this.orderRepo = orderRepo;
@Transactional
public List<Order> getOrdersByClient(String login) {
Client client = clientService.getClientByLogin(login);
return orderStorage.getOrdersByClientId(client.getId());
} }
@Transactional @Transactional
public List<Order> getOrdersPageByClient(String login, int size, int page) { public List<Order> getOrdersPageByClient(String login, int size, int page) {
// Client client = clientService.getClientByLogin(login);
// return orderStorage.readPageByClientId(size, page, client.getId());
Client client = clientService.getClientByLogin(login); Client client = clientService.getClientByLogin(login);
return orderStorage.readPageByClientId(size, page, client.getId()); return orderRepo.findByClient(client, PageRequest.of(page - 1, size)).getContent();
} }
@Transactional @Transactional
@ -44,15 +42,13 @@ public class OrderService {
Order order = new Order(orderDTO.getFrom(), orderDTO.getTo(), calculatePrice(orderDTO), Order order = new Order(orderDTO.getFrom(), orderDTO.getTo(), calculatePrice(orderDTO),
orderDTO.getDeliveryWeight(), driver, client); orderDTO.getDeliveryWeight(), driver, client);
orderStorage.insert(order); //orderStorage.insert(order);
orderRepo.insert(order);
} }
@Transactional @Transactional
public int getPagesCount(String login, int size) { public int getPagesCount(String login, int size) {
Client client = clientService.getClientByLogin(login); Client client = clientService.getClientByLogin(login);
int count = orderStorage.getCountByClientId(client.getId()); return orderRepo.findByClient(client, PageRequest.of(1, size)).getTotalPages();
int res = count / size;
if(count % size != 0) res++;
return res;
} }
@Transactional @Transactional
private double calculatePrice(CreateOrderDTO orderDTO) { private double calculatePrice(CreateOrderDTO orderDTO) {