diff --git a/pom.xml b/pom.xml index a68998b..6c8be59 100644 --- a/pom.xml +++ b/pom.xml @@ -45,6 +45,10 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-data-mongodb + org.hibernate.validator diff --git a/src/main/java/org/dbms/controllers/ClientController.java b/src/main/java/org/dbms/controllers/ClientController.java index 7a1b6ff..7a398fa 100644 --- a/src/main/java/org/dbms/controllers/ClientController.java +++ b/src/main/java/org/dbms/controllers/ClientController.java @@ -4,6 +4,8 @@ import org.dbms.dto.ClientLoginDto; import org.dbms.dto.ClientSignupDto; import org.dbms.dto.DriverDTO; import org.dbms.exceptions.UserExistsException; +import org.dbms.models.Client; +import org.dbms.repos.ClientRepo; import org.dbms.searchModels.DriverSearch; import org.dbms.service.ClientService; import org.dbms.storageImpl.ClientStorage; @@ -19,9 +21,11 @@ import java.util.stream.Collectors; @RestController public class ClientController { private final ClientService clientService; + private final ClientRepo repo; - public ClientController(ClientService clientService) { + public ClientController(ClientService clientService, ClientRepo repo) { this.clientService = clientService; + this.repo = repo; } @PostMapping("/client") @@ -47,5 +51,10 @@ public class ClientController { return "Неверный логин или пароль"; } } + + @GetMapping("/all-clients") + public List getAll() { + return repo.findAll(); + } } ; \ No newline at end of file diff --git a/src/main/java/org/dbms/controllers/DriverController.java b/src/main/java/org/dbms/controllers/DriverController.java index 7e212d5..c35fcf9 100644 --- a/src/main/java/org/dbms/controllers/DriverController.java +++ b/src/main/java/org/dbms/controllers/DriverController.java @@ -1,6 +1,7 @@ package org.dbms.controllers; import org.dbms.dto.DriverDTO; +import org.dbms.repos.DriverRepo; import org.dbms.searchModels.DriverSearch; import org.dbms.service.DriverService; import org.dbms.storageImpl.DriverStorage; @@ -12,9 +13,11 @@ import java.util.stream.Collectors; @RestController public class DriverController { private final DriverService driverService; + private final DriverRepo repo; - public DriverController(DriverService driverService) { + public DriverController(DriverService driverService, DriverRepo repo) { this.driverService = driverService; + this.repo = repo; } @GetMapping("/drivers") @@ -33,4 +36,9 @@ public class DriverController { return driverService.getFilteredCount(expFrom, expTo, size); } + + @GetMapping("/all-drivers") + public List getAll() { + return repo.findAll().stream().map(DriverDTO::new).toList(); + } } diff --git a/src/main/java/org/dbms/controllers/OrderController.java b/src/main/java/org/dbms/controllers/OrderController.java index 383565c..85ab76c 100644 --- a/src/main/java/org/dbms/controllers/OrderController.java +++ b/src/main/java/org/dbms/controllers/OrderController.java @@ -5,6 +5,8 @@ import org.dbms.dto.ClientSignupDto; import org.dbms.dto.CreateOrderDTO; import org.dbms.dto.OrderDTO; import org.dbms.exceptions.UserExistsException; +import org.dbms.models.Order; +import org.dbms.repos.OrderRepo; import org.dbms.service.ClientService; import org.dbms.service.OrderService; import org.springframework.web.bind.annotation.*; @@ -17,9 +19,11 @@ import java.util.List; @RestController public class OrderController { private final OrderService orderService; + private final OrderRepo repo; - public OrderController(OrderService orderService) { + public OrderController(OrderService orderService, OrderRepo repo) { this.orderService = orderService; + this.repo = repo; } @GetMapping("/orders") @@ -37,5 +41,10 @@ public class OrderController { public int getPagesCount(@RequestParam String login, @RequestParam int size) { return orderService.getPagesCount(login, size); } + + @GetMapping("/all-orders") + public List getAll() { + return repo.findAll(); + } } ; \ No newline at end of file diff --git a/src/main/java/org/dbms/data/DataGenerator.java b/src/main/java/org/dbms/data/DataGenerator.java index 1861b10..8c863a2 100644 --- a/src/main/java/org/dbms/data/DataGenerator.java +++ b/src/main/java/org/dbms/data/DataGenerator.java @@ -5,6 +5,10 @@ import org.dbms.models.Car; import org.dbms.models.Client; import org.dbms.models.Driver; 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.storageContracts.ICarStorage; import org.dbms.storageContracts.IClientStorage; @@ -16,11 +20,11 @@ import java.util.*; @Service public class DataGenerator { - private ICarStorage carStorage; - private IParkingStorage parkingStorage; - private IClientStorage clientStorage; + private CarRepo carRepo; + private ParkingRepo parkingRepo; + private ClientRepo clientRepo; private OrderService orderService; - private IDriverStorage driverStorage; + private DriverRepo driverRepo; private static String surnames[] = {"Иванов", "Петров", "Сидоров", "Александров", "Панов", "Ежов", "Зотов", "Котов", "зощенков"}; @@ -29,13 +33,13 @@ public class DataGenerator { private static String cities[] = {"Ульяновск", "Самара", "Москва", "Уфа", "Казань", "Нижний новгород", "Санкт петербург"}; private static String streets[] = {"пр. Нариманова", "ул. Ленина", "ул. Карла Маркса", "ул. Радищева", "ул. Гагарина"}; - public DataGenerator(ICarStorage carStorage, IParkingStorage parkingStorage, - IClientStorage clientStorage, OrderService orderService, IDriverStorage driverStorage) { - this.carStorage = carStorage; - this.parkingStorage = parkingStorage; - this.clientStorage = clientStorage; + public DataGenerator(CarRepo carRepo, ParkingRepo parkingRepo, + ClientRepo clientRepo, OrderService orderService, DriverRepo driverRepo) { + this.carRepo = carRepo; + this.parkingRepo = parkingRepo; + this.clientRepo = clientRepo; this.orderService = orderService; - this.driverStorage = driverStorage; + this.driverRepo = driverRepo; } private String generateStr() { @@ -76,7 +80,7 @@ public class DataGenerator { } private void generateCars() { - List parkings = parkingStorage.readAll(); + List parkings = parkingRepo.findAll(); Random rnd = new Random(); String models[] = {"Газель", "Камаз", "Volvo", "Трактор"}; @@ -85,7 +89,7 @@ public class DataGenerator { double cost = rnd.nextDouble(400000) + 50000; 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); - clientStorage.insert(client); + clientRepo.insert(client); } } @@ -117,12 +121,12 @@ public class DataGenerator { for(int i = 0; i < 600; ++i) { String address = generateAddress(); Parking parking = new Parking(address, rnd.nextInt(20) + 10); - parkingStorage.insert(parking); + parkingRepo.insert(parking); } } private void generateDrivers() { - List cars = carStorage.readAll(); + List cars = carRepo.findAll(); Random rnd = new Random(); for(int i = 0; i < 1000; ++i) { @@ -130,13 +134,13 @@ public class DataGenerator { cars.remove(car); Driver driver = new Driver(generateName(), generateDate(), car); - driverStorage.insert(driver); + driverRepo.insert(driver); } } private void generateOrders() { - List clients = clientStorage.readAll(); - List drivers = driverStorage.readAll(); + List clients = clientRepo.findAll(); + List drivers = driverRepo.findAll(); Random rnd = new Random(); for(int i = 0; i < 2000; ++i) { @@ -145,7 +149,7 @@ public class DataGenerator { int deliveryWeight = rnd.nextInt(30) + 5; 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()); orderService.createOrder(dto); diff --git a/src/main/java/org/dbms/dto/CarDTO.java b/src/main/java/org/dbms/dto/CarDTO.java index 70efeb1..84c597b 100644 --- a/src/main/java/org/dbms/dto/CarDTO.java +++ b/src/main/java/org/dbms/dto/CarDTO.java @@ -5,7 +5,7 @@ import org.dbms.models.Car; import java.util.Date; public class CarDTO { - private Long id; + private String id; private String model; private Date year; private double cost; @@ -13,13 +13,17 @@ public class CarDTO { public CarDTO(Car car) { if(car == null) return; - this.id = car.getId(); + this.id = car.getMongoId(); this.model = car.getModel(); this.year = car.getYear(); this.cost = car.getCost(); } public Long getId() { + return 1l; + } + + public String getMongoId() { return id; } @@ -36,7 +40,6 @@ public class CarDTO { } public void setId(Long id) { - this.id = id; } public void setCost(double cost) { diff --git a/src/main/java/org/dbms/dto/CreateOrderDTO.java b/src/main/java/org/dbms/dto/CreateOrderDTO.java index 5e66968..a089c1e 100644 --- a/src/main/java/org/dbms/dto/CreateOrderDTO.java +++ b/src/main/java/org/dbms/dto/CreateOrderDTO.java @@ -8,12 +8,12 @@ public class CreateOrderDTO { private String from; private String to; private int deliveryWeight; - private Long driverId; + private String driverId; private String login; private boolean isFragile; 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.driverId = driverId; this.from = from; @@ -34,7 +34,7 @@ public class CreateOrderDTO { public String getLogin() { return login; } - public Long getDriverId() { + public String getDriverId() { return driverId; } public boolean isFragile() { diff --git a/src/main/java/org/dbms/dto/DriverDTO.java b/src/main/java/org/dbms/dto/DriverDTO.java index 16ad9ad..de65000 100644 --- a/src/main/java/org/dbms/dto/DriverDTO.java +++ b/src/main/java/org/dbms/dto/DriverDTO.java @@ -6,14 +6,14 @@ import org.dbms.models.Driver; import java.util.Date; public class DriverDTO { - Long id; + String id; String name; Date licenseYear; CarDTO car; public DriverDTO(Driver driver) { if(driver == null) return; - this.id = driver.getId(); + this.id = driver.getMongoId(); this.name = driver.getName(); this.licenseYear = driver.getLicenseYear(); this.car = new CarDTO(driver.getCar()); @@ -28,11 +28,16 @@ public class DriverDTO { } 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) { - this.id = id; } public void setLicenseYear(Date licenseYear) { diff --git a/src/main/java/org/dbms/models/Car.java b/src/main/java/org/dbms/models/Car.java index fd8b42d..4cfce17 100644 --- a/src/main/java/org/dbms/models/Car.java +++ b/src/main/java/org/dbms/models/Car.java @@ -1,17 +1,20 @@ package org.dbms.models; +import org.springframework.data.annotation.Id; + import java.util.Date; import java.util.Objects; public class Car { - private Long id; + @Id + private String id; private String model; private Date year; private double cost; private Parking parking; 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.model = model; this.year = year; @@ -19,6 +22,13 @@ public class Car { 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) { this.id = id; this.model = model; @@ -28,8 +38,9 @@ public class Car { } public Long getId() { - return id; + return 1l; } + public String getMongoId() {return id; } public Date getYear() { return year; diff --git a/src/main/java/org/dbms/models/Client.java b/src/main/java/org/dbms/models/Client.java index 933f464..bc847a4 100644 --- a/src/main/java/org/dbms/models/Client.java +++ b/src/main/java/org/dbms/models/Client.java @@ -1,14 +1,17 @@ package org.dbms.models; +import org.springframework.data.annotation.Id; + public class Client { - private Long id; + @Id + private String id; private String name; private String phone; private String login; private String password; 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.name = name; this.phone = phone; @@ -16,6 +19,13 @@ public class Client { 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) { this.name = name; this.phone = phone; @@ -24,9 +34,11 @@ public class Client { } public Long getId() { - return id; + return 1l; } + public String getMongoId() {return id;} + public String getName() { return name; } diff --git a/src/main/java/org/dbms/models/Driver.java b/src/main/java/org/dbms/models/Driver.java index ed67824..9fb8a78 100644 --- a/src/main/java/org/dbms/models/Driver.java +++ b/src/main/java/org/dbms/models/Driver.java @@ -1,20 +1,28 @@ package org.dbms.models; +import org.springframework.data.annotation.Id; + import java.util.Date; public class Driver { - Long id; + @Id + String id; String name; Date licenseYear; Car car; 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.name = name; this.licenseYear = licenseYear; 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) { this.name = name; @@ -23,6 +31,9 @@ public class Driver { } public Long getId() { + return 1l; + } + public String getMongoId() { return id; } diff --git a/src/main/java/org/dbms/models/Order.java b/src/main/java/org/dbms/models/Order.java index 9fa8a01..dac50b7 100644 --- a/src/main/java/org/dbms/models/Order.java +++ b/src/main/java/org/dbms/models/Order.java @@ -1,7 +1,10 @@ package org.dbms.models; +import org.springframework.data.annotation.Id; + public class Order { - private Long id; + @Id + private String id; private String from; private String to; private double price; @@ -9,7 +12,9 @@ public class Order { private Driver driver; 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.from = from; this.price = price; @@ -19,6 +24,15 @@ public class Order { 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) { this.from = from; this.to = to; @@ -29,6 +43,9 @@ public class Order { } public Long getId() { + return 1l; + } + public String getMongoId() { return id; } diff --git a/src/main/java/org/dbms/models/Parking.java b/src/main/java/org/dbms/models/Parking.java index 4bcf56f..868bf9e 100644 --- a/src/main/java/org/dbms/models/Parking.java +++ b/src/main/java/org/dbms/models/Parking.java @@ -1,24 +1,36 @@ package org.dbms.models; +import org.springframework.data.annotation.Id; + public class Parking { - private Long id; + @Id + private String id; private String address; private int capacity; public Parking() {} - public Parking(Long id, String address, int capacity) { + public Parking(String id, String address, int capacity) { this.id = id; this.address = address; this.capacity = capacity; } + public Parking(Long id, String address, int capacity) { + this.address = address; + this.capacity = capacity; + } + public Parking(String address, int capacity) { this.address = address; this.capacity = capacity; } public Long getId() { + return 1l; + } + + public String getMongoId() { return id; } diff --git a/src/main/java/org/dbms/repos/CarRepo.java b/src/main/java/org/dbms/repos/CarRepo.java new file mode 100644 index 0000000..e10462f --- /dev/null +++ b/src/main/java/org/dbms/repos/CarRepo.java @@ -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 { +} diff --git a/src/main/java/org/dbms/repos/ClientRepo.java b/src/main/java/org/dbms/repos/ClientRepo.java new file mode 100644 index 0000000..7506c09 --- /dev/null +++ b/src/main/java/org/dbms/repos/ClientRepo.java @@ -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 { + List findByLogin(String login); +} diff --git a/src/main/java/org/dbms/repos/DriverRepo.java b/src/main/java/org/dbms/repos/DriverRepo.java new file mode 100644 index 0000000..4919411 --- /dev/null +++ b/src/main/java/org/dbms/repos/DriverRepo.java @@ -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 { + Page findBylicenseYearBetween(Date from, Date to, Pageable pg); + List findBylicenseYearBetween(Date from, Date to); +} diff --git a/src/main/java/org/dbms/repos/OrderRepo.java b/src/main/java/org/dbms/repos/OrderRepo.java new file mode 100644 index 0000000..e450ea6 --- /dev/null +++ b/src/main/java/org/dbms/repos/OrderRepo.java @@ -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 { + Page findByClient(Client id, Pageable pg); +} diff --git a/src/main/java/org/dbms/repos/ParkingRepo.java b/src/main/java/org/dbms/repos/ParkingRepo.java new file mode 100644 index 0000000..6b94456 --- /dev/null +++ b/src/main/java/org/dbms/repos/ParkingRepo.java @@ -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 { +} diff --git a/src/main/java/org/dbms/service/ClientService.java b/src/main/java/org/dbms/service/ClientService.java index f5e1f8d..f6c9b9e 100644 --- a/src/main/java/org/dbms/service/ClientService.java +++ b/src/main/java/org/dbms/service/ClientService.java @@ -4,6 +4,7 @@ import org.dbms.dto.ClientLoginDto; import org.dbms.dto.ClientSignupDto; import org.dbms.exceptions.UserExistsException; import org.dbms.models.Client; +import org.dbms.repos.ClientRepo; import org.dbms.storageContracts.IClientStorage; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -13,9 +14,10 @@ import java.util.Objects; @Service public class ClientService { IClientStorage clientStorage; - - public ClientService(IClientStorage clientStorage) { + ClientRepo clientRepo; + public ClientService(IClientStorage clientStorage, ClientRepo clientRepo) { this.clientStorage = clientStorage; + this.clientRepo = clientRepo; } @Transactional public String signup(ClientSignupDto client) { @@ -23,12 +25,12 @@ public class ClientService { throw new UserExistsException(client.getLogin()); } - clientStorage.insert(new Client(client.getName(), client.getPhone(), client.getLogin(), client.getPassword())); - return clientStorage.getIdByLogin(client.getLogin()); + Client res = clientRepo.insert(new Client(client.getName(), client.getPhone(), client.getLogin(), client.getPassword())); + return res.getId() + ""; } @Transactional 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(!Objects.equals(c.getPassword(), client.getPassword())) return false; @@ -37,6 +39,6 @@ public class ClientService { @Transactional public Client getClientByLogin(String login) { - return clientStorage.getByLogin(login); + return clientRepo.findByLogin(login).get(0); } } diff --git a/src/main/java/org/dbms/service/DriverService.java b/src/main/java/org/dbms/service/DriverService.java index 2407248..e6c6864 100644 --- a/src/main/java/org/dbms/service/DriverService.java +++ b/src/main/java/org/dbms/service/DriverService.java @@ -2,56 +2,57 @@ package org.dbms.service; import org.dbms.dto.DriverDTO; import org.dbms.models.Driver; +import org.dbms.repos.DriverRepo; import org.dbms.searchModels.DriverSearch; import org.dbms.storageContracts.IDriverStorage; 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.transaction.annotation.Transactional; +import java.util.Date; import java.util.List; @Service public class DriverService { - private IDriverStorage driverStorage; + private DriverRepo driverRepo; - public DriverService(IDriverStorage driverStorage) { - this.driverStorage = driverStorage; - } - - @Transactional - public List 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); + public DriverService(DriverRepo driverRepo) { + this.driverRepo = driverRepo; } @Transactional 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; - if(expTo != -1) ds.expirienceTo = expTo; + Date from = new Date(); + Date to = new Date(); - int count = driverStorage.getFilteredCount(ds); - int res = count / size; - if(count % size != 0) res++; - return res; + from.setYear(from.getYear() - expTo); + to.setYear(to.getYear() - expFrom); + + Page res = driverRepo.findBylicenseYearBetween(from, to, PageRequest.of(1, size)); + return res.getTotalPages(); } @Transactional public List 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; - if(expTo != -1) ds.expirienceTo = expTo; + Date from = new Date(); + Date to = new Date(); - return driverStorage.getFilteredPage(ds, size, page); + from.setYear(from.getYear() - expTo); + to.setYear(to.getYear() - expFrom); + + Page res = driverRepo.findBylicenseYearBetween(from, to, PageRequest.of(page - 1, size)); + return res.getContent(); } @Transactional - public Driver getDriverById(Long driverId) { - return driverStorage.getElementById(driverId); + public Driver getDriverById(String driverId) { + return driverRepo.findById(driverId).get(); } } diff --git a/src/main/java/org/dbms/service/OrderService.java b/src/main/java/org/dbms/service/OrderService.java index 110435e..c91cc40 100644 --- a/src/main/java/org/dbms/service/OrderService.java +++ b/src/main/java/org/dbms/service/OrderService.java @@ -4,7 +4,9 @@ import org.dbms.dto.CreateOrderDTO; import org.dbms.models.Client; import org.dbms.models.Driver; import org.dbms.models.Order; +import org.dbms.repos.OrderRepo; import org.dbms.storageContracts.IOrderStorage; +import org.springframework.data.domain.PageRequest; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.RequestParam; @@ -15,26 +17,22 @@ import java.util.List; @Service public class OrderService { - private IOrderStorage orderStorage; private ClientService clientService; private DriverService driverService; + private OrderRepo orderRepo; - public OrderService(IOrderStorage orderStorage, ClientService clientService, DriverService driverService) { - this.orderStorage = orderStorage; + public OrderService(ClientService clientService, DriverService driverService, OrderRepo orderRepo) { this.clientService = clientService; this.driverService = driverService; - } - - @Transactional - public List getOrdersByClient(String login) { - Client client = clientService.getClientByLogin(login); - return orderStorage.getOrdersByClientId(client.getId()); + this.orderRepo = orderRepo; } @Transactional public List getOrdersPageByClient(String login, int size, int page) { +// Client client = clientService.getClientByLogin(login); +// return orderStorage.readPageByClientId(size, page, client.getId()); Client client = clientService.getClientByLogin(login); - return orderStorage.readPageByClientId(size, page, client.getId()); + return orderRepo.findByClient(client, PageRequest.of(page - 1, size)).getContent(); } @Transactional @@ -44,15 +42,13 @@ public class OrderService { Order order = new Order(orderDTO.getFrom(), orderDTO.getTo(), calculatePrice(orderDTO), orderDTO.getDeliveryWeight(), driver, client); - orderStorage.insert(order); + //orderStorage.insert(order); + orderRepo.insert(order); } @Transactional public int getPagesCount(String login, int size) { Client client = clientService.getClientByLogin(login); - int count = orderStorage.getCountByClientId(client.getId()); - int res = count / size; - if(count % size != 0) res++; - return res; + return orderRepo.findByClient(client, PageRequest.of(1, size)).getTotalPages(); } @Transactional private double calculatePrice(CreateOrderDTO orderDTO) {