SUBD_SushiBar/SushiBar/SushiBarBusinessLogic/BusinessLogics/PlaceLogic.cs

99 lines
2.6 KiB
C#

using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SushiBarBusinessLogic.BusinessLogics
{
public class PlaceLogic : IPlaceLogic
{
private readonly IPlaceStorage _placeStorage;
public PlaceLogic(IPlaceStorage placeStorage)
{
_placeStorage = placeStorage;
}
public PlaceViewModel? ReadElement(PlaceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _placeStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<PlaceViewModel>? ReadList(PlaceSearchModel? model)
{
var list = model == null ? _placeStorage.GetFullList() : _placeStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(PlaceBindingModel model)
{
CheckModel(model);
if (_placeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(PlaceBindingModel model)
{
CheckModel(model, false);
if (_placeStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(PlaceBindingModel model)
{
CheckModel(model);
if (_placeStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(PlaceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PlaceNumber.ToString()))
{
throw new ArgumentNullException("Нет названия", nameof(model.PlaceNumber));
}
}
public void ClearEntity()
{
_placeStorage.ClearEntity();
}
}
}