PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/StudentLogic.cs

104 lines
3.1 KiB
C#
Raw Permalink 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;
}
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 (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("Приказ с таким названием уже есть");
}
}
}
}