using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.SearchModels; using BeautySaloonContracts.StoragesContracts; using BeautySaloonContracts.ViewModels; namespace BeautySaloonDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { public OrderViewModel? Delete(OrderBindingModel model) { using var context = new NewdbContext(); var element = context.Orders .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Orders.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public OrderViewModel? GetElement(OrderSearchModel model) { using var context = new NewdbContext(); if (model.Id.HasValue) return context.Orders .FirstOrDefault(x => x.Id == model.Id)? .GetViewModel; return null; } public List GetFilteredList(OrderSearchModel model) { using var context = new NewdbContext(); if (model.ClientId.HasValue) return context.Orders .Where(x => x.ClientId == model.ClientId) .Select(x => x.GetViewModel) .ToList(); if (model.EmployeeId.HasValue) return context.Orders .Where(x => x.EmployeeId == model.EmployeeId) .Select(x => x.GetViewModel) .ToList(); if (model.Date.HasValue) return context.Orders .Where(x => x.Date == model.Date) .Select(x => x.GetViewModel) .ToList(); return new(); } public List GetFullList() { using var context = new NewdbContext(); return context.Orders .Select(x => x.GetViewModel) .ToList(); } public OrderViewModel? Insert(OrderBindingModel model) { var newOrder = Order.Create(model); if (newOrder == null) { return null; } using var context = new NewdbContext(); context.Orders.Add(newOrder); context.SaveChanges(); return newOrder.GetViewModel; } } }