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-02-27 16:23:02 +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();
|
|
|
|
|
}
|
|
|
|
|
return source.Orders.Where(x =>
|
|
|
|
|
x.Id == model.Id).Select(x =>
|
2024-02-27 16:23:02 +04:00
|
|
|
|
AttachSushiName(x.GetViewModel)).ToList();
|
2024-02-27 14:27:20 +04:00
|
|
|
|
}
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2024-02-27 16:23:02 +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-27 16:23:02 +04:00
|
|
|
|
private OrderViewModel AttachSushiName(OrderViewModel model)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|