PIbd-22_Katysheva_N.E._Pizz.../Pizzeria/PizzeriaFileImplement/Implements/OrderStorage.cs

103 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-03-12 23:45:49 +04:00
using PizzeriaFileImplement.Models;
using PizzeriaContracts.BindingModels;
using PizzeriaContracts.SearchModels;
using PizzeriaContracts.StorageContracts;
using PizzeriaContracts.ViewModels;
namespace PizzeriaFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
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-05-07 23:20:02 +04:00
return AddData(element.GetViewModel);
2024-03-12 23:45:49 +04:00
}
return null;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
2024-06-21 15:37:13 +04:00
var order = source.Orders.FirstOrDefault(x =>
model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId && model.Status != null && x.Status.Equals(model.Status)
|| model.Status == null && model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId
|| model.Id.HasValue && x.Id == model.Id);
return order?.GetViewModel != null ? AddData(order.GetViewModel) : null;
2024-03-12 23:45:49 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-05-07 23:20:02 +04:00
if (model.Id.HasValue)
{
return source.Orders.Where(x => x.Id == model.Id).Select(x => AddData(x.GetViewModel)).ToList();
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue)
2024-03-12 23:45:49 +04:00
{
2024-05-07 23:20:02 +04:00
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AddData(x.GetViewModel)).ToList();
2024-03-12 23:45:49 +04:00
}
2024-05-07 23:20:02 +04:00
else if (model.ClientId.HasValue)
2024-04-10 02:43:55 +04:00
{
2024-05-07 23:20:02 +04:00
return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => AddData(x.GetViewModel)).ToList();
2024-04-10 02:43:55 +04:00
}
2024-06-21 15:37:13 +04:00
if (!model.ImplementerId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList();
}
2024-05-07 23:20:02 +04:00
return new();
2024-03-12 23:45:49 +04:00
}
public List<OrderViewModel> GetFullList()
{
2024-05-07 23:20:02 +04:00
return source.Orders.Select(x => AddData(x.GetViewModel)).ToList();
2024-03-12 23:45:49 +04:00
}
2024-06-21 15:37:13 +04:00
2024-03-12 23:45:49 +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-05-07 23:20:02 +04:00
return AddData(newOrder.GetViewModel);
2024-03-12 23:45:49 +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-05-07 23:20:02 +04:00
return AddData(order.GetViewModel);
2024-03-12 23:45:49 +04:00
}
2024-05-07 23:20:02 +04:00
private OrderViewModel AddData(OrderViewModel model)
2024-03-12 23:45:49 +04:00
{
var selectedPizza = source.Pizzas.FirstOrDefault(x => x.Id == model.PizzaId);
model.PizzaName = selectedPizza?.PizzaName;
2024-05-07 23:20:02 +04:00
var selectedClient = source.Clients.FirstOrDefault(x => x.Id == model.ClientId);
model.ClientFIO = selectedClient?.ClientFIO ?? string.Empty;
2024-03-12 23:45:49 +04:00
return model;
}
}
}