using DeviceContracts.BindingModels; using DeviceContracts.BusinessLogicsContracts; using DeviceContracts.SearchModels; using DeviceContracts.StoragesContracts; using DeviceContracts.ViewModels; using Microsoft.Extensions.Logging; namespace DeviceBusinessLogic.BusinessLogics { public class StaffLogic : IStaffLogic { private readonly ILogger _logger; private readonly IStaffStorage _staffStorage; public StaffLogic(ILogger logger, IStaffStorage staffStorage) { _logger = logger; _staffStorage = staffStorage; } public bool Create(StaffBindingModel model) { CheckModel(model); if (_staffStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(StaffBindingModel model) { CheckModel(model); if (_staffStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(StaffBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_staffStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public List? ReadList(StaffSearchModel? model) { _logger.LogInformation( "ReadList. Id: {Id}, FullName: {FullName}, Department: " + "{Department}, Email: {Email}, AccessLevel: {AccessLevel},", model?.Id, model?.FullName, model?.Department, model?.Email, model?.AccessLevel); var list = model == null ? _staffStorage.GetFullList() : _staffStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public StaffViewModel? ReadElement(StaffSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation( "ReadList. Id: {Id}, FullName: {FullName}, Department: " + "{Department}, Email: {Email}, AccessLevel: {AccessLevel},", model?.Id, model?.FullName, model?.Department, model?.Email, model?.AccessLevel); var element = _staffStorage.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(StaffBindingModel 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.FullName)) { throw new ArgumentNullException( "Отсутствует ФИО", nameof(model.FullName)); } if (string.IsNullOrEmpty(model.Department)) { throw new ArgumentNullException( "Отсутствует ФИО", nameof(model.Department)); } if (string.IsNullOrEmpty(model.Email)) { throw new ArgumentNullException( "Отсутствует ФИО", nameof(model.Email)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException( "Отсутствует ФИО", nameof(model.Password)); } _logger.LogInformation( "ReadList. Id: {Id}, FullName: {FullName}, Department: " + "{Department}, Email: {Email}, AccessLevel: {AccessLevel},", model?.Id, model?.FullName, model?.Department, model?.Email, model?.AccessLevel); var elementByFullName = _staffStorage.GetElement( new StaffSearchModel { FullName = model.FullName, }); var elementByDepartment = _staffStorage.GetElement( new StaffSearchModel { Department = model.Department, }); var elementByEmail = _staffStorage.GetElement( new StaffSearchModel { Email = model.Email, }); } } }