2024-02-27 14:27:20 +04:00
|
|
|
|
using SushiBarContracts.BindingModels;
|
|
|
|
|
using SushiBarContracts.SearchModels;
|
|
|
|
|
using SushiBarContracts.StoragesContracts;
|
|
|
|
|
using SushiBarContracts.ViewModels;
|
|
|
|
|
using SushiBarFileImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace SushiBarFileImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataFileSingleton source;
|
|
|
|
|
public OrderStorage()
|
|
|
|
|
{
|
|
|
|
|
source = DataFileSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
public List<OrderViewModel> GetFullList()
|
|
|
|
|
{
|
2024-05-19 16:50:41 +04:00
|
|
|
|
return source.Orders
|
|
|
|
|
.Select(x => AttachSushiName(x.GetViewModel))
|
|
|
|
|
.ToList();
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2024-05-19 16:50:41 +04:00
|
|
|
|
return source.Orders
|
|
|
|
|
.Where(o =>
|
|
|
|
|
(model.Id.HasValue && o.Id == model.Id) ||
|
|
|
|
|
(model.DateFrom.HasValue && model.DateTo.HasValue &&
|
|
|
|
|
model.DateFrom < o.DateCreate && o.DateCreate < model.DateTo))
|
|
|
|
|
.Select(x => AttachSushiName(x.GetViewModel))
|
|
|
|
|
.ToList();
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-05-19 16:50:41 +04:00
|
|
|
|
return AttachSushiName(source.Orders
|
|
|
|
|
.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel);
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.Id = source.Orders.Count > 0 ? source.Orders.Max(x => x.Id) + 1 : 1;
|
|
|
|
|
var newOrder = Order.Create(model);
|
|
|
|
|
if (newOrder == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
source.Orders.Add(newOrder);
|
|
|
|
|
source.SaveOrders();
|
2024-02-27 16:23:02 +04:00
|
|
|
|
return AttachSushiName(newOrder.GetViewModel);
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Update(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
order.Update(model);
|
|
|
|
|
source.SaveOrders();
|
2024-02-27 16:23:02 +04:00
|
|
|
|
return AttachSushiName(order.GetViewModel);
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var element = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
source.Orders.Remove(element);
|
|
|
|
|
source.SaveOrders();
|
2024-02-27 16:23:02 +04:00
|
|
|
|
return AttachSushiName(element.GetViewModel);
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-02-28 08:53:49 +04:00
|
|
|
|
private OrderViewModel AttachSushiName(OrderViewModel ?model)
|
2024-02-27 16:23:02 +04:00
|
|
|
|
{
|
|
|
|
|
if (model == null) { return null; }
|
|
|
|
|
model.SushiName = source.Sushis.First(x => x.Id == model.SushiId).SushiName;
|
|
|
|
|
return model;
|
|
|
|
|
}
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
}
|