public async Task <IEnumerable <CocktailUserCommentDTO> > GetAllAsync(Guid cocktailId, string page, string itemsOnPage)
        {
            try
            {
                var p        = int.Parse(page);
                var item     = int.Parse(itemsOnPage);
                var comments = await this._context.CocktailComments
                               .Include(c => c.Cocktail)
                               .Include(c => c.User)
                               .Where(c => c.CocktailId == cocktailId)
                               .Skip(p * item)
                               .Take(item)
                               .ToListAsync();

                var commentsDTO = comments.Select(c => _mapper.MapEntityToDTO(c));

                return(commentsDTO);
            }
            catch (Exception)
            {
                return(new List <CocktailUserCommentDTO>());
            }
        }
示例#2
0
        /// <summary>
        /// User comments a cocktail
        /// </summary>
        /// <param name="commentDTO">Comment DTO </param>
        /// <param name="cocktailId">Cocktail Id that is commented</param>
        /// <param name="userId">User Id that leaves the comment</param>
        /// <returns>The comment, DTO</returns>
        public async Task <CocktailUserCommentDTO> AddCocktailComment(CocktailUserCommentDTO commentDTO, Guid cocktailId, Guid userId)
        {
            var cocktail = await _context.Cocktails
                           .FirstOrDefaultAsync(x => x.Id == cocktailId);

            commentDTO.UserId = userId;
            var comment = _cocktailCommentMapper.MapDTOToEntity(commentDTO);

            await _context.CocktailComments.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(_cocktailCommentMapper.MapEntityToDTO(comment));
        }