111 lines
3.3 KiB
C#
111 lines
3.3 KiB
C#
using RestaurantContracts.BindingModels;
|
||
using RestaurantContracts.BusinessLogicsContracts;
|
||
using RestaurantContracts.SearchModels;
|
||
using RestaurantContracts.StoragesContracts;
|
||
using RestaurantContracts.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace RestaurantBusinessLogic.BusinessLogics
|
||
{
|
||
public class ComponentLogic : IComponentLogic
|
||
{
|
||
private readonly IComponentStorage _componentStorage;
|
||
|
||
public ComponentLogic(IComponentStorage componentStorage)
|
||
{
|
||
_componentStorage = componentStorage;
|
||
}
|
||
|
||
public bool Create(ComponentBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_componentStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(ComponentBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_componentStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public ComponentViewModel? ReadElement(ComponentSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _componentStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public List<ComponentViewModel>? ReadList(ComponentSearchModel? model)
|
||
{
|
||
var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public bool Update(ComponentBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_componentStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(ComponentBindingModel 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));
|
||
}
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Нет цены у компонента", nameof(model.Price));
|
||
}
|
||
if (model.Count <= 0)
|
||
{
|
||
throw new ArgumentNullException("Нет количества у компонента", nameof(model.Count));
|
||
}
|
||
var element = _componentStorage.GetElement(new ComponentSearchModel
|
||
{
|
||
Name = model.Name,
|
||
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Компонент с таким логином уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|