CourseWork_KPO/CandidateReviewBusinessLogic/BusinessLogic/AssessmentLogic.cs
2024-12-13 04:12:54 +04:00

117 lines
3.7 KiB
C#

using CandidateReviewContracts.BindingModels;
using CandidateReviewContracts.BusinessLogicsContracts;
using CandidateReviewContracts.SearchModels;
using CandidateReviewContracts.StoragesContracts;
using CandidateReviewContracts.ViewModels;
using CandidateReviewDataModels.Models;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
namespace CandidateReviewBusinessLogic.BusinessLogic
{
public class AssessmentLogic : IAssessmentLogic
{
private readonly ILogger _logger;
private readonly IAssessmentStorage _assessmentStorage;
private readonly IUserStorage _userStorage;
private readonly ICriterionStorage _criterionStorage;
public AssessmentLogic(ILogger<AssessmentLogic> logger, IAssessmentStorage assessmentStorage, IUserStorage userStorage, ICriterionStorage criterionStorage)
{
_logger = logger;
_assessmentStorage = assessmentStorage;
_userStorage = userStorage;
_criterionStorage = criterionStorage;
}
public int? Create(AssessmentBindingModel model)
{
CheckModel(model);
var id = _assessmentStorage.Insert(model);
if (id == null)
{
_logger.LogWarning("Insert operation failed");
return 0;
}
return id;
}
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;
}
var element = _assessmentStorage.GetElement(new AssessmentSearchModel
{
ResumeId = model.ResumeId,
UserId = model.UserId
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Резюме уже было оценено");
}
}
}
}