2024-11-06 01:27:34 +04:00
|
|
|
|
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 CompanyLogic : ICompanyLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly ICompanyStorage _сompanyStorage;
|
2024-11-19 16:45:59 +04:00
|
|
|
|
private readonly IVacancyStorage _vacancyStorage;
|
|
|
|
|
public CompanyLogic(ILogger<CompanyLogic> logger, ICompanyStorage сompanyStorage, IVacancyStorage vacancyStorage)
|
2024-11-06 01:27:34 +04:00
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_сompanyStorage = сompanyStorage;
|
2024-11-19 16:45:59 +04:00
|
|
|
|
_vacancyStorage = vacancyStorage;
|
2024-11-06 01:27:34 +04:00
|
|
|
|
}
|
2024-11-18 23:37:50 +04:00
|
|
|
|
public int Create(CompanyBindingModel model)
|
2024-11-06 01:27:34 +04:00
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_сompanyStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
2024-11-18 23:37:50 +04:00
|
|
|
|
return 0;
|
2024-11-06 01:27:34 +04:00
|
|
|
|
}
|
2024-11-18 23:37:50 +04:00
|
|
|
|
return model.Id;
|
2024-11-06 01:27:34 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(CompanyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Id: {Id}", model.Id);
|
|
|
|
|
if (_сompanyStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 16:45:59 +04:00
|
|
|
|
/*public CompanyViewModel? ReadElement(CompanySearchModel model)
|
2024-11-06 01:27:34 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
var element = _сompanyStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
|
|
|
|
|
return element;
|
2024-11-19 16:45:59 +04:00
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
public CompanyViewModel? ReadElement(CompanySearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
var company = _сompanyStorage.GetElement(model);
|
|
|
|
|
if (company == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement: Company not found for Id: {Id}", model.Id);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var vacancies = _vacancyStorage.GetFilteredList(new VacancySearchModel { CompanyId = company.Id});
|
|
|
|
|
|
|
|
|
|
var vacancyViewModels = vacancies?.Select(v => new VacancyViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = v.Id,
|
|
|
|
|
CompanyId = v.CompanyId,
|
|
|
|
|
JobTitle = v.JobTitle,
|
|
|
|
|
Requirements = v.Requirements,
|
|
|
|
|
Responsibilities = v.Responsibilities,
|
|
|
|
|
JobType = v.JobType,
|
|
|
|
|
Salary = v.Salary,
|
|
|
|
|
Description = v.Description,
|
|
|
|
|
Status = v.Status,
|
|
|
|
|
CreatedAt = v.CreatedAt,
|
|
|
|
|
Tags = v.Tags
|
|
|
|
|
}).ToList() ?? new List<VacancyViewModel>();
|
|
|
|
|
|
|
|
|
|
var companyViewModel = new CompanyViewModel
|
|
|
|
|
{
|
|
|
|
|
Id = company.Id,
|
|
|
|
|
Name = company.Name,
|
|
|
|
|
Description = company.Description,
|
|
|
|
|
Contacts = company.Contacts,
|
|
|
|
|
Address = company.Address,
|
|
|
|
|
Website = company.Website,
|
|
|
|
|
LogoFilePath = company.LogoFilePath,
|
|
|
|
|
Vacancies = vacancyViewModels
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement: Company found. Id: {Id}", company.Id);
|
|
|
|
|
return companyViewModel;
|
2024-11-06 01:27:34 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-19 16:45:59 +04:00
|
|
|
|
|
|
|
|
|
|
2024-11-06 01:27:34 +04:00
|
|
|
|
public List<CompanyViewModel>? ReadList(CompanySearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
var list = model == null ? _сompanyStorage.GetFullList() : _сompanyStorage.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(CompanyBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_сompanyStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(CompanyBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия компании", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var element = _сompanyStorage.GetElement(new CompanySearchModel
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Компания с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|