/// <summary> /// Deletes bar record /// </summary> /// <param name="id">Bar Id, Guid</param> /// <returns>True on success</returns> public async Task <bool> DeleteAsync(Guid id) { var bar = await _context.Bars.FindAsync(id); if (bar == null) { return(false); } bar.IsDeleted = true; _context.Update(bar); await _context.SaveChangesAsync(); return(true); }
/// <summary> /// Updates existing ingredient /// </summary> /// <param name="id">ID of the ingredient</param> /// <param name="ingredientDTO">The Ingredient DTO model with the new properties.</param> /// <returns>The updated ingredient, DTO</returns> public async Task <IngredientDTO> UpdateAsync(Guid id, IngredientDTO ingredientDTO) { var ingredient = await _context.Ingredients.FindAsync(id); if (ingredient == null) { return(null); } try { _context.Entry(ingredient).State = EntityState.Modified; ingredient.Name = ingredientDTO.Name; ingredient.IsAlcoholic = ingredientDTO.IsAlcoholic; _context.Update(ingredient); await _context.SaveChangesAsync(); } catch (Exception) { //TODO: think what to return and when? if (!IngredientExists(id)) { return(null); } else { throw new ArgumentException(); } } return(_mapper.MapEntityToDTO(ingredient)); }
/// <summary> /// Marks cocktail as IsDeleted /// </summary> /// <param name="id">ID of the cocktail</param> /// <returns>True when successful, false otherwise</returns> public async Task <bool> DeleteAsync(Guid id) { var cocktail = await this._context.Cocktails.FindAsync(id); if (cocktail == null) { return(false); } cocktail.IsDeleted = true; _context.Update(cocktail); await this._context.SaveChangesAsync(); return(true); }
public async Task <BarUserCommentDTO> UpdateAsync(BarUserCommentDTO commentDTO) { try { var comment = await this._context.BarComments .Include(c => c.Bar) .Include(c => c.User) .FirstOrDefaultAsync(c => c.BarId == commentDTO.BarId && c.UserId == commentDTO.UserId); comment.Text = commentDTO.Text; comment.IsFlagged = commentDTO.IsFlagged; _context.Update(comment); await _context.SaveChangesAsync(); return(commentDTO); } catch (Exception) { return(new BarUserCommentDTO()); } }
/// <summary> /// Creates new cocktail in the database. /// </summary> /// <param name="cocktailDTO">Cocktail DTO model.</param> /// <returns>The created cocktail.</returns> public async Task <CocktailDTO> CreateAsync(CocktailDTO cocktailDTO) { try { //TODO: Check if exists and recover if (await CocktailExistsByNameAsync(cocktailDTO.Name)) { var theCocktail = await _context.Cocktails .FirstOrDefaultAsync(c => c.Name.Equals(cocktailDTO.Name)); if (theCocktail.IsDeleted == true) { theCocktail.IsDeleted = false; } _context.Cocktails.Update(theCocktail); await _context.SaveChangesAsync(); return(_mapper.MapEntityToDTO(theCocktail)); } else { var cocktail = _mapper.MapDTOToEntity(cocktailDTO); _context.Cocktails.Add(cocktail); await _context.SaveChangesAsync(); cocktail = await _context.Cocktails .Include(c => c.Ingredients) .FirstOrDefaultAsync(c => c.Name.Equals(cocktailDTO.Name)); foreach (var item in cocktailDTO.Ingredients) { if (item.IngredientId != Guid.Empty) { bool isAdded = await AddIngredientsToCocktail(cocktail, item.IngredientId, item.Parts); if (isAdded == false) { throw new OperationCanceledException("Adding cocktail ingredient failed."); } } } if (cocktail.Ingredients.Select(x => x.Ingredient).Any(i => i.IsAlcoholic)) { cocktail.IsAlcoholic = true; } else { cocktail.IsAlcoholic = false; } _context.Update(cocktail); await _context.SaveChangesAsync(); cocktail = await _context.Cocktails.FirstOrDefaultAsync(x => x.Name.ToLower() == cocktailDTO.Name.ToLower()); return(_mapper.MapEntityToDTO(cocktail)); } } catch (Exception e) { throw new OperationCanceledException("Fail to create cocktail"); } }