using ComputerStoreContracts.BindingModels; using ComputerStoreContracts.BusinessLogicContracts; using ComputerStoreContracts.SearchModels; using ComputerStoreContracts.StorageContracts; using ComputerStoreContracts.ViewModels; using ComputerStoreDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ComputerStoreBusinessLogic.BusinessLogic { public class OrderLogic : IOrderLogic { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; public OrderLogic(ILogger logger, IOrderStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); if (model.Status != OrderStatus.Unknown) { _logger.LogWarning("Insert operation failed. Order status incorrect."); return false; } model.Status = OrderStatus.Accepted; if (_orderStorage.Insert(model) == null) { model.Status = OrderStatus.Unknown; _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool StatusUpdate(OrderBindingModel rawModel, OrderStatus newStatus) { var viewModel = _orderStorage.GetElement(new OrderSearchModel { ID = rawModel.ID }); if (viewModel == null) { _logger.LogWarning("Order model not found"); return false; } OrderBindingModel model = new OrderBindingModel { ID = viewModel.ID, ConsignmentID = viewModel.ConsignmentID, RequestID = viewModel.RequestID, Status = viewModel.Status, DateCreate = viewModel.DateCreate, DateImplement = viewModel.DateImplement, Price = viewModel.Price }; CheckModel(model, false); if (model.Status + 1 != newStatus) { _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); return false; } model.Status = newStatus; if (model.Status == OrderStatus.Given) model.DateImplement = DateTime.Now; if (_orderStorage.Update(model) == null) { model.Status--; _logger.LogWarning("Update operation failed"); return false; } return true; } public bool TakeOrderInProcess(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.InProcess); } public bool TakeOrderReady(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Ready); } public bool TakeOrderGiven(OrderBindingModel model) { return StatusUpdate(model, OrderStatus.Given); } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("Order. OrderID:{Id}", model?.ID); var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } private void CheckModel(OrderBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.RequestID.ToString()) && string.IsNullOrEmpty(model.ConsignmentID.ToString())) { throw new ArgumentNullException("Incorrect consignment or request ID", nameof(model.ConsignmentID)); } if (string.IsNullOrEmpty(model.RequestID.ToString()) && model.ConsignmentID < 0) { throw new ArgumentNullException("Incorrect consignment ID", nameof(model.ConsignmentID)); } if (string.IsNullOrEmpty(model.ConsignmentID.ToString()) && model.RequestID < 0) { throw new ArgumentNullException("Incorrect request ID", nameof(model.RequestID)); } if (model.Price <= 0) { throw new ArgumentNullException("Incorrect Price", nameof(model.Price)); } if (string.IsNullOrEmpty(model.SellerID.ToString()) && model.SellerID < 0) { throw new ArgumentNullException("Incorrect seller ID", nameof(model.SellerID)); } _logger.LogInformation("Order. OrderID:{ ID}.RequestID:{ RequestID}. ConsignmentID: { ConsignmentID}. SellerID: { SellerID}", model.ID, model.RequestID, model.ConsignmentID, model.SellerID); } } }