ISEbd-21_KashtanovD.A.FishF.../FishFactory/FishFactoryFileImplement/Implements/OrderStorage.cs

87 lines
2.8 KiB
C#
Raw Normal View History

2024-03-15 15:32:53 +04:00
using FishFactoryContracts.BindingModels;
using FishFactoryContracts.SearchModels;
using FishFactoryContracts.StoragesContracts;
using FishFactoryContracts.ViewModels;
using FishFactoryFileImplement.Models;
namespace FishFactoryFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachCannedName(x.GetViewModel)).ToList();
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-04-05 01:59:38 +04:00
if (!model.Id.HasValue || !model.DateFrom.HasValue || !model.DateTo.HasValue)
2024-03-15 15:32:53 +04:00
{
return new();
}
2024-04-05 01:59:38 +04:00
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => AttachCannedName(x.GetViewModel)).ToList();
2024-03-15 15:32:53 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
return AttachCannedName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
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();
return AttachCannedName(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 AttachCannedName(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 AttachCannedName(order.GetViewModel);
}
return null;
}
private OrderViewModel? AttachCannedName(OrderViewModel? model)
{
if (model == null)
{
return null;
}
var canned = source.Canneds.FirstOrDefault(x => x.Id == model.CannedId);
model.CannedName = canned?.CannedName;
return model;
}
}
}