forked from DavidMakarov/StudentEnrollment
130 lines
3.8 KiB
C#
130 lines
3.8 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using StudentEnrollmentContracts.BindingModels;
|
|
using StudentEnrollmentContracts.BusinessLogicContracts;
|
|
using StudentEnrollmentContracts.SearchModels;
|
|
using StudentEnrollmentContracts.StorageContracts;
|
|
using StudentEnrollmentContracts.ViewModels;
|
|
using StudentEnrollmentDataModels.Models;
|
|
|
|
namespace StudentEnrollmentBusinessLogic
|
|
{
|
|
public class ExamPointsLogic : IExamPointsLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IExamPointsStorage _examPointsStorage;
|
|
public ExamPointsLogic(ILogger<ExamPointsLogic> logger, IExamPointsStorage
|
|
examPointsStorage)
|
|
{
|
|
_logger = logger;
|
|
_examPointsStorage = examPointsStorage;
|
|
}
|
|
public List<ExamPointsViewModel>? ReadList(ExamPointsSearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. Id:{ Id}", model?.exampoints_id);
|
|
var list = model == null ? _examPointsStorage.GetFullList() :
|
|
_examPointsStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
public ExamPointsViewModel? ReadElement(ExamPointsSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
_logger.LogInformation("ReadElement .Id:{ Id}", model.exampoints_id);
|
|
var element = _examPointsStorage.GetElement(model);
|
|
if (element == null)
|
|
{
|
|
_logger.LogWarning("ReadElement element not found");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
|
|
return element;
|
|
}
|
|
public bool Create(ExamPointsBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_examPointsStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public IExamPointsModel Create(ExamPointsBindingModel model, bool returnTypeIsModel)
|
|
{
|
|
if (returnTypeIsModel)
|
|
{
|
|
CheckModel(model);
|
|
if (_examPointsStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return null;
|
|
}
|
|
var list = _examPointsStorage.GetFullList();
|
|
return list[list.Count - 1];
|
|
}
|
|
return null;
|
|
}
|
|
public bool Update(ExamPointsBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_examPointsStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public bool Delete(ExamPointsBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
if (_examPointsStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
private void CheckModel(ExamPointsBindingModel model, bool withParams = true)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (!withParams)
|
|
{
|
|
return;
|
|
}
|
|
if (model.FirstExamPoints == 0)
|
|
{
|
|
throw new ArgumentNullException("Нет баллов за первый экзамен",
|
|
nameof(model.FirstExamPoints));
|
|
}
|
|
if (model.SecondExamPoints == 0)
|
|
{
|
|
throw new ArgumentNullException("Нет баллов за второй экзамен",
|
|
nameof(model.SecondExamPoints));
|
|
}
|
|
if (model.ThirdExamPoints == 0)
|
|
{
|
|
throw new ArgumentNullException("Нет баллов за третий экзамен",
|
|
nameof(model.ThirdExamPoints));
|
|
}
|
|
_logger.LogInformation("ExamPoints. FirstExamPoints: {FirstExamPoints}. " +
|
|
"SecondExamPoints: {SecondExamPoints}. ThirdExamPoints: {ThirdExamPoints}. AddPoints: {AddPoints}. Summary: {Summary}" +
|
|
"Id: { Id}",
|
|
model.FirstExamPoints, model.SecondExamPoints, model.ThirdExamPoints,
|
|
model.AddPoints, model.Summary, model.Id);
|
|
}
|
|
}
|
|
}
|