using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryFileImplement.Models; namespace ConfectioneryFileImplement { public class OrderStorage : IOrderStorage { private readonly DataFileSingleton _source; public OrderStorage() { _source = DataFileSingleton.GetInstance(); } public OrderViewModel? Delete(OrderBindingModel model) { var element = _source.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { _source.Orders.Remove(element); _source.SaveOrders(); return element.GetViewModel; } return null; } 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 List GetFilteredList(OrderSearchModel model) { var result = GetElement(model); return result != null ? new() { result } : new(); } public List GetFullList() { return _source.Orders .Select(x => x.GetViewModel) .ToList(); } 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 pastry = _source.Orders.FirstOrDefault(x => x.Id == model.Id); if (pastry == null) { return null; } pastry.Update(model); _source.SaveOrders(); return pastry.GetViewModel; } } }