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

65 lines
2.1 KiB
C#
Raw Normal View History

2023-10-28 09:06:35 +04:00
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;
2023-11-09 04:07:06 +04:00
public StudentLogic(IStudentStorage studentStorage)
2023-10-28 09:06:35 +04:00
{
_studentStorage = studentStorage;
}
2023-11-09 04:07:06 +04:00
public void CreateOrUpdate(StudentBindingModel model)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
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)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
throw new Exception("Студент с таким именем уже существует.");
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
if (model.Id.HasValue)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
_studentStorage.Update(model);
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
else
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
_studentStorage.Insert(model);
2023-10-28 09:06:35 +04:00
}
}
2023-11-09 04:07:06 +04:00
public void Delete(StudentBindingModel model)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
var element = _studentStorage.GetElement(new StudentBindingModel { Id = model.Id });
if (element == null)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
throw new Exception("Студент не найден");
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
_studentStorage.Delete(model);
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
public List<StudentViewModel> Read(StudentBindingModel model)
2023-10-28 09:06:35 +04:00
{
if (model == null)
{
2023-11-09 04:07:06 +04:00
return _studentStorage.GetFullList();
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
if (model.Id.HasValue)
2023-10-28 09:06:35 +04:00
{
2023-11-09 04:07:06 +04:00
return new List<StudentViewModel> { _studentStorage.GetElement(model) };
2023-10-28 09:06:35 +04:00
}
2023-11-09 04:07:06 +04:00
return _studentStorage.GetFilteredList(model);
2023-10-28 09:06:35 +04:00
}
}
}