public List <OrderViewModel> Read(OrderBindingModel model) { using (var context = new FlowerShopDatabase()) { return(context.Orders .Where( rec => model == null || (rec.Id == model.Id && model.Id.HasValue) || (model.DateFrom.HasValue && model.DateTo.HasValue && rec.DateCreate >= model.DateFrom && rec.DateCreate <= model.DateTo) ) .Select(rec => new OrderViewModel { Id = rec.Id, BouquetId = rec.BouquetId, ClientId = rec.ClientId, ClientFIO = rec.Client.ClientFIO, Count = rec.Count, Sum = rec.Sum, Status = rec.Status, Delivery = rec.Delivery, DateCreate = rec.DateCreate, DateImplement = rec.DateImplement, BouquetName = rec.Bouquet.BouquetName }) .ToList()); } }
public void Delete(RequestBindingModel model) { using (var context = new FlowerShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { context.RequestsFlowers.RemoveRange(context.RequestsFlowers.Where(rec => rec.RequestId == model.Id)); Request element = context.Requests.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Requests.Remove(element); context.SaveChanges(); } else { CheckingElement(element); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(OrderBindingModel model) { using (var context = new FlowerShopDatabase()) { Order element; if (model.Id.HasValue) { element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); CheckingElement(element); } else { element = new Order { }; context.Orders.Add(element); } element.BouquetId = model.BouquetId == 0 ? element.BouquetId : model.BouquetId; element.ClientId = model.ClientId == 0 ? element.ClientId : model.ClientId; element.Count = model.Count; element.Sum = model.Sum; element.Delivery = model.Delivery; element.Status = model.Status; element.DateCreate = model.DateCreate; element.DateImplement = model.DateImplement; context.SaveChanges(); } }
public List <FlowerViewModel> Read(FlowerBindingModel model) { using (var context = new FlowerShopDatabase()) { return(context.Flowers .Where(rec => model == null || rec.Id == model.Id) .Select(rec => new FlowerViewModel { Id = rec.Id, FlowerName = rec.FlowerName, Count = rec.Count, Price = rec.Price }) .ToList()); } }
public void Delete(FlowerBindingModel model) { using (var context = new FlowerShopDatabase()) { Flower element = context.Flowers.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Flowers.Remove(element); context.SaveChanges(); } else { CheckingElement(element); } } }
public List <RequestViewModel> Read(RequestBindingModel model) { using (var context = new FlowerShopDatabase()) { return(context.Requests .Where(rec => model == null || rec.Id == model.Id || rec.DateCreate > model.DateFrom && rec.DateCreate < model.DateTo) .ToList() .Select(rec => new RequestViewModel { Id = rec.Id, RequestName = rec.RequestName, DateCreate = rec.DateCreate, RequestsFlowers = context.RequestsFlowers .Include(recWC => recWC.Flower) .Where(recWC => recWC.RequestId == rec.Id) .ToDictionary(recWC => recWC.FlowerId, recWC => ( recWC.Flower?.FlowerName, recWC.Count )) })
public void FlowersRefill(RequestFlowersBindingModel model) { using (var context = new FlowerShopDatabase()) { RequestFlowers element = context.RequestsFlowers.FirstOrDefault(rec => rec.RequestId == model.RequestId && rec.FlowerId == model.FlowerId); if (element == null) { context.RequestsFlowers.Add(new RequestFlowers { FlowerId = model.FlowerId, RequestId = model.RequestId, Count = model.Count }); } context.Flowers.FirstOrDefault(res => res.Id == model.FlowerId).Count += model.Count; context.SaveChanges(); } }
public List <ClientViewModel> Read(ClientBindingModel model) { using (var context = new FlowerShopDatabase()) { return(context.Clients .Where(rec => model == null || (rec.Id == model.Id) || (rec.Email == model.Email || rec.Email == model.Email) && (model.Password == null || rec.Password == model.Password)) .Select(rec => new ClientViewModel { Id = rec.Id, ClientFIO = rec.ClientFIO, Email = rec.Email, Password = rec.Password }) .ToList()); } }
public void CreateOrUpdate(RequestBindingModel model) { using (var context = new FlowerShopDatabase()) { Request element = context.Requests.FirstOrDefault(rec => rec.RequestName == model.RequestName && rec.Id != model.Id); if (model.Id.HasValue) { element = context.Requests.FirstOrDefault(rec => rec.Id == model.Id); CheckingElement(element); } else { element = new Request(); context.Requests.Add(element); } element.RequestName = model.RequestName; element.DateCreate = model.DateCreate; context.SaveChanges(); } }
public List <BouquetViewModel> Read(BouquetBindingModel model) { using (var context = new FlowerShopDatabase()) { return(context.Bouquets .Where(rec => model == null || rec.Id == model.Id) .ToList() .Select(rec => new BouquetViewModel { Id = rec.Id, BouquetName = rec.BouquetName, Price = rec.Price, FlowerBouquets = context.FlowerBouquets .Include(recPC => recPC.Flower) .Where(recPC => recPC.BouquetId == rec.Id) .ToDictionary(recPC => recPC.FlowerId, recPC => (recPC.Flower?.FlowerName, recPC.Count)), PackagingBouquets = context.PackagingBouquets .Include(recPC => recPC.Packaging) .Where(recPC => recPC.BouquetId == rec.Id) .ToDictionary(recPC => recPC.PackagingId, recPC => (recPC.Packaging?.PackagingName, recPC.Count)) })
public void CreateOrUpdate(FlowerBindingModel model) { using (var context = new FlowerShopDatabase()) { Flower element = context.Flowers.FirstOrDefault(rec => rec.FlowerName == model.FlowerName && rec.Id != model.Id); CheckingUniqueness(element); if (model.Id.HasValue) { element = context.Flowers.FirstOrDefault(rec => rec.Id == model.Id); CheckingElement(element); } else { element = new Flower(); context.Flowers.Add(element); } element.Count = model.Count; element.FlowerName = model.FlowerName; element.Price = model.Price; context.SaveChanges(); } }
public void CreateOrUpdate(ClientBindingModel model) { using (var context = new FlowerShopDatabase()) { Client element = model.Id.HasValue ? null : new Client(); if (model.Id.HasValue) { element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id); CheckingElement(element); } else { element = new Client(); context.Clients.Add(element); } element.ClientFIO = model.ClientFIO; element.Password = model.Password; element.Email = model.Email ?? element.Email; context.SaveChanges(); } }
public void DeletePackagingBouquets(BouquetBindingModel model) { using (var context = new FlowerShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { if (model.Id.HasValue) { context.PackagingBouquets.RemoveRange(context.PackagingBouquets.Where(rec => rec.BouquetId == model.Id)); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void CreateOrUpdate(BouquetBindingModel model) { using (var context = new FlowerShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Bouquet element = context.Bouquets.FirstOrDefault(rec => rec.BouquetName == model.BouquetName && rec.Id != model.Id); if (model.Id.HasValue) { element = context.Bouquets.FirstOrDefault(rec => rec.Id == model.Id); CheckingElement(element); } else { element = new Bouquet(); context.Bouquets.Add(element); } element.BouquetName = model.BouquetName; element.Price = model.Price; context.SaveChanges(); if (model.Id.HasValue) { var flowerBouquets = context.FlowerBouquets.Where(rec => rec.BouquetId == model.Id.Value).ToList(); var packagingBouquets = context.PackagingBouquets.Where(rec => rec.BouquetId == model.Id.Value).ToList(); // удалили те, которых нет в модели // context.PackagingBouquets.RemoveRange(packagingBouquets.Where(rec => !model.PackagingBouquets.ContainsKey(rec.BouquetId)).ToList()); // context.FlowerBouquets.RemoveRange(flowerBouquets.Where(rec => !model.FlowerBouquets.ContainsKey(rec.BouquetId)).ToList()); context.SaveChanges(); // обновили количество у существующих записей foreach (var updateComponent in flowerBouquets) { updateComponent.Count = model.FlowerBouquets[updateComponent.FlowerId].Item2; model.FlowerBouquets.Remove(updateComponent.FlowerId); } foreach (var updateComponent in packagingBouquets) { updateComponent.Count = model.PackagingBouquets[updateComponent.PackagingId].Item2; model.PackagingBouquets.Remove(updateComponent.PackagingId); } context.SaveChanges(); } // добавили новые foreach (var pc in model.PackagingBouquets) { context.PackagingBouquets.Add(new PackagingBouquet { BouquetId = element.Id, PackagingId = pc.Key, Count = pc.Value.Item2 }); context.SaveChanges(); } foreach (var pc in model.FlowerBouquets) { context.FlowerBouquets.Add(new FlowerBouquet { BouquetId = element.Id, FlowerId = pc.Key, Count = pc.Value.Item2 }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }