using Microsoft.Extensions.Logging; using SushiBarContracts.BusinessLogicsContracts; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using SushiBarContracts.BindingModels; using SushiBarContracts.SearchModels; using SushiBarContracts.StoragesContracts; using SushiBarContracts.ViewModels; namespace SushiBarBusinessLogic.BusinessLogics { public class SushiLogic :ISushiLogic { private readonly ILogger _logger; private readonly ISushiStorage _SushiStorage; public SushiLogic(ILogger logger, ISushiStorage SushiStorage) { _logger = logger; _SushiStorage = SushiStorage; } public List? ReadList(SushiSearchModel? model) { _logger.LogInformation("ReadList. SushiName:{SushiName}.Id:{ Id}", model?.SushiName, model?.Id); var list = model == null ? _SushiStorage.GetFullList() : _SushiStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public SushiViewModel? ReadElement(SushiSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. SushiName:{SushiName}.Id:{ Id}", model.SushiName, model.Id); var element = _SushiStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public bool Create(SushiBindingModel model) { CheckModel(model); if (_SushiStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(SushiBindingModel model) { CheckModel(model); if (_SushiStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(SushiBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_SushiStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } private void CheckModel(SushiBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.SushiName)) { throw new ArgumentNullException("Нет названия суши", nameof(model.SushiName)); } if (model.Price <= 0) { throw new ArgumentNullException("Цена суши должна быть больше 0", nameof(model.Price)); } if (model.SushiComponents == null || model.SushiComponents.Count == 0) { throw new ArgumentNullException("Перечень ингредиентов не может быть пустым", nameof(model.SushiComponents)); } _logger.LogInformation("Sushi. SushiName:{SushiName}.Price:{Price}.Id: { Id}", model.SushiName, model.Price, model.Id); var element = _SushiStorage.GetElement(new SushiSearchModel { SushiName = model.SushiName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Суши с таким названием уже есть"); } } } }