SUBD_Aleikin/Restaurant/RestaurantBusinessLogic/BusinessLogics/ProviderLogic.cs
Артём Алейкин e52b239c08 BusinessLogic,
Contracts,
DataModels,
View(пока пусто)
2023-05-02 22:51:41 +04:00

106 lines
3.1 KiB
C#
Raw Permalink 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 RestaurantContracts.BindingModels;
using RestaurantContracts.BusinessLogicsContracts;
using RestaurantContracts.SearchModels;
using RestaurantContracts.StoragesContracts;
using RestaurantContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RestaurantBusinessLogic.BusinessLogics
{
public class ProviderLogic : IProviderLogic
{
private readonly IProviderStorage _providerStorage;
public ProviderLogic(IProviderStorage providerStorage)
{
_providerStorage = providerStorage;
}
public bool Delete(ProviderBindingModel model)
{
CheckModel(model, false);
if (_providerStorage.Delete(model) == null)
{
return false;
}
return true;
}
public bool Create(ProviderBindingModel model)
{
CheckModel(model);
if (_providerStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ProviderBindingModel model)
{
CheckModel(model);
if (_providerStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ProviderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия поставщика", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Address))
{
throw new ArgumentNullException("Нет адресса", nameof(model.Address));
}
var element = _providerStorage.GetElement(new ProviderSearchModel
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Поставщик с таким названием уже есть");
}
}
public List<ProviderViewModel>? ReadList(ProviderSearchModel? model)
{
var list = (model == null) ? _providerStorage.GetFullList() : _providerStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ProviderViewModel? ReadElement(ProviderSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _providerStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
}
}