PIbd22_NikiforovaMV_Automob.../AutomobilePlantListImplements/Implements/OrderStorage.cs

172 lines
5.1 KiB
C#
Raw Normal View History

2024-04-17 10:13:26 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutomobilePlantContracts.BindingModels;
using AutomobilePlantContracts.SearchModels;
using AutomobilePlantContracts.StorageContracts;
using AutomobilePlantContracts.ViewModels;
using AutomobilePlantListImplements.Models;
namespace AutomobilePlantListImplements.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataListSingleton _source;
public OrderStorage()
{
_source = DataListSingleton.GetInstance();
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
foreach (var order in _source.Orders)
{
if (model.Id.HasValue && order.Id == model.Id)
2024-06-22 22:27:59 +04:00
{
return GetViewModel(order);
}
else if (model.ImplementerId.HasValue && model.Status != null && order.ImplementerId == model.ImplementerId && model.Status.Equals(order.Status))
{
return GetViewModel(order);
}
else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId)
2024-04-17 10:13:26 +04:00
{
return GetViewModel(order);
}
}
return null;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
2024-06-22 22:23:42 +04:00
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id || model.DateFrom <= order.DateCreate && order.DateCreate <= model.DateTo)
{
result.Add(GetViewModel(order));
}
}
}
if (model.ClientId.HasValue)
2024-04-17 10:13:26 +04:00
{
2024-06-22 22:23:42 +04:00
foreach (var order in _source.Orders)
2024-04-17 10:13:26 +04:00
{
2024-06-22 22:23:42 +04:00
if (order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
2024-04-17 10:13:26 +04:00
}
}
2024-06-22 22:27:59 +04:00
if (model.ImplementerId.HasValue)
{
foreach (var order in _source.Orders)
{
if (order.ImplementerId == model.ImplementerId)
{
result.Add(GetViewModel(order));
}
}
}
if (model.Status != null)
{
foreach (var order in _source.Orders)
{
if (model.Status.Equals(order.Status))
{
result.Add(GetViewModel(order));
}
}
}
2024-04-17 10:13:26 +04:00
return result;
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(GetViewModel(order));
}
return result;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.Id = 1;
foreach (var order in _source.Orders)
{
if (model.Id <= order.Id)
{
model.Id = order.Id + 1;
}
}
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
_source.Orders.Add(newOrder);
return GetViewModel(newOrder);
}
public OrderViewModel? Update(OrderBindingModel model)
{
foreach (var order in _source.Orders)
{
if (order.Id == model.Id)
{
order.Update(model);
return GetViewModel(order);
}
}
return null;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
for (int i = 0; i < _source.Orders.Count; ++i)
{
if (_source.Orders[i].Id == model.Id)
{
var element = _source.Orders[i];
_source.Orders.RemoveAt(i);
return GetViewModel(element);
}
}
return null;
}
private OrderViewModel GetViewModel(Order order)
{
var viewModel = order.GetViewModel;
foreach (var car in _source.Cars)
{
if (car.Id == order.CarId)
{
viewModel.CarName = car.CarName;
break;
}
}
2024-06-22 22:23:42 +04:00
foreach (var client in _source.Clients)
{
if (client.Id == order.ClientId)
{
viewModel.ClientFIO = client.ClientFIO;
break;
}
}
2024-04-17 10:13:26 +04:00
return viewModel;
}
}
}