using LawFirmContracts.BindingModels; using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.SearchModels; using LawFirmContracts.StorageContracts; using LawFirmContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LawFirmBusinessLogic.BusinessLogics { public class BlankLogic : IBlankLogic { private readonly ILogger _logger; private readonly IBlankStorage _blankStorage; public BlankLogic (ILogger logger, IBlankStorage blankStorage) { _logger = logger; _blankStorage = blankStorage; } public bool Create(BlankBindingModel model) { CheckModel(model); if (_blankStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(BlankBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id:{Id}", model.Id); if (_blankStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public BlankViewModel? ReadElement(BlankSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. BlankName:{BlankName}.Id:{ Id}", model.BlankName, model.Id); var element = _blankStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); return element; } public List? ReadList(BlankSearchModel? model) { _logger.LogInformation("ReadList. BlankName:{BlankName}.Id:{ Id}", model?.BlankName, model?.Id); var list = model == null ? _blankStorage.GetFullList() : _blankStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public bool Update(BlankBindingModel model) { CheckModel(model); if (_blankStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(BlankBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.BlankName)) { throw new ArgumentNullException("Нет названия бланка", nameof(model.BlankName)); } if (model.Cost <= 0) { throw new ArgumentNullException("Цена бланка должна быть больше 0", nameof(model.Cost)); } _logger.LogInformation("Blank. BlankName:{BlankName}.Cost:{ Cost}. Id: { Id}", model.BlankName, model.Cost, model.Id); var element = _blankStorage.GetElement(new BlankSearchModel { BlankName = model.BlankName }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Бланк с таким названием уже есть"); } } } }