PIbd22_Kuznetsov_A.V._Sewin.../SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs

82 lines
2.8 KiB
C#
Raw Normal View History

2024-05-04 19:42:41 +04:00
using SewingDressesContracts.BindingModels;
using SewingDressesContracts.SearchModels;
using SewingDressesContracts.StoragesContracts;
using SewingDressesContracts.ViewModels;
using SewingDressesDataModels.Models;
using SewingDressesFileImplement.Models;
namespace SewingDressesFileImplement.Implements
{
public class OrderStorage : IOrderStorage
{
private readonly DataFileSingleton source;
public OrderStorage()
{
source = DataFileSingleton.GetInstance();
}
public List<OrderViewModel> GetFullList()
{
return source.Orders.Select(x => AcessDressesStorage(x.GetViewModel)).ToList();
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
2024-05-04 19:46:22 +04:00
if (model.Id.HasValue)
2024-05-04 19:42:41 +04:00
return source.Orders.Where(x => x.Id == model.Id)
.Select(x => AcessDressesStorage( x.GetViewModel))
.ToList();
2024-05-04 19:46:22 +04:00
else
return source.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).Select(x => AcessDressesStorage(x.GetViewModel)).ToList();
2024-05-04 19:42:41 +04:00
}
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
return source.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
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 newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
var component = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
source.SaveOrders();
return component.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
var component = source.Orders.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
source.Orders.Remove(component);
source.SaveOrders();
return component.GetViewModel;
}
public OrderViewModel AcessDressesStorage(OrderViewModel model)
{
var component = source.Dresses.FirstOrDefault(x => x.Id == model.DressId);
model.DressName = component.DressName;
return model;
}
}
}