PIbd-21_KozyrevSS_SewingDre.../SewingDresses/SewingDressesFileImplement/Implements/OrderStorage.cs

78 lines
2.6 KiB
C#
Raw Normal View History

2024-02-23 20:02:16 +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)
{
return source.Orders.Where(x => x.Id == model.Id)
.Select(x => AcessDressesStorage( x.GetViewModel))
.ToList();
}
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;
}
}
}