PIbd-23-Lisov-N.A-SewingDre.../DressAtelierListImplement/Implements/OrderStorage.cs

120 lines
3.4 KiB
C#
Raw Normal View History

2023-02-13 02:45:41 +04:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DressAtelierContracts.BindingModels;
using DressAtelierContracts.SearchModels;
using DressAtelierContracts.StorageContracts;
using DressAtelierContracts.ViewModels;
using DressAtelierListImplement.Models;
namespace DressAtelierListImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataListSingleton _source;
public OrderStorage()
{
_source = DataListSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
2023-02-13 10:07:35 +04:00
result.Add(ReceiveDressName(order.GetViewModel));
2023-02-13 02:45:41 +04:00
}
return result;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.ID.HasValue)
{
return result;
}
2023-02-13 10:07:35 +04:00
foreach (var order in _source.Orders)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
if (order.ID == model.ID)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
result.Add(ReceiveDressName(order.GetViewModel));
2023-02-13 02:45:41 +04:00
}
}
return result;
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.ID.HasValue)
{
return null;
}
2023-02-13 10:07:35 +04:00
foreach (var order in _source.Orders)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
if (model.ID.HasValue && order.ID == model.ID)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
return ReceiveDressName(order.GetViewModel);
2023-02-13 02:45:41 +04:00
}
}
return null;
}
public OrderViewModel? Insert(OrderBindingModel model)
{
model.ID = 1;
2023-02-13 10:07:35 +04:00
foreach (var order in _source.Orders)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
if (model.ID <= order.ID)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
model.ID = order.ID + 1;
2023-02-13 02:45:41 +04:00
}
}
var newComponent = Order.Create(model);
if (newComponent == null)
{
return null;
}
_source.Orders.Add(newComponent);
2023-02-13 10:07:35 +04:00
return ReceiveDressName(newComponent.GetViewModel);
2023-02-13 02:45:41 +04:00
}
public OrderViewModel? Update(OrderBindingModel model)
{
2023-02-13 10:07:35 +04:00
foreach (var order in _source.Orders)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
if (order.ID == model.ID)
2023-02-13 02:45:41 +04:00
{
2023-02-13 10:07:35 +04:00
order.Update(model);
return ReceiveDressName(order.GetViewModel);
2023-02-13 02:45:41 +04:00
}
}
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);
2023-02-13 10:07:35 +04:00
return ReceiveDressName(element.GetViewModel);
2023-02-13 02:45:41 +04:00
}
}
return null;
}
2023-02-13 10:07:35 +04:00
private OrderViewModel? ReceiveDressName(OrderViewModel model)
{
var dress = _source.Dresses.Find(un => un.ID == model.ID);
model.DressName = dress == null ? "" : dress.DressName;
return model;
}
2023-02-13 02:45:41 +04:00
}
}