using DeviceContracts.BindingModels; using DeviceContracts.BusinessLogicsContracts; using DeviceContracts.SearchModels; using DeviceContracts.StoragesContracts; using DeviceContracts.ViewModels; using Microsoft.Extensions.Logging; namespace DeviceBusinessLogic.BusinessLogics { public class CabinetLogic : ICabinetLogic { private readonly ILogger _logger; private readonly ICabinetStorage _cabinetStorage; public CabinetLogic(ILogger logger, ICabinetStorage cabinetStorage) { _logger = logger; _cabinetStorage = cabinetStorage; } public bool Create(CabinetBindingModel model) { CheckModel(model); if (_cabinetStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(CabinetBindingModel model) { CheckModel(model); if (_cabinetStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(CabinetBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_cabinetStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(CabinetSearchModel? model) { _logger.LogInformation( "ReadList. Id: {Id}, Room: {Room}, Building: " + "{Building}.", model?.Id, model?.Room, model?.Building); var list = model == null ? _cabinetStorage.GetFullList() : _cabinetStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public CabinetViewModel? ReadElement(CabinetSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation( "ReadElemnt. Id: {Id}, Room: {Room}, Building: " + "{Building}.", model?.Id, model?.Room, model?.Building); var element = _cabinetStorage.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(CabinetBindingModel 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.Room)) { throw new ArgumentNullException( "Отсутствует номер/название кабинета", nameof(model.Room)); } if (model.Building < 1) { throw new ArgumentNullException( "Номер корпуса должен быть больше 0", nameof(model.Id)); } _logger.LogInformation( "Id: {Id}, Room: {Room}, Building: " + "{Building}.", model?.Id, model?.Room, model?.Building); var elementByRoom = _cabinetStorage.GetElement( new CabinetSearchModel { Room = model.Room, }); var elementByBuilding = _cabinetStorage.GetElement( new CabinetSearchModel { Building = model.Building, }); } } }