PIbd-21_Shanygin_A.V_BaseData/Hotel/HotelBusinessLogic/BusinessLogics/PostLogic.cs

96 lines
2.5 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogics
{
public class PostLogic : IPostLogic
{
private readonly IPostStorage _postStorage;
public PostLogic(IPostStorage postStorage)
{
_postStorage = postStorage;
}
public PostViewModel? ReadElement(PostSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _postStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<PostViewModel>? ReadList(PostSearchModel? model)
{
var list = model == null ? _postStorage.GetFullList() : _postStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Create(PostBindingModel model)
{
CheckModel(model);
if (_postStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(PostBindingModel model)
{
CheckModel(model, false);
if (_postStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Update(PostBindingModel model)
{
CheckModel(model);
if (_postStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(PostBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.PostName))
{
throw new ArgumentNullException("Нужно заполнить поле должности", nameof(model.PostName));
}
}
}
}