/// <summary> /// User rates a cocktail /// </summary> /// <param name="userId">User Id, Guid</param> /// <param name="cocktailId">Cocktail Id, Guid</param> /// <param name="theRating">Rating, 1 to 5 </param> /// <returns>The cocktail rated, DTO</returns> public async Task <CocktailDTO> RateCocktail(int theRating, Guid cocktailId, Guid userId) { var user = await _context.Users .FirstOrDefaultAsync(u => u.Id == userId); var cocktail = await _context.Cocktails .FirstOrDefaultAsync(c => c.Id == cocktailId); var existingRating = await _context.CocktailRatings .FirstOrDefaultAsync(r => r.UserId == userId && r.CocktailId == cocktailId); //bool isRated = this._context.CocktailRatings // .Select(r => r.UserId == userId && r.CocktailId == cocktailId) // .Count() == 1; if (existingRating != null) { existingRating.Rating = theRating; _context.CocktailRatings.Update(existingRating); } else { var theNewRating = new UserCocktailRating { CocktailId = cocktailId, Cocktail = cocktail, UserId = userId, User = user, Rating = theRating, }; cocktail.TimesRated += 1; _context.CocktailRatings.Add(theNewRating); } try { //Saving before recalculating for it to be correct. await _context.SaveChangesAsync(); } catch (Exception) { return(null); } cocktail.Rating = await RecalculateCocktailRatingAsync(cocktail.Id); try { _context.Cocktails.Update(cocktail); await _context.SaveChangesAsync(); } catch (Exception) { return(null); } return(_cocktailMapper.MapEntityToDTO(cocktail)); }
/// <summary> /// Gets all cocktails from the database. /// </summary> /// <returns>List of cocktails, DTOs</returns> public async Task <IEnumerable <CocktailDTO> > GetAllAsync(string page, string itemsOnPage) { var cocktails = await _context.Cocktails .Include(c => c.Ingredients) .ThenInclude(c => c.Ingredient) .Include(c => c.Comments) .ThenInclude(c => c.User) .Include(c => c.Bars) .ToListAsync(); return(cocktails.Select(x => _mapper.MapEntityToDTO(x)).ToList()); }
/// <summary> /// Get a list of cocktails offerred in a bar with a search constraint /// </summary> /// <param name="id">Bar Id to be checked</param> /// <param name="page">Number of viewable page</param> /// <param name="itemsOnPage">Number of cocktails to be shown</param> /// <param name="search">Additional search constraints</param> /// <returns>Collection of CocktailDTO</returns> public async Task <IEnumerable <CocktailDTO> > GetCocktailsAsync(Guid id, string page, string itemsOnPage, string search, bool access) { try { var cocktails = await _context.CocktailBars .Include(c => c.Cocktail) .ThenInclude(c => c.Ingredients) .ThenInclude(i => i.Ingredient) .Include(c => c.Cocktail) .ThenInclude(c => c.Comments) .ThenInclude(c => c.User) .Include(c => c.Cocktail) .ThenInclude(c => c.CocktailRatings) .ThenInclude(r => r.User) .Include(c => c.Cocktail) .ThenInclude(c => c.Bars) .ThenInclude(c => c.Bar) .Include(c => c.Bar) .ThenInclude(b => b.Location) .Where(c => c.BarId == id) .Select(c => c.Cocktail) .ToListAsync(); if (!access) { cocktails = cocktails .Where(b => b.IsDeleted == false).ToList(); } ; var p = int.Parse(page); var item = int.Parse(itemsOnPage); cocktails = cocktails.Skip(p * item).Take(item).ToList(); return(cocktails.Select(x => _cocktailMapper.MapEntityToDTO(x))); } catch (Exception) { throw new ArgumentException("Failed to get cocktails"); } }