CourseWork_KPO/CandidateReviewBusinessLogic/BusinessLogic/AssessmentLogic.cs

142 lines
4.8 KiB
C#
Raw Normal View History

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);
List<AssessmentViewModel> result = new();
foreach (var elem in list)
{
var user = _userStorage.GetElement(new UserSearchModel { Id = elem.UserId });
var assessmentCriterions = _assessmentStorage.GetAssessmentCriterions(elem.Id);
var assessmentViewModel = new AssessmentViewModel
{
Id = elem.Id,
ResumeId = elem.ResumeId,
UserId = elem.UserId,
UserName = user.Name + " " + user.Name,
Comment = elem.Comment,
AssessmentCriterions = assessmentCriterions.Select(ac => new AssessmentCriterionViewModel
{
AssessmentId = ac.AssessmentId,
CriterionId = ac.CriterionId,
Value = ac.Value,
CriterionName = _criterionStorage.GetElement(new CriterionSearchModel { Id = ac.CriterionId })?.Name
}).ToList()
};
result.Add(assessmentViewModel);
}
if (result == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", result.Count);
return result;
}
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("Резюме уже было оценено");
}
}
}
}