CourseWork_KPO/CandidateReviewBusinessLogic/BusinessLogic/CriterionLogic.cs

111 lines
3.6 KiB
C#
Raw 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 CriterionLogic : ICriterionLogic
{
private readonly ILogger _logger;
private readonly ICriterionStorage _criterionStorage;
public CriterionLogic(ILogger<CriterionLogic> logger, ICriterionStorage criterionStorage)
{
_logger = logger;
_criterionStorage = criterionStorage;
}
public bool Create(CriterionBindingModel model)
{
CheckModel(model);
if (_criterionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(CriterionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_criterionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CriterionViewModel? ReadElement(CriterionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _criterionStorage.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<CriterionViewModel>? ReadList(CriterionSearchModel? model)
{
var list = model == null ? _criterionStorage.GetFullList() : _criterionStorage.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(CriterionBindingModel model)
{
CheckModel(model);
if (_criterionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CriterionBindingModel 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));
}
if (model.Weight <= 0)
{
throw new ArgumentException("Некорректный вес критерия оценивания", nameof(model.Weight));
}
if (string.IsNullOrEmpty(model.Type.ToString()))
{
throw new ArgumentNullException("Нет типа критерия оценивания", nameof(model.Type));
}
}
}
}