PIbd-22_Gerimovich.I.M._Fur.../FurnitureAssembly/FurnitureAssemblyFileImplement/Implements/OrderStorage.cs

134 lines
3.9 KiB
C#
Raw Normal View History

2024-03-27 00:02:22 +04:00
using FurnitureAssemblyContracts.BindingModels;
using FurnitureAssemblyContracts.SearchModels;
using FurnitureAssemblyContracts.StoragesContracts;
using FurnitureAssemblyContracts.ViewModels;
using FurnitureAssemblyFileImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FurnitureAssemblyFileImplement.Implements
{
2024-05-22 10:14:48 +04:00
// Реализация интерфейса хранилища заказов
2024-03-27 00:02:22 +04:00
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public OrderViewModel? GetElement(OrderSearchModel model)
2024-03-27 00:02:22 +04:00
{
if (!model.Id.HasValue)
{
return null;
}
2024-06-21 14:50:51 +04:00
if (model.ImplementerId.HasValue && model.Status != null)
return source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId && model.Status
.Equals(x.Status))?.GetViewModel;
return source.Orders
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
2024-03-27 00:02:22 +04:00
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-06-21 14:50:51 +04:00
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue && model.Status == null)
2024-03-27 00:02:22 +04:00
{
return new();
2024-03-27 00:02:22 +04:00
}
2024-06-21 14:50:51 +04:00
return source.Orders
2024-06-21 14:50:51 +04:00
.Where(x => x.Id == model.Id || model.DateFrom <= x.DateCreate && x.DateCreate <= model.DateTo
|| x.ClientId == model.ClientId || model.Status.Equals(x.Status))
.Select(x => GetViewModel(x)).ToList();
2024-03-27 00:02:22 +04:00
}
public List<OrderViewModel> GetFullList()
2024-03-27 00:02:22 +04:00
{
return source.Orders.Select(x => GetViewModel(x)).ToList();
2024-03-27 00:02:22 +04:00
}
2024-06-21 14:50:51 +04:00
// Для загрузки названий изделия и исполнителя в заказе
2024-03-27 00:02:22 +04:00
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
var furniture = source.Furnitures.FirstOrDefault(x => x.Id == order.FurnitureId);
var client = source.Clients.FirstOrDefault(x => x.Id == order.ClientId);
2024-06-21 14:50:51 +04:00
var implementer = source.Implementers.FirstOrDefault(x => x.Id == order.ImplementerId);
if (furniture != null)
{
viewModel.FurnitureName = furniture.FurnitureName;
}
if (client != null)
{
viewModel.ClientFIO = client.ClientFIO;
}
2024-03-27 00:02:22 +04:00
2024-06-21 14:50:51 +04:00
if (implementer != null)
{
viewModel.ImplementerFIO = implementer.ImplementerFIO;
}
return viewModel;
2024-03-27 00:02:22 +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();
return GetViewModel(newOrder);
}
public OrderViewModel? Update(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
2024-05-22 10:14:48 +04:00
if(order == null)
2024-03-27 00:02:22 +04:00
{
return null;
}
order.Update(model);
source.SaveOrders();
return GetViewModel(order);
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var order = source.Orders.FirstOrDefault(x => x.Id == model.Id);
2024-03-27 00:02:22 +04:00
2024-05-22 10:14:48 +04:00
if(order != null)
2024-03-27 00:02:22 +04:00
{
source.Orders.Remove(order);
2024-03-27 00:02:22 +04:00
source.SaveOrders();
return GetViewModel(order);
2024-03-27 00:02:22 +04:00
}
return null;
}
}
}