public void Delete(DishBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { context.DishProducts.RemoveRange(context.DishProducts.Where(rec => rec.DishId == model.Id)); Dish dish = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id); if (dish != null) { context.Dishes.Remove(dish); context.SaveChanges(); } else { throw new Exception("Блюдо не найдено"); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public async Task <ActionResult <Dish> > PostDish(DishBindingModel dish) { //Authorization string usertype = User.Claims.First(c => c.Type == "Role").Value; if (usertype.Equals(EntityConstants.Role_SuperAdmin) || usertype.Equals(EntityConstants.Role_Admin)) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } // Get Image Objects from DB according to the ImageID array recieved List <Image> All_Images = new List <Image>(); foreach (var imageId in dish.Images) { if (imageId != null) { var imagedata = await _context.Images.FindAsync(imageId); if (imagedata != null) { Image image_ = new Image { Name = imagedata.Name, Path = imagedata.Path }; All_Images.Add(image_); } } } ; //Create new Dish object Dish new_dish = new Dish() { DishName = dish.DishName, DishDescrition = dish.DishDescription, DishPrice = dish.DishPrice, Images = All_Images }; //Add to DB _context.Dishes.Add(new_dish); await _context.SaveChangesAsync(); return(CreatedAtAction("GetDish", new { id = new_dish.DishId }, dish)); } return(Unauthorized()); }
public void AddElement(DishBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { Dish dish = context.Dishes.FirstOrDefault(rec => rec.Name == model.Name); if (dish != null) { throw new Exception("Такое блюдо существует"); } dish = new Dish { Name = model.Name, Description = model.Description, Price = model.Price }; context.Dishes.Add(dish); context.SaveChanges(); var groupProducts = model.DishProducts .GroupBy(rec => rec.ProductId) .Select(rec => new { ProductId = rec.Key, Count = rec.Sum(r => r.Count) } ); foreach (var groupProduct in groupProducts) { context.DishProducts.Add( new DishProduct { DishId = dish.Id, ProductId = groupProduct.ProductId, Count = groupProduct.Count } ); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }
public List <DishViewModel> Read(DishBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { return(context.Dishes .Where(rec => model == null || rec.Id == model.Id) .ToList() .Select(rec => new DishViewModel { Id = rec.Id, DishName = rec.DishName, DishType = rec.DishType, Price = rec.Price, DishProducts = context.DishProducts .Include(recDP => recDP.Product) .Where(recDP => recDP.DishId == rec.Id) .ToDictionary(recDP => recDP.ProductId, recDP => (recDP.Product?.ProductName, recDP.Weight)) })
public void AddElement(DishBindingModel model) { int maxId = 0; for (int i = 0; i < source.Dishs.Count; ++i) { if (source.Dishs[i].Id > maxId) { maxId = source.Dishs[i].Id; } if (source.Dishs[i].DishName == model.DishName) { throw new Exception("Уже есть компонент с таким названием"); } } source.Dishs.Add(new Dish { Id = maxId + 1, DishName = model.DishName }); }
public void UpdElement(DishBindingModel model) { int index = -1; for (int i = 0; i < source.Dishs.Count; ++i) { if (source.Dishs[i].Id == model.Id) { index = i; } if (source.Dishs[i].DishName == model.DishName && source.Dishs[i].Id != model.Id) { throw new Exception("Уже есть компонент с таким названием"); } } if (index == -1) { throw new Exception("Элемент не найден"); } source.Dishs[index].DishName = model.DishName; }
public void CreateOrUpdate(DishBindingModel model) { using (var context = new AbstractSweetShopDatabase()) { using (var transaction = context.Database.BeginTransaction()) { try { Dish tempDish = context.Dishes.FirstOrDefault(rec => rec.DishName == model.DishName && rec.Id != model.Id); if (tempDish != null) { throw new Exception("Уже есть блюдо с таким названием"); } if (model.Id.HasValue) { tempDish = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id); if (tempDish == null) { throw new Exception("Блюдо не найдено"); } } else { tempDish = new Dish(); context.Dishes.Add(tempDish); } tempDish.DishName = model.DishName; tempDish.Price = model.Price; tempDish.DishType = model.DishType; context.SaveChanges(); if (model.Id.HasValue) { var dishProducts = context.DishProducts.Where(rec => rec.DishId == model.Id.Value).ToList(); // удалили те, которых нет в модели context.DishProducts.RemoveRange(dishProducts.Where(rec => !model.DishProducts.ContainsKey(rec.ProductId)).ToList()); context.SaveChanges(); dishProducts = context.DishProducts.Where(rec => rec.DishId == model.Id.Value).ToList(); // обновили количество у существующих записей foreach (var updateProduct in dishProducts) { updateProduct.Weight = model.DishProducts[updateProduct.ProductId].Item2; model.DishProducts.Remove(updateProduct.ProductId); } context.SaveChanges(); } // добавили новые foreach (var dp in model.DishProducts) { context.DishProducts.Add(new DishProduct { DishId = tempDish.Id, ProductId = dp.Key, Weight = dp.Value.Item2 }); context.SaveChanges(); } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } } }
public void UpdElement(DishBindingModel model) { using (var transaction = context.Database.BeginTransaction()) { try { Dish dish = context.Dishes.FirstOrDefault(rec => rec.Name == model.Name && rec.Id != model.Id); if (dish != null) { throw new Exception("Такое блюдо существует"); } dish = context.Dishes.FirstOrDefault(rec => rec.Id == model.Id); if (dish == null) { throw new Exception("Блюдо не найдено"); } dish.Name = model.Name; dish.Price = model.Price; dish.Description = model.Description; context.SaveChanges(); var compIds = model.DishProducts.Select(rec => rec.ProductId).Distinct(); var updateProducts = context.DishProducts.Where(rec => rec.DishId == model.Id && compIds.Contains(rec.ProductId)); foreach (var updateProduct in updateProducts) { updateProduct.Count = model.DishProducts.FirstOrDefault(rec => rec.Id == updateProduct.Id).Count; } context.SaveChanges(); context.DishProducts.RemoveRange(context.DishProducts.Where(rec => rec.DishId == model.Id && !compIds.Contains(rec.ProductId))); context.SaveChanges(); var groupProducts = model.DishProducts .Where(rec => rec.Id == 0) .GroupBy(rec => rec.ProductId) .Select(rec => new { ProductId = rec.Key, Count = rec.Sum(r => r.Count) } ); foreach (var groupProduct in groupProducts) { DishProduct dishProduct = context.DishProducts.FirstOrDefault(rec => rec.DishId == model.Id && rec.ProductId == groupProduct.ProductId); if (dishProduct != null) { dishProduct.Count += groupProduct.Count; context.SaveChanges(); } else { context.DishProducts.Add( new DishProduct { DishId = model.Id, ProductId = groupProduct.ProductId, Count = groupProduct.Count } ); context.SaveChanges(); } } transaction.Commit(); } catch (Exception) { transaction.Rollback(); throw; } } }