using DeviceContracts.BindingModels; using DeviceContracts.BusinessLogicsContracts; using DeviceContracts.SearchModels; using DeviceContracts.StoragesContracts; using DeviceContracts.ViewModels; using Microsoft.Extensions.Logging; namespace DeviceBusinessLogic.BusinessLogics { public class KitLogic : IKitLogic { private readonly ILogger _logger; private readonly IKitStorage _kitStorage; public KitLogic(ILogger logger, IKitStorage kitStorage) { _logger = logger; _kitStorage = kitStorage; } public bool Create(KitBindingModel model) { CheckModel(model); if (_kitStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(KitBindingModel model) { CheckModel(model); if (_kitStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(KitBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_kitStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(KitSearchModel? model) { _logger.LogInformation( "ReadList. Id: {Id}, Title: {Title}, CabinetId: " + "{CabinetId}.", model?.Id, model?.Title, model?.CabinetId); var list = model == null ? _kitStorage.GetFullList() : _kitStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public KitViewModel? ReadElement(KitSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation( "ReadElement. Id: {Id}, Title: {Title}, CabinetId: " + "{CabinetId}.", model?.Id, model?.Title, model?.CabinetId); var element = _kitStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); return element; } private void CheckModel(KitBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.Id <= 0) { throw new ArgumentNullException( "Идентификатор должен быть больше 0", nameof(model.Id)); } if (string.IsNullOrEmpty(model.Title)) { throw new ArgumentNullException( "Отсутствует номер/название кабинета", nameof(model.Title)); } _logger.LogInformation( "Id: {Id}, Title: {Title}, CabinetId: " + "{CabinetId}.", model?.Id, model?.Title, model?.CabinetId); var elementByTitle = _kitStorage.GetElement( new KitSearchModel { Title = model.Title, }); } } }