98 lines
2.0 KiB
C#
98 lines
2.0 KiB
C#
using TransportLogisticContracts.BindingModels;
|
||
using TransportLogisticContracts.BusinessLogicsContracts;
|
||
using TransportLogisticContracts.SearchModels;
|
||
using TransportLogisticContracts.StorageContracts;
|
||
using TransportLogisticContracts.ViewModels;
|
||
|
||
namespace TransportLogisticBusinessLogic
|
||
{
|
||
public class GoodLogic : IGoodLogic
|
||
{
|
||
public readonly IGoodStorage _GoodStorage;
|
||
|
||
public GoodLogic(IGoodStorage GoodStorage)
|
||
{
|
||
_GoodStorage = GoodStorage;
|
||
}
|
||
|
||
public List<GoodViewModel>? ReadList(GoodSearchModel? model)
|
||
{
|
||
var list = model == null ? _GoodStorage.GetFullList() : _GoodStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public GoodViewModel? ReadElement(GoodSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _GoodStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public bool Create(GoodBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_GoodStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(GoodBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_GoodStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Update(GoodBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_GoodStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(GoodBindingModel 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 = _GoodStorage.GetElement(new GoodSearchModel
|
||
{
|
||
Name = model.Name
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Заказчик с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|