using ComputerShopContracts.BindingModels; using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.SearchModels; using ComputerShopContracts.StorageContracts; using ComputerShopContracts.ViewModels; using ComputerShopDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ComputerShopBusinessLogic.BusinessLogics { public class SupplyLogic : ISupplyLogic { private readonly ILogger _logger; private readonly ISupplyStorage _supplyStorage; public SupplyLogic(ILogger logger, ISupplyStorage supplyStorage) { _logger = logger; _supplyStorage = supplyStorage; } public bool Create(SupplyBindingModel model) { CheckModel(model); if (_supplyStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(SupplyBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_supplyStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public SupplyViewModel? ReadElement(SupplySearchModel model) { throw new NotImplementedException(); } public List? ReadList(SupplySearchModel? model) { _logger.LogInformation("ReadList. Supplyid:{ Id}", model?.Id); var list = model == null ? _supplyStorage.GetFullList() : _supplyStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool TakeInWork(SupplyBindingModel model) { return StatusUpdate(model, SupplyStatus.Отправляется); } public bool Finish(SupplyBindingModel model) { return StatusUpdate(model, SupplyStatus.Отправлено); } public bool Update(SupplyBindingModel model) { throw new NotImplementedException(); } private void CheckModel(SupplyBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } } public bool StatusUpdate(SupplyBindingModel model, SupplyStatus _newStatus) { var viewModel = _supplyStorage.GetElement(new SupplySearchModel { Id = model.Id }); if (viewModel == null) { throw new ArgumentNullException(nameof(model)); } if (viewModel.Status + 1 != _newStatus) { _logger.LogWarning("Status update to " + _newStatus.ToString() + " operation failed. Order status incorrect."); return false; } model.Status = _newStatus; if (model.Status == SupplyStatus.Отправлено) model.DateImplement = DateTime.Now; else { model.DateImplement = viewModel.DateImplement; } CheckModel(model, false); if (_supplyStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } } }