PIbd-22_Karamushko_M_K_Univ.../University/UniversityBuisnessLogic/ExaminationResultLogic.cs
2023-04-05 20:24:16 +04:00

104 lines
3.6 KiB
C#

using UniversityContracts.BindingModels;
using UniversityContracts.BuisnessLogicContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace UniversityBuisnessLogic
{
public class ExaminationResultLogic : IExaminationResultLogic
{
private readonly ILogger _logger;
private readonly IExaminationResultStorage _examinationResultStorage;
public ExaminationResultLogic(ILogger<ExaminationResultLogic> logger, IExaminationResultStorage examinationResultStorage)
{
_logger = logger;
_examinationResultStorage = examinationResultStorage;
}
public bool Create(ExaminationResultBindingModel model)
{
CheckModel(model);
if (_examinationResultStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ExaminationResultBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_examinationResultStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ExaminationResultViewModel? ReadElement(ExaminationResultSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _examinationResultStorage.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<ExaminationResultViewModel>? ReadList(ExaminationResultSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = model == null ? _examinationResultStorage.GetFullList() : _examinationResultStorage.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(ExaminationResultBindingModel model)
{
CheckModel(model);
if (_examinationResultStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ExaminationResultBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ExaminationForm))
{
throw new ArgumentNullException("Нет формы испытания", nameof(model.ExaminationForm));
}
_logger.LogInformation("ExaminationResult. ExaminationForm: {ExaminationForm}. Id: {Id}", model.ExaminationForm, model.Id);
}
}
}