97 lines
2.2 KiB
C#
97 lines
2.2 KiB
C#
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 SubjectLogic : ISubjectLogic
|
||
{
|
||
private readonly ISubjectStorage _subjectStorage;
|
||
public SubjectLogic(ISubjectStorage subjectStorage)
|
||
{
|
||
_subjectStorage = subjectStorage;
|
||
}
|
||
|
||
public List<SubjectViewModel> ReadList(SubjectSearchModel? model)
|
||
{
|
||
var list = model == null ? _subjectStorage.GetFullList() :
|
||
_subjectStorage.GetFilteredList(model);
|
||
return list;
|
||
}
|
||
|
||
public SubjectViewModel? ReadElement(SubjectSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _subjectStorage.GetElement(model);
|
||
return element;
|
||
}
|
||
public bool Create(SubjectBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_subjectStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(SubjectBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_subjectStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(SubjectBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_subjectStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
private void CheckModel(SubjectBindingModel model, bool withParams =
|
||
true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.SubjectName))
|
||
{
|
||
throw new ArgumentNullException("Нет названия предмета",
|
||
nameof(model.SubjectName));
|
||
}
|
||
var element = _subjectStorage.GetElement(new SubjectSearchModel
|
||
{
|
||
SubjectName = model.SubjectName
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Предмет с таким названием уже есть");
|
||
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
}
|