forked from DavidMakarov/StudentEnrollment
107 lines
3.0 KiB
C#
107 lines
3.0 KiB
C#
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?.name, model?.course_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.name, model.course_id);
|
||
var element = _courseStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
_logger.LogWarning("ReadElement element not found");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadElement find. Id:{Id}", element.course_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.course_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.name))
|
||
{
|
||
throw new ArgumentNullException("Нет названия направления!",
|
||
nameof(model.name));
|
||
}
|
||
_logger.LogInformation("Course. CourseName:{CourseName}. Id: { Id}", model.name, model.course_id);
|
||
var element = _courseStorage.GetElement(new CourseSearchModel
|
||
{
|
||
name = model.name
|
||
});
|
||
if (element != null && element.course_id != model.course_id)
|
||
{
|
||
throw new InvalidOperationException("Направление с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|