PIbd-33_Dyakonov_R_R_COP_14/BusinessLogic/ProductLogic.cs

77 lines
1.8 KiB
C#
Raw Normal View History

2024-10-28 20:58:57 +04:00
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic
{
public class ProductLogic : IProductLogic
{
private readonly IProductStorage _ProductStorage;
public ProductLogic(IProductStorage ProductStorage)
{
_ProductStorage = ProductStorage;
}
public void CreateOrUpdate(ProductBindingModel model)
{
var element = _ProductStorage.GetElement(
new ProductBindingModel
{
DeliveryDate = model.DeliveryDate,
Name = model.Name,
Manufacturers = model.Manufacturers,
Image = model.Image
});
if (element != null && element.Id != model.Id)
{
throw new Exception("Клиент с таким именем уже существует");
}
if (model.Id.HasValue)
{
_ProductStorage.Update(model);
}
else
{
_ProductStorage.Insert(model);
}
}
public void Delete(ProductBindingModel model)
{
var element = _ProductStorage.GetElement(new ProductBindingModel { Id = model.Id });
if (element == null)
{
throw new Exception("Клиент не найден");
}
_ProductStorage.Delete(model);
}
public List<ProductViewModel> Read(ProductBindingModel model)
{
if (model == null)
{
return _ProductStorage.GetFullList();
}
if (model.Id.HasValue)
{
return new List<ProductViewModel> { _ProductStorage.GetElement(model) };
}
return _ProductStorage.GetFilteredList(model);
}
public List<(int, double)> GetWordInfo()
{
var data = _ProductStorage.GetProductCountByManufacturer();
return data
.Select((g, index) => (Index: index + 1, Value: (double)g.Count))
.ToList();
}
}
}