forked from DavidMakarov/StudentEnrollment
120 lines
3.7 KiB
C#
120 lines
3.7 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 StudentLogic : IStudentLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IStudentStorage _studentStorage;
|
||
public StudentLogic(ILogger<StudentLogic> logger, IStudentStorage studentStorage)
|
||
{
|
||
_logger = logger;
|
||
_studentStorage = studentStorage;
|
||
}
|
||
public List<StudentViewModel>? ReadList(StudentSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. TIN: {TIN}. Id:{Id}", model?.tin, model?.student_id);
|
||
var list = model == null ? _studentStorage.GetFullList() : _studentStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public StudentViewModel? ReadElement(StudentSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadList. TIN: {TIN}. Id:{Id}", model?.tin, model?.student_id);
|
||
var element = _studentStorage.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(StudentBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_studentStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(StudentBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_studentStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(StudentBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_studentStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(StudentBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.LastName))
|
||
{
|
||
throw new ArgumentNullException("Нет фамилии студента!", nameof(model.LastName));
|
||
}
|
||
if (string.IsNullOrEmpty(model.FirstName))
|
||
{
|
||
throw new ArgumentNullException("Нет имени студента!", nameof(model.FirstName));
|
||
}
|
||
if (model.TIN == 0)
|
||
{
|
||
throw new ArgumentNullException("Нет ИНН студента!", nameof(model.TIN));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Email))
|
||
{
|
||
throw new ArgumentNullException("Нет почты студента!", nameof(model.Email));
|
||
}
|
||
if (model.StudentCourse.Count == 0)
|
||
{
|
||
throw new ArgumentNullException("У студента должны быть баллы за экзамены!", nameof(model.StudentCourse));
|
||
}
|
||
_logger.LogInformation("Student. LastName: {LastName}, FirstName: {FirstName}, MiddleName: {MiddleName}, TIN: {TIN}, Email: {Email}, Id: {Id}", model.LastName, model.FirstName, model.MiddleName, model.TIN, model.Email, model.Id);
|
||
var element = _studentStorage.GetElement(new StudentSearchModel
|
||
{
|
||
tin = model.TIN
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Студент с таким ИНН уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|