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

111 lines
3.2 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 ProductLogic : IProductLogic
{
private readonly IProductStorage _productStorage;
public ProductLogic(IProductStorage productStorage)
{
_productStorage = productStorage;
}
public bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
if (_productStorage.Delete(model) == null)
{
return false;
}
return true;
}
public ProductViewModel? ReadElement(ProductSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _productStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<ProductViewModel>? ReadList(ProductSearchModel? model)
{
var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ProductBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Type))
{
throw new ArgumentNullException("Нет вида продукта", nameof(model.Type));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Нет цены у продукта", nameof(model.Price));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Нет количества у продукта", nameof(model.Count));
}
var element = _productStorage.GetElement(new ProductSearchModel
{
Type = model.Type,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Компонент с таким логином уже есть");
}
}
}
}