using DressAtelierContracts.BindingModels; using DressAtelierContracts.BusinessLogicContracts; using DressAtelierContracts.SearchModels; using DressAtelierContracts.StorageContracts; using DressAtelierContracts.ViewModels; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DressAtelierBusinessLogic.BusinessLogic { public class EmployeeLogic : IEmployeeLogic { private readonly ILogger _logger; private readonly IEmployeeStorage _employeeStorage; public EmployeeLogic(ILogger logger, IEmployeeStorage employeeStorage) { _logger = logger; _employeeStorage = employeeStorage; } public bool Create(EmployeeBindingModel model) { CheckEmployee(model); if(_employeeStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Update(EmployeeBindingModel model) { CheckEmployee(model); if (_employeeStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } public bool Delete(EmployeeBindingModel model) { CheckEmployee(model,false); _logger.LogInformation("Delete. ID:{ID}", model.ID); if (_employeeStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public EmployeeViewModel? ReadElement(EmployeeSearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } _logger.LogInformation("ReadElement. FullName:{FullName}.ID:{ ID}", model.FullName, model.ID); var element = _employeeStorage.GetElement(model); if (element == null) { _logger.LogWarning("ReadElement element not found"); return null; } _logger.LogInformation("ReadElement find. ID:{ID}", element.ID); return element; } public List? ReadList(EmployeeSearchModel? model) { _logger.LogInformation("ReadList. FullName:{FullName}. ID:{ ID}", model?.FullName, model?.ID); var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count:{Count}", list.Count); return list; } public void CheckEmployee(EmployeeBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (string.IsNullOrEmpty(model.FullName)) { throw new ArgumentNullException("Invalid fullname of employee", nameof(model.FullName)); } if (string.IsNullOrEmpty(model.Qualification.ToString())) { throw new ArgumentNullException("Invalid occupation of employee", nameof(model.Qualification)); } if (string.IsNullOrEmpty(model.WorkExperience.ToString())) { throw new ArgumentNullException("Invalid email of employee", nameof(model.WorkExperience)); } if (string.IsNullOrEmpty(model.Password)) { throw new ArgumentNullException("Invalid password of employee", nameof(model.Password)); } _logger.LogInformation("Employee. FullName:{ FullName}. WorkExperience:{ WorkExperience}. Qualification:{ Qualification}. ID: { ID} ", model.FullName, model.WorkExperience,model.Qualification, model.ID); var element = _employeeStorage.GetElement(new EmployeeSearchModel { FullName = model.FullName }); if (element != null && element.ID != model.ID) { throw new InvalidOperationException("Employee with such fullname already exists."); } } } }