SUBD_TransportLogistic_Puch.../TransportLogistic/TransportLogisticBusinessLogic/CustomerLogic.cs
2024-05-14 18:22:24 +04:00

98 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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("Заказчик с таким названием уже есть");
}
}
}
}