using DeviceContracts.BindingModels; using DeviceContracts.BusinessLogicsContracts; using DeviceContracts.SearchModels; using DeviceContracts.StoragesContracts; using DeviceContracts.ViewModels; using DeviceDataModels; using Microsoft.Extensions.Logging; namespace DeviceBusinessLogic.BusinessLogics { public class DeviceLogic : IDeviceLogic { private readonly ILogger _logger; private readonly IDeviceStorage _deviceStorage; public DeviceLogic(ILogger logger, IDeviceStorage deviceStorage) { _logger = logger; _deviceStorage = deviceStorage; } public bool Create(DeviceBindingModel model) { CheckModel(model); if (_deviceStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(DeviceBindingModel model) { CheckModel(model); if (_deviceStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(DeviceBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_deviceStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(DeviceSearchModel? model) { _logger.LogInformation( "ReadList. Id: {Id}, Model: {Model}, SerialNumber: " + "{SerialNumber}, ProductionDate: {ProductionDate}, " + "WarrantyPeriod: {WarrantyPeriod}, Condition: {Condition}, " + "KindId: {KindId}, KitId: {KitId}", model?.Id, model?.Model, model?.SerialNumber, model?.ProductionDate, model?.WarrantyPeriod, model?.Condition, model?.KindId, model?.KitId); var list = model == null ? _deviceStorage.GetFullList() : _deviceStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public DeviceViewModel? ReadElement(DeviceSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation( "ReadElement. Id: {Id}, Model: {Model}, SerialNumber: " + "{SerialNumber}, ProductionDate: {ProductionDate}, " + "WarrantyPeriod: {WarrantyPeriod}, Condition: {Condition}, " + "KindId: {KindId}, KitId: {KitId}", model?.Id, model?.Model, model?.SerialNumber, model?.ProductionDate, model?.WarrantyPeriod, model?.Condition, model?.KindId, model?.KitId); var element = _deviceStorage.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(DeviceBindingModel 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.Model)) { throw new ArgumentNullException( "Отсутствует название модели", nameof(model.Model)); } if ((model.Condition != true) && (model.Condition != false)) { throw new ArgumentNullException( "Отсутствует показатель работоспособности", nameof(model.Model)); } if (model.KindId <= 0) { throw new ArgumentNullException( "Идентификатор вида должен быть больше 0", nameof(model.Id)); } _logger.LogInformation( "ReadElement. Id: {Id}, Model: {Model}, SerialNumber: " + "{SerialNumber}, ProductionDate: {ProductionDate}, " + "WarrantyPeriod: {WarrantyPeriod}, Condition: {Condition}, " + "KindId: {KindId}, KitId: {KitId}", model?.Id, model?.Model, model?.SerialNumber, model?.ProductionDate, model?.WarrantyPeriod, model?.Condition, model?.KindId, model?.KitId); var elementByModel = _deviceStorage.GetElement( new DeviceSearchModel { Model = model.Model, }); var elementBySerialNumber = _deviceStorage.GetElement( new DeviceSearchModel { SerialNumber = model.SerialNumber, }); var elementByProductionDate = _deviceStorage.GetElement( new DeviceSearchModel { ProductionDate = model.ProductionDate, }); var elementByWarrantyPeriod = _deviceStorage.GetElement( new DeviceSearchModel { WarrantyPeriod = model.WarrantyPeriod, }); var elementByCondition = _deviceStorage.GetElement( new DeviceSearchModel { Condition = model.Condition, }); var elementByKindId = _deviceStorage.GetElement( new DeviceSearchModel { KindId = model.KindId, }); var elementByKitId = _deviceStorage.GetElement( new DeviceSearchModel { KitId = model.KitId, }); } } }