106 lines
3.0 KiB
C#
106 lines
3.0 KiB
C#
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 OrderLogic : IOrderLogic
|
||
{
|
||
private readonly IOrderStorage _orderStorage;
|
||
|
||
public OrderLogic(IOrderStorage orderStorage)
|
||
{
|
||
_orderStorage = orderStorage;
|
||
}
|
||
|
||
public bool Create(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_orderStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(OrderBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_orderStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public OrderViewModel? ReadElement(OrderSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _orderStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
|
||
{
|
||
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public bool Update(OrderBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_orderStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(OrderBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (model.Price <= 0)
|
||
{
|
||
throw new ArgumentNullException("Нет цены у заказа", nameof(model.Price));
|
||
}
|
||
if (model.Date != null)
|
||
{
|
||
throw new ArgumentNullException("Нет даты у заказа", nameof(model.Date));
|
||
}
|
||
var element = _orderStorage.GetElement(new OrderSearchModel
|
||
{
|
||
Date = model.Date,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Заказ с таким номером уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|