92 lines
2.6 KiB
C#
92 lines
2.6 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using TaskTrackerContracts.BindingModels;
|
||
using TaskTrackerContracts.BusinessLogicsContracts;
|
||
using TaskTrackerContracts.SearchModels;
|
||
using TaskTrackerContracts.StoragesContracts;
|
||
using TaskTrackerContracts.ViewModels;
|
||
|
||
namespace TaskTrackerBusinessLogics.BusinessLogic
|
||
{
|
||
public class ExamLogic : IExamLogic
|
||
{
|
||
private readonly IExamStorage _examStorage;
|
||
public ExamLogic(IExamStorage examStorage)
|
||
{
|
||
_examStorage = examStorage;
|
||
}
|
||
public List<ExamViewModel>? ReadList(ExamSearchModel? model)
|
||
{
|
||
var list = model == null ? _examStorage.GetFullList() : _examStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
public ExamViewModel? ReadElement(ExamSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _examStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
public bool Create(ExamBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_examStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(ExamBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_examStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(ExamBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_examStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(ExamBindingModel model, bool withParams =
|
||
true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
var element = _examStorage.GetElement(new ExamSearchModel
|
||
{
|
||
SubjectId = model.SubjectId,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Проект с таким названием уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|