SUBD_Aleikin/Restaurant/RestaurantBusinessLogic/BusinessLogics/OrderLogic.cs

107 lines
3.0 KiB
C#
Raw 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 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("Заказ с таким номером уже есть");
}
}
}
}