113 lines
2.6 KiB
C#
Raw Normal View History

2024-04-08 14:40:49 +04:00
using SchoolScheduleContracts.BindingModels;
using SchoolScheduleContracts.BusinessLogicsContracts;
using SchoolScheduleContracts.SearchModels;
using SchoolScheduleContracts.StoragesContracts;
using SchoolScheduleContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolScheduleBusinessLogic
{
public class GradeLogic : IGradeLogic
{
private readonly IGradeStorage _gradeStorage;
private readonly IStudentStorage _studentStorage;
public GradeLogic(IGradeStorage gradeStorage, IStudentStorage studentStorage)
{
_gradeStorage = gradeStorage;
_studentStorage = studentStorage;
}
public List<GradeViewModel> ReadList(GradeSearchModel? model)
{
var list = model == null ? _gradeStorage.GetFullList() :
_gradeStorage.GetFilteredList(model);
return list;
}
public GradeViewModel? ReadElement(GradeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _gradeStorage.GetElement(model);
return element;
}
public List<StudentViewModel> GetStudents(GradeSearchModel model)
{
var list = _studentStorage.GetFilteredList(new StudentSearchModel { GradeId = model.Id });
return list;
}
public bool Create(GradeBindingModel model)
{
CheckModel(model);
if (_gradeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(GradeBindingModel model)
{
CheckModel(model);
if (_gradeStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(GradeBindingModel model)
{
CheckModel(model, false);
if (_gradeStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(GradeBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (!(model.Letter >= 'A' && model.Letter <= 'Z'))
{
throw new ArgumentNullException("Буква класса не является заглавной буквой",
nameof(model.Letter));
}
if (!(model.Year >= 1 && model.Year <= 11))
{
throw new ArgumentNullException("Год класса не является числом от 1 до 11",
nameof(model.Letter));
}
var element = _gradeStorage.GetElement(new GradeSearchModel
{
Letter = model.Letter,
Year = model.Year
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Такой класс уже есть");
}
}
}
}