COP3PLUSSagirov/BuisnessLogic/DeliveryLogic.cs
2024-11-06 15:40:33 +04:00

101 lines
2.9 KiB
C#

using Contracts.BindingModels;
using Contracts.BuisnessLogicsContracts;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
namespace BuisnessLogic
{
public class DeliveryLogic : IDeliveryLogic
{
IDeliveryStorage _deliveryStorage;
public DeliveryLogic(IDeliveryStorage deliveryStorage)
{
_deliveryStorage = deliveryStorage;
}
public bool Create(DeliveryBindingModel model)
{
CheckModel(model);
if (_deliveryStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(DeliveryBindingModel model)
{
CheckModel(model, false);
if (_deliveryStorage.Delete(model) == null)
{
return false;
}
return true;
}
public DeliveryViewModel? ReadElement(DeliverySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _deliveryStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<DeliveryViewModel>? ReadList(DeliverySearchModel? model)
{
var list = model == null ? _deliveryStorage.GetFullList() : _deliveryStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(DeliveryBindingModel model)
{
CheckModel(model);
if (_deliveryStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(DeliveryBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.CourierFIO))
{
throw new ArgumentNullException("Нет ФИО Курьера", nameof(model.CourierFIO));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона офиса", nameof(model.Phone));
}
if (string.IsNullOrEmpty(model.Image))
{
throw new ArgumentNullException("Нет фото посылки", nameof(model.Image));
}
if (string.IsNullOrEmpty(model.Type))
{
throw new ArgumentNullException("Нет типа посылки", nameof(model.Type));
}
}
}
}