PIbd-32.-Stroev-V.M.-COP/WinFormsLibrary1/Businesslogic/BusinessLogics/ProductLogic.cs

104 lines
3.1 KiB
C#
Raw Normal View History

2024-10-27 16:16:44 +04:00
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogics.BusinessLogics
{
public class ProductLogic : IProductLogic
{
private readonly IProductStorage _productStorage;
public ProductLogic(IProductStorage productStorage)
{
_productStorage = productStorage;
}
public List<ProductViewModel> ReadList(ProductSearchModel? model)
{
var list = model == null ? _productStorage.GetFullList() : _productStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
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 bool Create(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ProductBindingModel model)
{
CheckModel(model);
if (_productStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ProductBindingModel model)
{
CheckModel(model, false);
if (_productStorage.Delete(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.Name))
{
throw new ArgumentException("Введите название продукта", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Description))
{
throw new ArgumentException("Введите описание продукта", nameof(model.Description));
}
if (model.Category == null)
{
throw new ArgumentException("Введите категорию продукта", nameof(model.Category));
}
if (model.CountOnStorage < 0)
{
throw new ArgumentException("Укажите валидное число продукта на складе", nameof(model.CountOnStorage));
}
}
}
}