2024-05-14 23:07:08 +04:00

92 lines
2.6 KiB
C#
Raw 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 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("Проект с таким названием уже есть");
}
}
}
}