public async Task <bool> Update(Recipe recipe, NewRecipeDto newRecipe) { var findRecipe = await _dataContext.Recipe.FirstOrDefaultAsync(x => x.Id.Equals(recipe.Id)); if (findRecipe == null) { throw new InvalidOperationException($"Item {recipe.Id} was not found"); } findRecipe.Description = newRecipe.Description; findRecipe.Title = newRecipe.Title; findRecipe.Time = newRecipe.Time; findRecipe.Servings = newRecipe.Servings; findRecipe.Image = newRecipe.Image; var products = _dataContext.Product.Where(x => x.FkRecipe.Equals(recipe.Id)).ToList(); foreach (var item in products) { _dataContext.Product.Remove(item); } foreach (var item in newRecipe.Products) { var product = new Product { Name = item.Name, Quantity = item.Quantity, FkRecipe = recipe.Id }; await _dataContext.Product.AddAsync(product); } _dataContext.Update(findRecipe); await _dataContext.SaveChangesAsync(); return(true); }
public async Task <bool> Update(int id, NewProductDto[] newProducts) { var findProducts = await _dataContext.Product.Where(x => x.FkRecipe.Equals(id)).ToListAsync(); if (findProducts.Count == 0 || findProducts == null) { throw new ArgumentNullException(); } foreach (var item in findProducts) { var findProduct = await _dataContext.Product.FirstOrDefaultAsync(x => x.Id.Equals(item.Id)); foreach (var a in newProducts) { findProduct.Name = a.Name; findProduct.Quantity = a.Quantity; } _dataContext.Update(findProduct); } await _dataContext.SaveChangesAsync(); return(true); }
public async Task <bool> Update(int id, Event newEv) { var ev = await _dataContext.Event.FirstOrDefaultAsync(x => x.Id.Equals(id)); if (ev == null) { throw new InvalidOperationException($"Item {id} was not found"); } if (newEv.Title != null || newEv.Title != "") { ev.Title = newEv.Title; } if (newEv.EventStartDate != null) { ev.EventStartDate = newEv.EventStartDate; } if (newEv.EventEndDate != null) { ev.EventEndDate = newEv.EventEndDate; } if (newEv.PeopleCount > 0) { ev.PeopleCount = newEv.PeopleCount; } _dataContext.Update(ev); await _dataContext.SaveChangesAsync(); return(true); }
public async Task <bool> Update(Comment comment, CommentDto newComment) { var findComment = await _dataContext.Comment.FirstOrDefaultAsync(x => x.Id.Equals(comment.Id)); if (findComment == null) { throw new InvalidOperationException($"Item {comment.Id} was not found"); } findComment.TextField = newComment.TextField; _dataContext.Update(findComment); await _dataContext.SaveChangesAsync(); return(true); }
public async Task <User> Update(User user, UserDto newUser) { var findUser = await _dataContext.User.FirstOrDefaultAsync(x => x.Id.Equals(user.Id)); if (findUser == null) { throw new InvalidOperationException($"User id: {user.Id} was not found"); } findUser.Name = newUser.Name; findUser.Surname = newUser.Surname; findUser.Email = newUser.Email; findUser.Photo = newUser.Photo; findUser.IsAdmin = newUser.IsAdmin; _dataContext.Update(findUser); await _dataContext.SaveChangesAsync(); return(findUser); }