2023-04-07 17:34:20 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UniversityContracts.BindingModels;
|
|
|
|
|
using UniversityContracts.BusinessLogicContracts;
|
|
|
|
|
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 bool Create(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_studentStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Update(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if (_studentStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public bool Delete(StudentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if (_studentStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
public StudentViewModel? ReadElement(StudentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
var student = _studentStorage.GetElement(model);
|
|
|
|
|
if (student == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return student;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<StudentViewModel>? ReadList(StudentSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
var list = model == null ? _studentStorage.GetFullList() : _studentStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 16:47:38 +04:00
|
|
|
|
public int GetNumberOfPages(int userId, int pageSize = 10)
|
|
|
|
|
{
|
|
|
|
|
return _studentStorage.GetNumberOfPages(userId, pageSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(StudentBindingModel model, bool withParams = true)
|
2023-04-07 17:34:20 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
2023-05-17 16:47:38 +04:00
|
|
|
|
throw new ArgumentNullException("Нет имени студента", nameof(model.Name));
|
2023-04-07 17:34:20 +04:00
|
|
|
|
}
|
2023-05-17 16:47:38 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет фамилии студента", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
if (model.DateOfBirth > DateTime.UtcNow)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Неверно указана дата рождения студента", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
if (model.StudentCard <= 0)
|
2023-04-07 17:34:20 +04:00
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Неверно указан номер студенческого билета", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var student = _studentStorage.GetElement(new StudentSearchModel
|
|
|
|
|
{
|
|
|
|
|
StudentCard = model.StudentCard,
|
|
|
|
|
});
|
|
|
|
|
if (student != null && student.Id != model.Id)
|
|
|
|
|
{
|
2023-05-17 16:47:38 +04:00
|
|
|
|
throw new InvalidOperationException("Студент с таким номером студенческого билета уже есть");
|
2023-04-07 17:34:20 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|