SUBD_Aleikin/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs

127 lines
3.4 KiB
C#
Raw Normal View History

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
{
ClientId = model.ClientId,
Date = model.Date,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Заказ с таким номером уже есть");
}
}
2023-05-17 00:48:03 +04:00
public bool ClearList()
{
return _orderStorage.ClearList();
}
public string DiffGetTest(int count)
{
return _orderStorage.DiffGetTest(count);
}
public string GetTest(int count)
{
return _orderStorage.GetTest(count);
}
public string SetTest(int count)
{
return _orderStorage.SetTest(count);
}
}
}