SUBD_Aleikin/Restaurant/RestaurantBusinessLogic/BusinessLogics/ComponentLogic.cs
Артём Алейкин e52b239c08 BusinessLogic,
Contracts,
DataModels,
View(пока пусто)
2023-05-02 22:51:41 +04:00

111 lines
3.3 KiB
C#
Raw Permalink 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 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("Компонент с таким логином уже есть");
}
}
}
}