using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.SearchModels; using DressAtelierContracts.StorageContracts; using DressAtelierContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Transactions; using System.Xml.Linq; namespace DressAtelierBusinessLogic.BusinessLogic { public class AtelierLogic : IAtelierLogic { private readonly ILogger _logger; private readonly IAtelierStorage _atelierStorage; private readonly IDressStorage _dressStorage; public AtelierLogic(ILogger logger, IAtelierStorage atelierStorage, IDressStorage dressStorage) { _logger = logger; _atelierStorage = atelierStorage; _dressStorage = dressStorage; } public bool Create(AtelierBindingModel model) { CheckAtelier(model); if(_atelierStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(AtelierBindingModel model) { CheckAtelier(model); if(_atelierStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(AtelierBindingModel model) { CheckAtelier(model, false); _logger.LogInformation("Delete. ID:{ID}", model.ID); if(_atelierStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public AtelierViewModel? ReadElement(AtelierSearchModel model) { if(model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. AtelierName:{Name}.ID:{ ID}", model.Name, model.ID); var atelier = _atelierStorage.GetElement(model); if(atelier == null) { _logger.LogWarning("ReadElement atelier not found"); return null; } _logger.LogInformation("ReadElement find. ID:{ID}", atelier.ID); return atelier; } public List? ReadList(AtelierSearchModel? model) { _logger.LogInformation("ReadList. AtelierName:{AtelierName}. ID:{ ID}", model?.Name, model?.ID); var list = model == null ? _atelierStorage.GetFullList() : _atelierStorage.GetFilteredList(model); if(list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } private void CheckAtelier(AtelierBindingModel model, bool withParams = true) { if(model == null) { throw new ArgumentNullException(nameof(model)); } if(!withParams) { return; } if(string.IsNullOrEmpty(model.Name)) { throw new ArgumentNullException("Invalid atelier name", nameof(model.Name)); } if(string.IsNullOrEmpty(model.Address)) { throw new ArgumentNullException("Invalid atelier address", nameof(model.Address)); } _logger.LogInformation("Atelier. AtelierName:{Name}. Address:{Address}. ID: {ID}", model.Name, model.Address, model.ID); var element = _atelierStorage.GetElement(new AtelierSearchModel { Name = model.Name }); if(element != null && (element.ID == model.ID)) { throw new InvalidOperationException("Atelier with such name already exists"); } } public bool CheckQuantity(int quantity) { return _atelierStorage.CheckDressesQuantity(quantity); } public (bool result,int quantity) AddDress(AtelierBindingModel? atelierModel, DressBindingModel dressModel, int quantity) { if(dressModel == null || quantity == 0) { return (false,-1); } var dress = _dressStorage.GetElement(new DressSearchModel { ID = dressModel.ID }); var atelier = _atelierStorage.GetElement(new AtelierSearchModel { ID = atelierModel.ID }); if(!atelier.DressesList.ContainsKey(dress.ID)) { atelier.DressesList.Add(dress.ID, (dress, 1)); quantity--; } while(atelier.DressesList.Sum(x => x.Value.Item2) < atelier.MaxTotalOfDresses && quantity != 0) { var qnt = atelier.DressesList[dressModel.ID]; qnt.Item2++; atelier.DressesList[dressModel.ID] = qnt; quantity--; } atelierModel.Name = atelier.Name; atelierModel.Address = atelier.Address; atelierModel.DressesList = atelier.DressesList; atelierModel.OpeningDate = atelier.OpeningDate; atelierModel.MaxTotalOfDresses = atelier.MaxTotalOfDresses; if (_atelierStorage.Update(atelierModel) == null) { _logger.LogWarning("Restocking operation failed"); throw new Exception("Something went wrong."); } return (true, quantity); } public bool SellDress(DressSearchModel model, int quantity) { if(_atelierStorage.SellDresses(model,quantity)) { return true; } return false; } } }