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

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