бизнес логика
This commit is contained in:
parent
e4ec593863
commit
d8b4905639
42
src/main/java/org/dbms/service/ClientService.java
Normal file
42
src/main/java/org/dbms/service/ClientService.java
Normal file
@ -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);
|
||||
}
|
||||
}
|
57
src/main/java/org/dbms/service/DriverService.java
Normal file
57
src/main/java/org/dbms/service/DriverService.java
Normal file
@ -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<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
|
||||
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<Driver> 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);
|
||||
}
|
||||
}
|
70
src/main/java/org/dbms/service/OrderService.java
Normal file
70
src/main/java/org/dbms/service/OrderService.java
Normal file
@ -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<Order> getOrdersByClient(String login) {
|
||||
Client client = clientService.getClientByLogin(login);
|
||||
return orderStorage.getOrdersByClientId(client.getId());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Order> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user