SUBD_SushiBar/SushiBar/SushiBarBusinessLogic/BusinessLogics/BuyerLogic.cs

95 lines
2.5 KiB
C#
Raw Normal View History

2024-03-26 20:35:34 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarBusinessLogic.BusinessLogics
{
public class BuyerLogic : IBuyerLogic
{
private readonly IBuyerStorage _buyerStorage;
public BuyerLogic(IBuyerStorage buyerStorage)
{
_buyerStorage = buyerStorage;
}
public BuyerViewModel? ReadElement(BuyerSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _buyerStorage.GetElement(model);
if(element == null)
{
return null;
}
return element;
}
public List<BuyerViewModel>? ReadList(BuyerSearchModel? model)
{
var list = model == null ? _buyerStorage.GetFullList() : _buyerStorage.GetFilteredList(model);
if(list == null)
{
return null;
}
return list;
}
public bool Create(BuyerBindingModel model)
{
CheckModel(model);
if (_buyerStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(BuyerBindingModel model)
{
CheckModel(model, false);
if(_buyerStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(BuyerBindingModel model)
{
CheckModel(model);
if(_buyerStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(BuyerBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BuyerName))
{
throw new ArgumentNullException("Нет названия", nameof(model.BuyerName));
}
}
}
}