SUBD_SchoolSchedule/SchoolSchedule/SchoolScheduleBusinessLogic/SubjectLogic.cs
2024-04-08 14:40:49 +04:00

97 lines
2.2 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 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("Предмет с таким названием уже есть");
}
}
}
}