using DatabaseImplement.Implements; using DataContracts.bindingModels; using DataContracts.BLs; using DataContracts.searchModels; using DataContracts.storages; using DataContracts.viewModels; namespace BusinessLogic { public class ClientBL : IClientBL { private readonly ClientStorage _clientStorage; private readonly ProductBL productBL; public ClientBL() { _clientStorage = new(); productBL = new ProductBL(); } public bool Create(ClientBindingModel model) { CheckModel(model); if (_clientStorage.Insert(model) == null) { return false; } return true; } public List<(string title, int count)> FindDataDiagram() { List<(string, int)> list = new List<(string, int)>(); List clients = ReadList(); List products = productBL.ReadList(); foreach(ProductViewModel product in products) { int count = 0; foreach(ClientViewModel client in clients) { if(client.products.Contains(product.title)) count++; } list.Add((product.title, count)); } return list; } public bool Delete(ClientBindingModel model) { CheckModel(model, false); if (_clientStorage.Delete(model) == null) { return false; } return true; } public ClientViewModel? ReadElement(ClientSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _clientStorage.GetElement(model); if (element == null) { return null; } return element; } public List? ReadList() { var list = _clientStorage.GetFullList(); if (list == null) { return null; } return list; } public bool Update(ClientBindingModel model) { CheckModel(model); if (_clientStorage.Update(model) == null) { return false; } return true; } private void CheckModel(ClientBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.FIO)) { throw new ArgumentNullException("Нет имени клиента", nameof(model.FIO)); } if (string.IsNullOrEmpty(model.email)) { throw new ArgumentNullException("Нет почты клиента", nameof(model.email)); } if (string.IsNullOrEmpty(model.products)) { throw new ArgumentNullException("Нет продуктов клиента", nameof(model.products)); } } } }