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

127 lines
3.6 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
2024-04-26 00:17:50 +04:00
{
private readonly DataFileSingleton source;
2024-03-15 15:32:53 +04:00
2024-04-26 00:17:50 +04:00
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
2024-03-15 15:32:53 +04:00
2024-04-26 00:17:50 +04:00
public List<OrderViewModel> GetFullList() => source.Orders.Select(x => AttachCannedName(x.GetViewModel)).ToList();
2024-03-15 15:32:53 +04:00
2024-04-26 00:17:50 +04:00
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-04-30 22:57:42 +04:00
if (model.DateFrom.HasValue)
{
return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => GetViewModel(x)).ToList();
}
2024-04-26 00:17:50 +04:00
if (model.ClientId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
}
2024-04-30 22:57:42 +04:00
if (!model.ImplementerId.HasValue && !model.Id.HasValue)
{
return source.Orders.Where(x => x.ImplementerId == model.ImplementerId).Select(x => x.GetViewModel).ToList();
}
2024-04-26 00:17:50 +04:00
if (model.Id.HasValue)
{
return source.Orders.Where(x => x.Id.Equals(model.Id)).Select(x => GetViewModel(x)).ToList();
}
return new();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
2024-04-30 22:57:42 +04:00
if (model.ImplementerId.HasValue)
{
return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
}
2024-04-26 00:17:50 +04:00
if (!model.Id.HasValue)
{
return new();
}
return AttachCannedName(source.Orders.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel);
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var canned = source.Canneds.FirstOrDefault(x => x.Id == order.CannedId);
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
if (canned != null)
{
viewModel.CannedName = canned.CannedName;
}
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
return viewModel;
}
2024-03-15 15:32:53 +04:00
2024-04-26 00:17:50 +04:00
public OrderViewModel? Insert(OrderBindingModel model)
2024-03-15 15:32:53 +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 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;
}
}
}