134 lines
3.0 KiB
C#
134 lines
3.0 KiB
C#
using TransportLogisticContracts.BindingModels;
|
||
using TransportLogisticContracts.BusinessLogicsContracts;
|
||
using TransportLogisticContracts.SearchModels;
|
||
using TransportLogisticContracts.StorageContracts;
|
||
using TransportLogisticContracts.ViewModels;
|
||
using TransportLogisticDataModels.Enums;
|
||
|
||
namespace TransportLogisticBusinessLogic
|
||
{
|
||
public class OrderLogic : IOrderLogic
|
||
{
|
||
private readonly IOrderStorage _orderStorage;
|
||
|
||
public OrderLogic(IOrderStorage orderStorage)
|
||
{
|
||
_orderStorage = orderStorage;
|
||
}
|
||
|
||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||
{
|
||
var list = model == null ? _orderStorage.GetFullList() :
|
||
_orderStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public OrderViewModel? ReadElement(OrderSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _orderStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public bool CreateOrder(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (model.Status != OrderStatus.Неизвестен) return false;
|
||
model.Status = OrderStatus.Принят;
|
||
if (_orderStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
|
||
{
|
||
CheckModel(model);
|
||
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
|
||
if (element == null)
|
||
{
|
||
return false;
|
||
}
|
||
if (element.Status != status - 1)
|
||
{
|
||
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
|
||
}
|
||
model.Status = status;
|
||
if (model.Status == OrderStatus.Выдан) model.DateDelivered = DateTime.Now;
|
||
if (_orderStorage.Update(model) == null)
|
||
{
|
||
model.Status--;
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool TakeOrderInWork(OrderBindingModel model)
|
||
{
|
||
return ChangeStatus(model, OrderStatus.В_пути);
|
||
}
|
||
|
||
public bool FinishOrder(OrderBindingModel model)
|
||
{
|
||
return ChangeStatus(model, OrderStatus.Доставлен);
|
||
}
|
||
public bool DeliveryOrder(OrderBindingModel model)
|
||
{
|
||
return ChangeStatus(model, OrderStatus.Выдан);
|
||
}
|
||
public bool Create(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_orderStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_orderStorage.Update(model) == null)
|
||
{
|
||
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(OrderBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_orderStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|