PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/StudentLogic.cs
2023-05-17 16:47:38 +04:00

117 lines
3.6 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 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;
}
public int GetNumberOfPages(int userId, int pageSize = 10)
{
return _studentStorage.GetNumberOfPages(userId, pageSize);
}
private void CheckModel(StudentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени студента", nameof(model.Name));
}
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)
{
throw new ArgumentNullException("Неверно указан номер студенческого билета", nameof(model.Name));
}
var student = _studentStorage.GetElement(new StudentSearchModel
{
StudentCard = model.StudentCard,
});
if (student != null && student.Id != model.Id)
{
throw new InvalidOperationException("Студент с таким номером студенческого билета уже есть");
}
}
}
}