SUBD_SportCompetitions/SportCompetitionsBusinessLogic/BusinessLogics/CompetitionLogic.cs

94 lines
2.1 KiB
C#

using SportCompetitionsContracts.BindingModels;
using SportCompetitionsContracts.BusinessLogicsContracts;
using SportCompetitionsContracts.SearchModels;
using SportCompetitionsContracts.StoragesContracts;
using SportCompetitionsContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SportCompetitionsBusinessLogic.BusinessLogics
{
public class CompetitionLogic : ICompetitionLogic
{
private readonly ICompetitionStorage _competitionStorage;
public CompetitionLogic(ICompetitionStorage competitionStorage)
{
_competitionStorage = competitionStorage;
}
public CompetitionViewModel? ReadElement(CompetitionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _competitionStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<CompetitionViewModel>? ReadList(CompetitionSearchModel? model)
{
var list = model == null ? _competitionStorage.GetFullList() : _competitionStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(CompetitionBindingModel model)
{
CheckModel(model);
if (_competitionStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(CompetitionBindingModel model)
{
CheckModel(model, false);
if (_competitionStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(CompetitionBindingModel model)
{
CheckModel(model);
if (_competitionStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(CompetitionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.CompetitionName))
{
throw new ArgumentNullException("Нет названия", nameof(model.CompetitionName));
}
}
}
}