111 lines
3.5 KiB
C#
111 lines
3.5 KiB
C#
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;
|
||
}
|
||
public bool Create(CompanyBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_сompanyStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
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("Компания с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|