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 GetFullList() { return source.Orders.Select(x => AcessDressesStorage(x.GetViewModel)).ToList(); } public List GetFilteredList(OrderSearchModel model) { if (model.Id.HasValue) return source.Orders.Where(x => x.Id == model.Id) .Select(x => AcessDressesStorage( x.GetViewModel)) .ToList(); else return source.Orders.Where(x => x.DateCreate >= model.DateFrom).Where(x => x.DateCreate <= model.DateTo).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; } } }