2024-02-25 19:43:57 +04:00
|
|
|
|
using SushiBarContracts.BindingModel;
|
|
|
|
|
using SushiBarContracts.SearchModel;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarListImplement;
|
2024-02-25 19:52:48 +04:00
|
|
|
|
using SushiBarListImplements.Models;
|
2024-02-25 19:43:57 +04:00
|
|
|
|
|
2024-02-25 19:52:48 +04:00
|
|
|
|
namespace SushiBarListImplements.Implements
|
2024-02-25 19:43:57 +04:00
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
private DataListSingleton _source;
|
|
|
|
|
|
|
|
|
|
public OrderStorage()
|
|
|
|
|
{
|
|
|
|
|
_source = DataListSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
var result = new List<OrderViewModel>();
|
|
|
|
|
foreach (var order in _source.Orders)
|
|
|
|
|
{
|
|
|
|
|
result.Add(AddSushiName(order.GetViewModel));
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-03-24 23:52:35 +04:00
|
|
|
|
var orders = _source.Orders
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
if (model.Id.HasValue)
|
2024-02-25 19:43:57 +04:00
|
|
|
|
{
|
2024-03-24 23:52:35 +04:00
|
|
|
|
orders = orders.Where(x => x.Id == model.Id.Value).ToList();
|
2024-02-25 19:43:57 +04:00
|
|
|
|
}
|
2024-03-24 23:52:35 +04:00
|
|
|
|
if (model.DateFrom.HasValue)
|
2024-02-25 19:43:57 +04:00
|
|
|
|
{
|
2024-03-24 23:52:35 +04:00
|
|
|
|
orders = orders.Where(x => x.DateCreate >= model.DateFrom.Value).ToList();
|
2024-02-25 19:43:57 +04:00
|
|
|
|
}
|
2024-03-24 23:52:35 +04:00
|
|
|
|
if (model.DateTo.HasValue)
|
|
|
|
|
{
|
|
|
|
|
orders = orders.Where(x => x.DateCreate <= model.DateTo.Value).ToList();
|
|
|
|
|
}
|
|
|
|
|
return orders;
|
2024-02-25 19:43:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
foreach (var order in _source.Orders)
|
|
|
|
|
{
|
|
|
|
|
if (order.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
return AddSushiName(order.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.Id = 1;
|
|
|
|
|
foreach (var order in _source.Orders)
|
|
|
|
|
{
|
|
|
|
|
if (model.Id <= order.Id)
|
|
|
|
|
{
|
|
|
|
|
model.Id = order.Id + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newOrder = Order.Create(model);
|
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
_source.Orders.Add(newOrder);
|
|
|
|
|
return AddSushiName(newOrder.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
foreach (var order in _source.Orders)
|
|
|
|
|
{
|
|
|
|
|
if (order.Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
order.Update(model);
|
|
|
|
|
return AddSushiName(order.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < _source.Orders.Count; ++i)
|
|
|
|
|
{
|
|
|
|
|
if (_source.Orders[i].Id == model.Id)
|
|
|
|
|
{
|
|
|
|
|
var element = _source.Orders[i];
|
|
|
|
|
_source.Orders.RemoveAt(i);
|
|
|
|
|
return AddSushiName(element.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OrderViewModel AddSushiName(OrderViewModel model)
|
|
|
|
|
{
|
|
|
|
|
foreach (var sushi in _source.Sushis)
|
|
|
|
|
{
|
|
|
|
|
if (sushi.Id == model.SushiId)
|
|
|
|
|
{
|
|
|
|
|
model.SushiName = sushi.SushiName;
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|