SushiBarBase/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs

125 lines
4.0 KiB
C#
Raw Permalink Normal View History

2024-02-09 17:38:25 +04:00
using SushiBarContracts.BindingModels;
using SushiBarContracts.SearchModels;
using SushiBarContracts.StoragesContracts;
using SushiBarContracts.ViewModels;
using SushiBarListImplement.Models;
namespace SushiBarListImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataListSingleton _source;
public OrderStorage()
{
2024-02-14 09:58:18 +04:00
_source = DataListSingleton.GetInstance();
2024-02-09 17:38:25 +04:00
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
2024-05-21 21:13:55 +04:00
result.Add(AccessStorage(order.GetViewModel));
2024-02-09 17:38:25 +04:00
}
return result;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
2024-02-24 18:41:00 +04:00
foreach (var order in _source.Orders)
{
if (order.Id == model.Id ||
(model.DateFrom.HasValue && model.DateTo.HasValue &&
2024-05-19 17:58:47 +04:00
model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo))
2024-02-24 18:41:00 +04:00
{
2024-05-21 21:13:55 +04:00
result.Add(AccessStorage(order.GetViewModel));
2024-02-24 18:41:00 +04:00
}
}
2024-02-09 17:38:25 +04:00
return result;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
foreach (var order in _source.Orders)
{
if (model.Id.HasValue && order.Id == model.Id)
{
2024-05-21 21:13:55 +04:00
return AccessStorage(order.GetViewModel);
2024-02-09 17:38:25 +04:00
}
}
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);
2024-05-21 21:13:55 +04:00
return AccessStorage(newOrder.GetViewModel);
2024-02-09 17:38:25 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
order.Update(model);
2024-05-21 21:13:55 +04:00
return AccessStorage(order.GetViewModel);
2024-02-09 17:38:25 +04:00
}
}
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);
2024-05-21 21:13:55 +04:00
return AccessStorage(element.GetViewModel);
2024-02-09 17:38:25 +04:00
}
}
return null;
}
2024-05-19 17:58:47 +04:00
public OrderViewModel AccessSushiStorage(OrderViewModel model)
2024-02-24 18:41:00 +04:00
{
2024-05-19 17:58:47 +04:00
foreach (var iceCream in _source.Sushis)
2024-02-24 18:41:00 +04:00
{
2024-05-19 17:58:47 +04:00
if (iceCream.Id == model.SushiId)
2024-02-24 18:41:00 +04:00
{
2024-05-19 17:58:47 +04:00
model.SushiName = iceCream.SushiName;
break;
2024-02-24 18:41:00 +04:00
}
}
return model;
}
2024-05-19 17:58:47 +04:00
2024-05-21 21:13:55 +04:00
public OrderViewModel AccessStorage(OrderViewModel model)
2024-05-19 17:58:47 +04:00
{
2024-05-21 21:13:55 +04:00
var iceCream = _source.Sushis.FirstOrDefault(x => x.Id == model.Id);
var client = _source.Clients.FirstOrDefault(x => x.Id == model.Id);
var implementer = _source.Implementers.FirstOrDefault(x => x.Id == model.ImplementerId);
if (iceCream != null)
model.SushiName = iceCream.SushiName;
2024-05-19 17:58:47 +04:00
if (client != null)
model.ClientFIO = client.ClientFIO;
2024-05-21 21:13:55 +04:00
if (implementer != null)
model.ImplementerName = implementer.ImplementerFIO;
2024-05-19 17:58:47 +04:00
return model;
}
2024-02-09 17:38:25 +04:00
}
}