StudentEnrollmentNew/StudentEnrollment/StudentEnrollmentBusinessLogic/CourseLogic.cs

107 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using StudentEnrollmentContracts.BindingModels;
using StudentEnrollmentContracts.BusinessLogicContracts;
using StudentEnrollmentContracts.SearchModels;
using StudentEnrollmentContracts.StorageContracts;
using StudentEnrollmentContracts.ViewModels;
namespace StudentEnrollmentBusinessLogic
{
public class CourseLogic : ICourseLogic
{
private readonly ILogger _logger;
private readonly ICourseStorage _courseStorage;
public CourseLogic(ILogger<CourseLogic> logger, ICourseStorage
courseStorage)
{
_logger = logger;
_courseStorage = courseStorage;
}
public List<CourseViewModel>? ReadList(CourseSearchModel? model)
{
_logger.LogInformation("ReadList. CourseName:{CourseName}.Id:{ Id}", model?.CourseName, model?.Id);
var list = model == null ? _courseStorage.GetFullList() :
_courseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CourseViewModel? ReadElement(CourseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CourseName:{CourseName}.Id:{ Id}", model.CourseName, model.Id);
var element = _courseStorage.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(CourseBindingModel model)
{
CheckModel(model);
if (_courseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CourseBindingModel model)
{
CheckModel(model);
if (_courseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CourseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_courseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CourseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.CourseName))
{
throw new ArgumentNullException("Нет названия направления!",
nameof(model.CourseName));
}
_logger.LogInformation("Course. CourseName:{CourseName}. Id: { Id}", model.CourseName, model.Id);
var element = _courseStorage.GetElement(new CourseSearchModel
{
CourseName = model.CourseName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Направление с таким названием уже есть");
}
}
}
}