using ConfectioneryContracts.BindingModels; using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.ViewModels; using ConfectioneryListImplement.Models; namespace ConfectioneryListImplement { public class OrderStorage : IOrderStorage { private readonly DataListSingleton _source; public OrderStorage() { _source = DataListSingleton.GetInstance(); } 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); return element.GetViewModel; } } return null; } public OrderViewModel? GetElement(OrderSearchModel model) { if (model.ImplementerId.HasValue) { return _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel; } if (!model.Id.HasValue) { return null; } foreach (var order in _source.Orders) { if (model.Id.HasValue && order.Id == model.Id) { return GetViewModel(order); } } return null; } public List GetFilteredList(OrderSearchModel model) { var result = new List(); if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) // если не ищем по айдишнику, значит ищем по диапазону дат { return _source.Orders .Where(x => model.DateFrom <= x.DateCreate.Date && x.DateCreate <= model.DateTo) .Select(x => x.GetViewModel) .ToList(); } if (!model.Id.HasValue && model.ClientId.HasValue) { return _source.Orders .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); } if (!model.Id.HasValue && model.Status.HasValue) { return _source.Orders .Where(x => x.Status == model.Status) .Select(x => x.GetViewModel) .ToList(); } if (model.ImplementerId.HasValue) { var res = _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId); return res != null ? new() { res.GetViewModel } : new(); } foreach (var order in _source.Orders) { if (order.Id == model.Id) { return new() { GetViewModel(order) }; } } return new(); } public List GetFullList() { var result = new List(); foreach (var order in _source.Orders) { result.Add(GetViewModel(order)); } return result; } public OrderViewModel? Insert(OrderBindingModel model) { model.Id = 1; foreach (var order in _source.Orders) { if (model.Id <= order.Id) { model.Id = order.Id + 1; } } var newOrder = Order.Create(model); if (newOrder == null) { return null; } _source.Orders.Add(newOrder); return newOrder.GetViewModel; } public OrderViewModel? Update(OrderBindingModel model) { foreach (var order in _source.Orders) { if (order.Id == model.Id) { order.Update(model); return order.GetViewModel; } } return null; } private OrderViewModel GetViewModel(Order model) { var res = model.GetViewModel; foreach (var pastry in _source.Pastry) { if (pastry.Id == model.PastryId) { res.PastryName = pastry.PastryName; break; } } return res; } } }