121 lines
3.0 KiB
C#
121 lines
3.0 KiB
C#
|
using SchoolScheduleContracts.BindingModels;
|
|||
|
using SchoolScheduleContracts.BusinessLogicsContracts;
|
|||
|
using SchoolScheduleContracts.SearchModels;
|
|||
|
using SchoolScheduleContracts.StoragesContracts;
|
|||
|
using SchoolScheduleContracts.ViewModels;
|
|||
|
using SchoolScheduleDataModels.Enums;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace SchoolScheduleBusinessLogic
|
|||
|
{
|
|||
|
public class LessonLogic : ILessonLogic
|
|||
|
{
|
|||
|
private readonly ILessonStorage _lessonStorage;
|
|||
|
private readonly ISubjectStorage _subjectStorage;
|
|||
|
private readonly ITeacherStorage _teacherStorage;
|
|||
|
private readonly IGradeStorage _gradeStorage;
|
|||
|
private readonly IStudentStorage _studentStorage;
|
|||
|
public LessonLogic(ILessonStorage lessonStorage, ISubjectStorage subjectStorage, ITeacherStorage teacherStorage, IGradeStorage gradeStorage, IStudentStorage studentStorage)
|
|||
|
{
|
|||
|
_lessonStorage = lessonStorage;
|
|||
|
_subjectStorage = subjectStorage;
|
|||
|
_teacherStorage = teacherStorage;
|
|||
|
_gradeStorage = gradeStorage;
|
|||
|
_studentStorage = studentStorage;
|
|||
|
}
|
|||
|
|
|||
|
public List<LessonViewModel> ReadList(LessonSearchModel? model)
|
|||
|
{
|
|||
|
var list = model == null ? _lessonStorage.GetFullList() :
|
|||
|
_lessonStorage.GetFilteredList(model);
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public LessonViewModel? ReadElement(LessonSearchModel model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
var element = _lessonStorage.GetElement(model);
|
|||
|
return element;
|
|||
|
}
|
|||
|
|
|||
|
public List<StudentViewModel> GetStudents(LessonSearchModel model)
|
|||
|
{
|
|||
|
var list = _studentStorage.GetFilteredList(new StudentSearchModel { GradeId = model.GradeId });
|
|||
|
return list;
|
|||
|
}
|
|||
|
|
|||
|
public bool Create(LessonBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_lessonStorage.Insert(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
public bool Update(LessonBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model);
|
|||
|
if (_lessonStorage.Update(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
public bool Delete(LessonBindingModel model)
|
|||
|
{
|
|||
|
CheckModel(model, false);
|
|||
|
if (_lessonStorage.Delete(model) == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private void CheckModel(LessonBindingModel model, bool withParams =
|
|||
|
true)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
throw new ArgumentNullException(nameof(model));
|
|||
|
}
|
|||
|
if (!withParams)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var element1 = _lessonStorage.GetElement(new LessonSearchModel
|
|||
|
{
|
|||
|
Date = model.Date,
|
|||
|
SchedulePlaceId = model.SchedulePlaceId,
|
|||
|
TeacherId = model.TeacherId
|
|||
|
});
|
|||
|
var element2 = _lessonStorage.GetElement(new LessonSearchModel
|
|||
|
{
|
|||
|
Date = model.Date,
|
|||
|
SchedulePlaceId = model.SchedulePlaceId,
|
|||
|
GradeId = model.GradeId
|
|||
|
});
|
|||
|
|
|||
|
if (element1 != null)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("У учителя уже есть урок в это время");
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
if (element2 != null)
|
|||
|
{
|
|||
|
throw new InvalidOperationException("У класса уже есть урок в это время");
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|