2024-02-17 17:52:28 +04:00
|
|
|
|
using PizzeriaContracts.BindingModels;
|
|
|
|
|
using PizzeriaContracts.SearchModels;
|
|
|
|
|
using PizzeriaContracts.StoragesContracts;
|
|
|
|
|
using PizzeriaContracts.ViewModels;
|
|
|
|
|
using PizzeriaFileImplement.Models;
|
|
|
|
|
|
|
|
|
|
namespace PizzeriaFileImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class OrderStorage : IOrderStorage
|
|
|
|
|
{
|
|
|
|
|
private readonly DataFileSingleton source;
|
|
|
|
|
|
|
|
|
|
public OrderStorage()
|
|
|
|
|
{
|
|
|
|
|
source = DataFileSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachPizzaName(x.GetViewModel)).ToList();
|
|
|
|
|
|
|
|
|
|
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
|
|
|
|
{
|
2024-03-22 10:07:28 +04:00
|
|
|
|
if (model.DateFrom.HasValue)
|
2024-02-17 17:52:28 +04:00
|
|
|
|
{
|
2024-03-22 10:07:28 +04:00
|
|
|
|
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
|
|
|
|
.Select(x => GetViewModel(x)).ToList();
|
|
|
|
|
}
|
2024-03-21 13:21:25 +04:00
|
|
|
|
|
2024-03-22 10:07:28 +04:00
|
|
|
|
if (model.ClientId.HasValue && !model.Id.HasValue)
|
2024-03-21 13:21:25 +04:00
|
|
|
|
{
|
2024-03-22 10:07:28 +04:00
|
|
|
|
return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
2024-03-21 13:21:25 +04:00
|
|
|
|
|
2024-03-22 10:07:28 +04:00
|
|
|
|
if (model.Id.HasValue)
|
2024-03-21 13:21:25 +04:00
|
|
|
|
{
|
2024-03-22 10:07:28 +04:00
|
|
|
|
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
|
|
|
|
|
}
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2024-02-17 17:52:28 +04:00
|
|
|
|
|
|
|
|
|
public OrderViewModel? GetElement(OrderSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
return AttachPizzaName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-22 10:07:28 +04:00
|
|
|
|
private OrderViewModel GetViewModel(Order order)
|
|
|
|
|
{
|
|
|
|
|
var viewModel = order.GetViewModel;
|
2024-03-21 13:21:25 +04:00
|
|
|
|
|
2024-03-22 10:07:28 +04:00
|
|
|
|
var pizza = source.Pizzas.FirstOrDefault(x => x.Id == order.PizzaId);
|
|
|
|
|
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
|
2024-03-21 13:21:25 +04:00
|
|
|
|
|
|
|
|
|
if (pizza != null)
|
|
|
|
|
{
|
|
|
|
|
viewModel.PizzaName = pizza.PizzaName;
|
|
|
|
|
}
|
2024-03-22 10:07:28 +04:00
|
|
|
|
if (client != null)
|
2024-03-21 13:21:25 +04:00
|
|
|
|
{
|
2024-03-22 10:07:28 +04:00
|
|
|
|
viewModel.ClientFIO = client.ClientFIO;
|
|
|
|
|
}
|
|
|
|
|
return viewModel;
|
|
|
|
|
}
|
2024-03-21 13:21:25 +04:00
|
|
|
|
|
2024-03-22 10:07:28 +04:00
|
|
|
|
public OrderViewModel? Insert(OrderBindingModel model)
|
2024-02-17 17:52:28 +04:00
|
|
|
|
{
|
|
|
|
|
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();
|
|
|
|
|
return AttachPizzaName(newOrder.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
return AttachPizzaName(order.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrderViewModel? Delete(OrderBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (order != null)
|
|
|
|
|
{
|
|
|
|
|
source.Orders.Remove(order);
|
|
|
|
|
source.SaveOrders();
|
|
|
|
|
return AttachPizzaName(order.GetViewModel);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private OrderViewModel? AttachPizzaName(OrderViewModel? model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var pizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId);
|
|
|
|
|
if (pizza != null)
|
|
|
|
|
{
|
|
|
|
|
model.PizzaName = pizza.PizzaName;
|
|
|
|
|
}
|
|
|
|
|
return model;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|