PIbd-22_Kozlova_A.A_Precast.../PrecastConcretePlant/PrecastConcretePlantFileImplement/Implements/OrderStorage.cs

117 lines
3.8 KiB
C#
Raw Normal View History

2024-03-06 09:19:13 +04:00
using PrecastConcretePlantContracts.BindingModels;
using PrecastConcretePlantContracts.SearchModels;
using PrecastConcretePlantContracts.StoragesContracts;
using PrecastConcretePlantContracts.ViewModels;
using PrecastConcretePlantFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PrecastConcretePlantFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders
2024-04-07 22:12:50 +04:00
.Select(x => AddData(x.GetViewModel))
2024-03-06 09:19:13 +04:00
.ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-04-07 22:12:50 +04:00
if (model.Id.HasValue)
2024-03-06 09:19:13 +04:00
{
2024-04-07 22:12:50 +04:00
return source.Orders
.Where(x => x.Id == model.Id)
.Select(x => AddData(x.GetViewModel))
.ToList();
2024-03-06 09:19:13 +04:00
}
2024-04-07 22:12:50 +04:00
else if (model.DateFrom.HasValue && model.DateTo.HasValue)
2024-04-03 00:11:17 +04:00
{
return source.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
2024-04-07 22:12:50 +04:00
.Select(x => AddData(x.GetViewModel))
2024-04-03 00:11:17 +04:00
.ToList();
}
2024-04-07 22:12:50 +04:00
else if (model.ClientId.HasValue)
{
return source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => AddData(x.GetViewModel))
.ToList();
}
return new();
2024-03-06 09:19:13 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
2024-04-07 22:12:50 +04:00
return AddData(order.GetViewModel);
2024-03-06 09:19:13 +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-04-07 22:12:50 +04:00
return AddData(newOrder.GetViewModel);
2024-03-06 09:19:13 +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-04-07 22:12:50 +04:00
return AddData(order.GetViewModel);
2024-03-06 09:19:13 +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-04-07 22:12:50 +04:00
return AddData(element.GetViewModel);
2024-03-06 09:19:13 +04:00
}
return null;
}
2024-04-07 22:12:50 +04:00
private OrderViewModel AddData(OrderViewModel model)
2024-03-06 09:19:13 +04:00
{
var selectedReinforced = source.Reinforceds.FirstOrDefault(x => x.Id == model.ReinforcedId);
2024-04-07 22:12:50 +04:00
model.ReinforcedName = selectedReinforced?.ReinforcedName ?? string.Empty;
var selectedClient = source.Clients.FirstOrDefault(x => x.Id == model.ClientId);
model.ClientFIO = selectedClient?.ClientFIO ?? string.Empty;
2024-03-06 09:19:13 +04:00
return model;
}
}
}