SushiBarBase/SushiBar/SushiBarListImplement/Implements/OrderStorage.cs
2024-05-21 21:13:55 +04:00

125 lines
4.0 KiB
C#

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()
{
_source = DataListSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(AccessStorage(order.GetViewModel));
}
return result;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
if (order.Id == model.Id ||
(model.DateFrom.HasValue && model.DateTo.HasValue &&
model.DateFrom < order.DateCreate && order.DateCreate < model.DateTo))
{
result.Add(AccessStorage(order.GetViewModel));
}
}
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)
{
return AccessStorage(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 AccessStorage(newOrder.GetViewModel);
}
public OrderViewModel? Update(OrderBindingModel model)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
order.Update(model);
return AccessStorage(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 AccessStorage(element.GetViewModel);
}
}
return null;
}
public OrderViewModel AccessSushiStorage(OrderViewModel model)
{
foreach (var iceCream in _source.Sushis)
{
if (iceCream.Id == model.SushiId)
{
model.SushiName = iceCream.SushiName;
break;
}
}
return model;
}
public OrderViewModel AccessStorage(OrderViewModel model)
{
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;
if (client != null)
model.ClientFIO = client.ClientFIO;
if (implementer != null)
model.ImplementerName = implementer.ImplementerFIO;
return model;
}
}
}