public async Task <Response> Rate(CreateCommentRatingDto createCommentRatingDto) { var comment = await _context.Comments .Include(c => c.Thread) .SingleOrDefaultAsync(c => c.CommentId == createCommentRatingDto.CommentId); //var existingCommentRating = await _context.CommentRatings // .Include(cr => cr.Comment) // .Include(cr => cr.User) // .SingleOrDefaultAsync(cr => // cr.Comment.CommentId == createCommentRatingDto.CommentId // && cr.User.Id == _userService.GetUserId()); if (!Enum.IsDefined(typeof(Rating), createCommentRatingDto.Type)) { return(Respond().WithError("*", "Invalid Rating Type")); } //if (existingCommentRating != null) //{ // //user already rated this comment, either change the rating type or do nothing // if (existingCommentRating.Type == createCommentRatingDto.Type) // { // return Respond().WithError("*", "Rating already exists"); // } // existingCommentRating.Type = createCommentRatingDto.Type; // await _context.SaveChangesAsync(); // return Respond().WithSuccess(); //} var commentRatingEntity = _mapper.Map <CommentRating>(createCommentRatingDto); commentRatingEntity.CreatedOn = DateTime.UtcNow; //commentRatingEntity.User = await _userService.GetUser(); comment.CommentRatings.Add(commentRatingEntity); await _context.SaveChangesAsync(); ClearCacheKeysForThread(comment.Thread.ThreadId); return(Respond().WithSuccess()); }
//[Authorize] public async Task <IActionResult> Rate(CreateCommentRatingDto createCommentRatingDto) { return(this.GenerateResponse(await _commentService.Rate(createCommentRatingDto))); }