using ElectronicsShopContracts.BindingModels; using ElectronicsShopContracts.BusinessLogicContracts; using ElectronicsShopContracts.SearchModels; using ElectronicsShopContracts.StorageContracts; using ElectronicsShopContracts.ViewModels; using ElectronicsShopDataModels.Enums; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ElectronicsShopBusinessLogic.BusinessLogic { public class OrderLogic : IOrderLogic { private readonly ILogger _logger; private readonly IOrderStorage _storage; public OrderLogic(ILogger logger, IOrderStorage storage) { _logger = logger; _storage = storage; } public bool CreateOrder(OrderBindingModel model) { CheckModel(model); if (_storage.Insert(model) == null) { _logger.LogInformation("Insert operation failed"); return false; } return true; } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation($"ReadList:ID:{model?.ID}"); var list = model == null ? _storage.GetFullList() : _storage.GetFilteredList(model); ; if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation($"ReadList.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.ID).ToString())) { throw new ArgumentNullException("Нет ID заказа", nameof(model.ID)); } if (model.Sum <= 0) { throw new ArgumentNullException("Цена зака должна быть больше 0", nameof(model.Sum)); } _logger.LogInformation($"Order. ID:{model.ID}.Sum:{model.Sum}.DateCreate:{model.DateCreate}."); } public OrderViewModel? ReadElement(OrderSearchModel model) { if (model == null) throw new ArgumentNullException(nameof(model)); _logger.LogInformation($"ReadElement. ID:{model.ID}"); var element = _storage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement. elementn not found"); return null; } _logger.LogInformation($"ReadElement find. ID:{element.ID}"); return element; } } }