2024-04-16 20:15:07 +04:00
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-06 20:26:16 +04:00
|
|
|
|
_logger.LogInformation("ReadList. CourseName:{CourseName}.Id:{ Id}", model?.name, model?.course_id);
|
2024-04-16 20:15:07 +04:00
|
|
|
|
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));
|
|
|
|
|
}
|
2024-05-06 20:26:16 +04:00
|
|
|
|
_logger.LogInformation("ReadElement. CourseName:{CourseName}.Id:{ Id}", model.name, model.course_id);
|
2024-04-16 20:15:07 +04:00
|
|
|
|
var element = _courseStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogInformation("ReadElement find. Id:{Id}", element.course_id);
|
2024-04-16 20:15:07 +04:00
|
|
|
|
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);
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogInformation("Delete. Id:{Id}", model.course_id);
|
2024-04-16 20:15:07 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
2024-05-07 23:34:50 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.name))
|
2024-04-16 20:15:07 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия направления!",
|
2024-05-07 23:34:50 +04:00
|
|
|
|
nameof(model.name));
|
2024-04-16 20:15:07 +04:00
|
|
|
|
}
|
2024-05-07 23:34:50 +04:00
|
|
|
|
_logger.LogInformation("Course. CourseName:{CourseName}. Id: { Id}", model.name, model.course_id);
|
2024-04-16 20:15:07 +04:00
|
|
|
|
var element = _courseStorage.GetElement(new CourseSearchModel
|
|
|
|
|
{
|
2024-05-07 23:34:50 +04:00
|
|
|
|
name = model.name
|
2024-04-16 20:15:07 +04:00
|
|
|
|
});
|
2024-05-07 23:34:50 +04:00
|
|
|
|
if (element != null && element.course_id != model.course_id)
|
2024-04-16 20:15:07 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Направление с таким названием уже есть");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|