PIbd-31_PotapovNS_COP_20/InternetShop/InternetShopLogics/Logics/OrderLogic.cs

102 lines
3.1 KiB
C#

using InternetShopContracts.DataBindingModels;
using InternetShopContracts.DataSearchModels;
using InternetShopContracts.DataViewModels;
using InternetShopContracts.LogicsContracts;
using InternetShopContracts.StorageContracts;
namespace InternetShopLogics.Logics
{
public class OrderLogic : IOrderLogic
{
private IOrderStorage _orderStorage;
public OrderLogic(IOrderStorage orderStorage)
{
_orderStorage = orderStorage;
}
public bool Create(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(OrderBindingModel model)
{
CheckModel(model, false);
if (_orderStorage.Delete(model) == null)
{
return false;
}
return true;
}
public OrderViewModel? ReadElement(OrderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
return _orderStorage.GetElement(model);
}
public List<OrderViewModel> ReadList(OrderSearchModel? model = null)
{
List<OrderViewModel>? list = null;
if (model == null)
{
list = _orderStorage.GetFullList();
}
else
{
list = _orderStorage.GetFilteredList(model);
}
if (list == null)
{
return new List<OrderViewModel>();
}
return list;
}
public bool Update(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(OrderBindingModel model, bool checkParams = true)
{
if (model == null) throw new ArgumentNullException(nameof(model));
if (!checkParams) return;
if (string.IsNullOrEmpty(model.CustomerFIO))
{
throw new ArgumentNullException("Нет фио заказчика", nameof(model.CustomerFIO));
}
if (string.IsNullOrEmpty(model.CustomerEmail))
{
throw new ArgumentNullException("Нет email заказчика", nameof(model.CustomerEmail));
}
if (string.IsNullOrEmpty(model.ImagePath))
{
throw new ArgumentNullException("Нет фото заказа", nameof(model.ImagePath));
}
if (model.ProductNames == null)
{
throw new ArgumentNullException("Список товаров не инициализирован", nameof(model.ProductNames));
}
else if (model.ProductNames.Count == 0)
{
throw new ArgumentException("Список товаров пуст", nameof(model.ProductNames));
}
}
}
}