98 lines
2.1 KiB
C#
98 lines
2.1 KiB
C#
using TransportLogisticContracts.BindingModels;
|
||
using TransportLogisticContracts.BusinessLogicsContracts;
|
||
using TransportLogisticContracts.SearchModels;
|
||
using TransportLogisticContracts.StorageContracts;
|
||
using TransportLogisticContracts.ViewModels;
|
||
|
||
namespace TransportLogisticBusinessLogic
|
||
{
|
||
public class CustomerLogic : ICustomerLogic
|
||
{
|
||
public readonly ICustomerStorage _CustomerStorage;
|
||
|
||
public CustomerLogic(ICustomerStorage CustomerStorage)
|
||
{
|
||
_CustomerStorage = CustomerStorage;
|
||
}
|
||
|
||
public List<CustomerViewModel>? ReadList(CustomerSearchModel? model)
|
||
{
|
||
var list = model == null ? _CustomerStorage.GetFullList() : _CustomerStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public CustomerViewModel? ReadElement(CustomerSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _CustomerStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public bool Create(CustomerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_CustomerStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(CustomerBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_CustomerStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(CustomerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_CustomerStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(CustomerBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.Name))
|
||
{
|
||
throw new ArgumentNullException("Нет имени заказчика",
|
||
nameof(model.Name));
|
||
}
|
||
var element = _CustomerStorage.GetElement(new CustomerSearchModel
|
||
{
|
||
Name = model.Name
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Заказчик с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|