65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using Microsoft.Extensions.Logging;
|
||
using UniversityContracts.BindingModels;
|
||
using UniversityContracts.BusinessLogicsContracts;
|
||
using UniversityContracts.SearchModels;
|
||
using UniversityContracts.StoragesContracts;
|
||
using UniversityContracts.ViewModels;
|
||
|
||
namespace UniversityBusinessLogic.BusinessLogics
|
||
{
|
||
public class StudentLogic : IStudentLogic
|
||
{
|
||
private readonly IStudentStorage _studentStorage;
|
||
public StudentLogic(IStudentStorage studentStorage)
|
||
{
|
||
_studentStorage = studentStorage;
|
||
}
|
||
public void CreateOrUpdate(StudentBindingModel model)
|
||
{
|
||
var element = _studentStorage.GetElement(
|
||
new StudentBindingModel
|
||
{
|
||
FIO = model.FIO,
|
||
Email = model.Email,
|
||
PhotoFilePath = model.PhotoFilePath,
|
||
DirectionName = model.DirectionName,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new Exception("Студент с таким именем уже существует.");
|
||
}
|
||
if (model.Id.HasValue)
|
||
{
|
||
_studentStorage.Update(model);
|
||
}
|
||
else
|
||
{
|
||
_studentStorage.Insert(model);
|
||
}
|
||
}
|
||
|
||
public void Delete(StudentBindingModel model)
|
||
{
|
||
var element = _studentStorage.GetElement(new StudentBindingModel { Id = model.Id });
|
||
if (element == null)
|
||
{
|
||
throw new Exception("Студент не найден");
|
||
}
|
||
_studentStorage.Delete(model);
|
||
}
|
||
|
||
public List<StudentViewModel> Read(StudentBindingModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
return _studentStorage.GetFullList();
|
||
}
|
||
if (model.Id.HasValue)
|
||
{
|
||
return new List<StudentViewModel> { _studentStorage.GetElement(model) };
|
||
}
|
||
return _studentStorage.GetFilteredList(model);
|
||
}
|
||
}
|
||
}
|