CourseWork_KPO/CandidateReviewBusinessLogic/BusinessLogic/AssessmentLogic.cs

117 lines
3.8 KiB
C#
Raw Permalink Normal View History

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 AssessmentLogic : IAssessmentLogic
{
private readonly ILogger _logger;
private readonly IAssessmentStorage _assessmentStorage;
public AssessmentLogic(ILogger<AssessmentLogic> logger, IAssessmentStorage assessmentStorage)
{
_logger = logger;
_assessmentStorage = assessmentStorage;
}
public bool Create(AssessmentBindingModel model)
{
CheckModel(model);
if (_assessmentStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AssessmentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_assessmentStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AssessmentViewModel? ReadElement(AssessmentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _assessmentStorage.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<AssessmentViewModel>? ReadList(AssessmentSearchModel? model)
{
var list = model == null ? _assessmentStorage.GetFullList() : _assessmentStorage.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(AssessmentBindingModel model)
{
CheckModel(model);
if (_assessmentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(AssessmentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.ResumeId <= 0)
{
throw new ArgumentException("Нет идентификатора резюме", nameof(model.ResumeId));
}
if (model.UserId <= 0)
{
throw new ArgumentException("Нет идентификатора пользователя", nameof(model.UserId));
}
var element = _assessmentStorage.GetElement(new AssessmentSearchModel
{
ResumeId = model.ResumeId,
UserId = model.UserId
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Резюме уже было оценено");
}
}
}
}