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;
|
|
|
|
|
public CompanyLogic(ILogger<CompanyLogic> logger, ICompanyStorage сompanyStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_сompanyStorage = сompanyStorage;
|
|
|
|
|
}
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CompanyViewModel? ReadElement(CompanySearchModel model)
|
|
|
|
|
{
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("Компания с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|