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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<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.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<Client> getAll() {
return repo.findAll();
}
}
;

View File

@ -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<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.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<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.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<Parking> parkings = parkingStorage.readAll();
List<Parking> 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<Car> cars = carStorage.readAll();
List<Car> 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<Client> clients = clientStorage.readAll();
List<Driver> drivers = driverStorage.readAll();
List<Client> clients = clientRepo.findAll();
List<Driver> 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);

View File

@ -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) {

View File

@ -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() {

View File

@ -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) {

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

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.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);
}
}

View File

@ -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<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);
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<Driver> res = driverRepo.findBylicenseYearBetween(from, to, PageRequest.of(1, size));
return res.getTotalPages();
}
@Transactional
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;
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<Driver> 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();
}
}

View File

@ -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<Order> getOrdersByClient(String login) {
Client client = clientService.getClientByLogin(login);
return orderStorage.getOrdersByClientId(client.getId());
this.orderRepo = orderRepo;
}
@Transactional
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);
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) {