PIbd-32_Artamonova_T.V._COP_2/UniversityBusinessLogic/BusinessLogics/StudentLogic.cs

65 lines
2.1 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 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);
}
}
}