using CandidateReviewContracts.BindingModels; using CandidateReviewContracts.BusinessLogicsContracts; using CandidateReviewContracts.SearchModels; using CandidateReviewContracts.StoragesContracts; using CandidateReviewContracts.ViewModels; using Microsoft.Extensions.Logging; using System.Text.RegularExpressions; namespace CandidateReviewBusinessLogic.BusinessLogic { public class VacancyLogic : IVacancyLogic { private readonly ILogger _logger; private readonly IVacancyStorage _vacancyStorage; public VacancyLogic(ILogger logger, IVacancyStorage vacancyStorage) { _logger = logger; _vacancyStorage = vacancyStorage; } public bool Create(VacancyBindingModel model) { CheckModel(model); if (_vacancyStorage.Insert(model) == null) { _logger.LogWarning("Insert operation failed"); return false; } return true; } public bool Delete(VacancyBindingModel model) { CheckModel(model, false); _logger.LogInformation("Delete. Id: {Id}", model.Id); if (_vacancyStorage.Delete(model) == null) { _logger.LogWarning("Delete operation failed"); return false; } return true; } public VacancyViewModel? ReadElement(VacancySearchModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _vacancyStorage.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(VacancySearchModel? model) { var list = model == null ? _vacancyStorage.GetFullList() : _vacancyStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); return null; } _logger.LogInformation("ReadList. Count: {Count}", list.Count); return list; } public bool Update(VacancyBindingModel model) { CheckModel(model); if (_vacancyStorage.Update(model) == null) { _logger.LogWarning("Update operation failed"); return false; } return true; } private void CheckModel(VacancyBindingModel model, bool withParams = true) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (!withParams) { return; } if (model.CompanyId <= 0) { throw new ArgumentException("Некорректный идентификатор компании", nameof(model.CompanyId)); } if (string.IsNullOrEmpty(model.JobTitle)) { throw new ArgumentNullException("Нет названия вакансии", nameof(model.JobTitle)); } if (string.IsNullOrEmpty(model.Requirements)) { throw new ArgumentNullException("Нет требований к вакансии", nameof(model.Requirements)); } if (string.IsNullOrEmpty(model.Responsibilities)) { throw new ArgumentNullException("Нет обязанностей вакансии", nameof(model.Responsibilities)); } if (string.IsNullOrEmpty(model.JobType.ToString())) { throw new ArgumentNullException("Нет типа работы", nameof(model.JobType)); } if (string.IsNullOrEmpty(model.CreatedAt.ToString())) { throw new ArgumentNullException("Нет даты создания вакансии", nameof(model.CreatedAt)); } if (string.IsNullOrEmpty(model.Status.ToString())) { throw new ArgumentNullException("Нет статуса вакансии", nameof(model.Status)); } var element = _vacancyStorage.GetElement(new VacancySearchModel { CompanyId = model.CompanyId, JobTitle = model.JobTitle, Salary = model.Salary, }); if (element != null && element.Id != model.Id) { throw new InvalidOperationException("Такая вакансия уже существует"); } } } }