using DeviceContracts.BindingModels; using DeviceContracts.BusinessLogicsContracts; using DeviceContracts.SearchModels; using DeviceContracts.StoragesContracts; using DeviceContracts.ViewModels; using Microsoft.Extensions.Logging; namespace DeviceBusinessLogic.BusinessLogics { public class KindLogic : IKindLogic { private readonly ILogger _logger; private readonly IKindStorage _kindStorage; public KindLogic(ILogger logger, IKindStorage kindStorage) { _logger = logger; _kindStorage = kindStorage; } public bool Create(KindBindingModel model) { CheckModel(model); if (_kindStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(KindBindingModel model) { CheckModel(model); if (_kindStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(KindBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_kindStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(KindSearchModel? model) { _logger.LogInformation( "ReadList. Id: {Id}, Title: {Title}, Frequency: " + "{Frequency}.", model?.Id, model?.Title, model?.Frequency); var list = model == null ? _kindStorage.GetFullList() : _kindStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public KindViewModel? ReadElement(KindSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation( "ReadElement. Id: {Id}, Title: {Title}, Frequency: " + "{Frequency}.", model?.Id, model?.Title, model?.Frequency); var element = _kindStorage.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(KindBindingModel 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)); } if (string.IsNullOrEmpty(model.Title)) { throw new ArgumentNullException( "Отсутствует название вида устройства", nameof(model.Title)); } if (model.Frequency > 0) { throw new ArgumentNullException( "Частота обслуживания должна быть больше 0 месяцев", nameof(model.Frequency)); } _logger.LogInformation( "Id: {Id}, Title: {Title}, Frequency: " + "{Frequency}.", model?.Id, model?.Title, model?.Frequency); var elementByTitle = _kindStorage.GetElement( new KindSearchModel { Title = model.Title, }); var elementByFrequency = _kindStorage.GetElement( new KindSearchModel { Frequency = model.Frequency, }); } } }