diff --git a/src/main/java/org/dbms/service/ClientService.java b/src/main/java/org/dbms/service/ClientService.java new file mode 100644 index 0000000..f5e1f8d --- /dev/null +++ b/src/main/java/org/dbms/service/ClientService.java @@ -0,0 +1,42 @@ +package org.dbms.service; + +import org.dbms.dto.ClientLoginDto; +import org.dbms.dto.ClientSignupDto; +import org.dbms.exceptions.UserExistsException; +import org.dbms.models.Client; +import org.dbms.storageContracts.IClientStorage; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Objects; + +@Service +public class ClientService { + IClientStorage clientStorage; + + public ClientService(IClientStorage clientStorage) { + this.clientStorage = clientStorage; + } + @Transactional + public String signup(ClientSignupDto client) { + if(clientStorage.getByLogin(client.getLogin()) != null) { + throw new UserExistsException(client.getLogin()); + } + + clientStorage.insert(new Client(client.getName(), client.getPhone(), client.getLogin(), client.getPassword())); + return clientStorage.getIdByLogin(client.getLogin()); + } + @Transactional + public boolean login(ClientLoginDto client) { + Client c = clientStorage.getByLogin(client.getLogin()); + if(c == null) return false; + if(!Objects.equals(c.getPassword(), client.getPassword())) return false; + + return true; + } + + @Transactional + public Client getClientByLogin(String login) { + return clientStorage.getByLogin(login); + } +} diff --git a/src/main/java/org/dbms/service/DriverService.java b/src/main/java/org/dbms/service/DriverService.java new file mode 100644 index 0000000..2407248 --- /dev/null +++ b/src/main/java/org/dbms/service/DriverService.java @@ -0,0 +1,57 @@ +package org.dbms.service; + +import org.dbms.dto.DriverDTO; +import org.dbms.models.Driver; +import org.dbms.searchModels.DriverSearch; +import org.dbms.storageContracts.IDriverStorage; +import org.dbms.storageImpl.DriverStorage; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +public class DriverService { + private IDriverStorage driverStorage; + + 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); + } + + @Transactional + public int getFilteredCount(int expFrom, int expTo, int size) { + DriverSearch ds = new DriverSearch(); + + if(expFrom != -1) ds.expirienceFrom = expFrom; + if(expTo != -1) ds.expirienceTo = expTo; + + int count = driverStorage.getFilteredCount(ds); + int res = count / size; + if(count % size != 0) res++; + return res; + } + @Transactional + public List getFilteredPage(int expFrom, int expTo, int size, int page) { + DriverSearch ds = new DriverSearch(); + + if(expFrom != -1) ds.expirienceFrom = expFrom; + if(expTo != -1) ds.expirienceTo = expTo; + + return driverStorage.getFilteredPage(ds, size, page); + } + + @Transactional + public Driver getDriverById(Long driverId) { + return driverStorage.getElementById(driverId); + } +} diff --git a/src/main/java/org/dbms/service/OrderService.java b/src/main/java/org/dbms/service/OrderService.java new file mode 100644 index 0000000..110435e --- /dev/null +++ b/src/main/java/org/dbms/service/OrderService.java @@ -0,0 +1,70 @@ +package org.dbms.service; + +import org.dbms.dto.CreateOrderDTO; +import org.dbms.models.Client; +import org.dbms.models.Driver; +import org.dbms.models.Order; +import org.dbms.storageContracts.IOrderStorage; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RequestParam; + +import java.time.LocalDate; +import java.util.Date; +import java.util.List; + +@Service +public class OrderService { + private IOrderStorage orderStorage; + private ClientService clientService; + private DriverService driverService; + + public OrderService(IOrderStorage orderStorage, ClientService clientService, DriverService driverService) { + this.orderStorage = orderStorage; + this.clientService = clientService; + this.driverService = driverService; + } + + @Transactional + public List getOrdersByClient(String login) { + Client client = clientService.getClientByLogin(login); + return orderStorage.getOrdersByClientId(client.getId()); + } + + @Transactional + public List getOrdersPageByClient(String login, int size, int page) { + Client client = clientService.getClientByLogin(login); + return orderStorage.readPageByClientId(size, page, client.getId()); + } + + @Transactional + public void createOrder(CreateOrderDTO orderDTO) { + Client client = clientService.getClientByLogin(orderDTO.getLogin()); + Driver driver = driverService.getDriverById(orderDTO.getDriverId()); + Order order = new Order(orderDTO.getFrom(), orderDTO.getTo(), calculatePrice(orderDTO), + orderDTO.getDeliveryWeight(), driver, client); + + orderStorage.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; + } + @Transactional + private double calculatePrice(CreateOrderDTO orderDTO) { + Driver driver = driverService.getDriverById(orderDTO.getDriverId()); + double frigileCoef = 1.0; + if(orderDTO.isFragile()) frigileCoef = 1.1; + + double driverCoef = LocalDate.now().getYear() - driver.getLicenseYear().getYear() - 1900; + driverCoef += 5; + driverCoef /= 5; + + double res = orderDTO.getDeliveryWeight() * 50 * frigileCoef * driverCoef; + return res; + } +}