PIbd-21_Pyatakov_KM_Markov_.../UniversityBusinessLogic/BusinessLogics/DisciplineLogic.cs
2023-04-08 13:59:34 +04:00

98 lines
2.9 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 DisciplineLogic : IDisciplineLogic
{
private readonly IDisciplineStorage _disciplineStorage;
public DisciplineLogic(IDisciplineStorage disciplineStorage)
{
_disciplineStorage = disciplineStorage;
}
public bool Create(DisciplineBindingModel model)
{
CheckModel(model);
if (_disciplineStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(DisciplineBindingModel model)
{
CheckModel(model, false);
if (_disciplineStorage.Delete(model) == null)
{
return false;
}
return true;
}
public DisciplineViewModel? ReadElement(DisciplineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var discipline = _disciplineStorage.GetElement(model);
if (discipline == null)
{
return null;
}
return discipline;
}
public List<DisciplineViewModel>? ReadList(DisciplineSearchModel? model)
{
var list = model == null ? _disciplineStorage.GetFullList() : _disciplineStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(DisciplineBindingModel model)
{
CheckModel(model, false);
if (_disciplineStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(DisciplineBindingModel 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));
}
var discipline = _disciplineStorage.GetElement(new DisciplineSearchModel { Name = model.Name });
if (discipline != null)
{
throw new InvalidOperationException("Дисциплина с таким названием уже есть");
}
}
}
}