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

93 lines
2.7 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 ResultLogic : IResultLogic
{
private readonly IResultStorage _resultStorage;
public ResultLogic(IResultStorage resultStorage)
{
_resultStorage = resultStorage;
}
public List<ResultViewModel>? ReadList(ResultSearchModel? model)
{
var list = model == null ? _resultStorage.GetFullList() : _resultStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ResultViewModel? ReadElement(ResultSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _resultStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(ResultBindingModel model)
{
CheckModel(model);
if (_resultStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ResultBindingModel model)
{
CheckModel(model);
if (_resultStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ResultBindingModel model)
{
CheckModel(model, false);
if (_resultStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ResultBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
var element = _resultStorage.GetElement(new ResultSearchModel
{
StudentId = model.StudentId,
SubjectId = model.SubjectId,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Проект с таким названием уже есть");
}
}
}
}