private async Task RegisterUserVote(ClaimsPrincipal user, ContentItem contentItem, int rating) { var currentVote = await GetUserVote(user, contentItem); //also update in content item part so part driver does not require extra queiry var voteUpDownPart = contentItem.As <VotingPart>(); if (currentVote != null && (currentVote.Value + rating == 0)) { voteUpDownPart.UserVote = rating; await _votingStore.DeleteAsync(currentVote); // _votingService.RemoveVote(currentVote); } else { if (currentVote != null) { voteUpDownPart.UserVote = rating; await _votingStore.ChangeVoteAsync(currentVote, rating); } else { //add new vote var vote = new VoteItem() { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = rating, Dimension = Constants.DimensionRating, CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); voteUpDownPart.UserVote = rating; } } contentItem.Apply(voteUpDownPart); _session.Save(contentItem); }
public async Task <IActionResult> ApplyRatingAction(string contentItemId, decimal rating, string returnUrl) { var contentItem = await _contentManager.GetAsync(contentItemId, VersionOptions.Published); if (contentItem == null)// || !contentItem.Has(nameof(FollowActionPart)) ) { // return NotFound(); return(Redirect(returnUrl)); } if (User == null) { // return NotFound(); return(Redirect(returnUrl)); //for anon voting /*var anonHostname =HttpContext.Connection.RemoteIpAddress.ToString(); * if (!string.IsNullOrWhiteSpace(HttpContext.Request.Headers["X-Forwarded-For"])) * anonHostname += "-" + HttpContext.Request.Headers["X-Forwarded-For"]; * * var currentVote =(await _votingStore.GetAsync(vote => ((VoteItemIndex)vote).User == "Anonymous" && ((VoteItemIndex)vote).Hostname == anonHostname && vote.CorrelationId == contentItem.ContentItemId)).FirstOrDefault(); * if (rating > 0 && currentVote == null) // anonymous votes are only set once per anonHostname * { * var vote = new VoteItem(){User = "******", * CorrelationId =contentItem?.ContentItemId, * Value = rating, * CreatedUtc = _clock.UtcNow, * // Dimension = Constants.Dimension, * Hostname =anonHostname}; * * * await _votingStore.SaveAsync(vote); * } * // _votingService.Vote(content, "Anonymous", anonHostname, rating);*/ } var currentVote = (await _votingStore.GetAsync(vote => vote.User == User.Identity.Name && vote.CorrelationId == contentItem.ContentItemId && vote.Dimension == Constants.DimensionRating)).FirstOrDefault(); //also update in content item part so part driver does not require extra queiry var ratingPart = contentItem.As <RatingPart>(); if (ratingPart == null) { var newPart = contentItem.Weld(new RatingPart() { UserRating = 0 }); contentItem.Apply(newPart); } var actualRatingPart = contentItem.As <RatingPart>(); actualRatingPart.UserRating = rating; contentItem.Apply(actualRatingPart); string successMessage; if (rating == -1) // clear { if (currentVote != null) { await _votingStore.DeleteAsync(currentVote); } } else // vote!! { if (currentVote != null) { await _votingStore.ChangeVoteAsync(currentVote, Convert.ToDouble(rating)); } // _session.Save(contentItem); // await _votingStore.SaveAsync(vote); else { //add new vote var vote = new VoteItem() { User = User.Identity.Name, CorrelationId = contentItem?.ContentItemId, Value = Convert.ToDouble(rating), Dimension = Constants.DimensionRating, CreatedUtc = _clock.UtcNow, Hostname = _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString() }; await _votingStore.SaveAsync(vote); } // _votingService.Vote(content, currentUser.UserName, HttpContext.Request.UserHostAddress, rating); } successMessage = "Added to Rating."; //favoritePart.IsFavorite = true; // _votingStore.Vote(content, currentUser.UserName, _httpContextAccessor.Current().Request.UserHostAddress, 1, Constants.Dimension); // contentItem.Apply(actualRatingPart); _session.Save(contentItem); _notifier.Success(T[successMessage]); if (returnUrl == null) { // return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId } }); return(LocalRedirect("/")); } // else if (stayOnSamePage) // { // return RedirectToAction("Edit", new RouteValueDictionary { { "ContentItemId", contentItem.ContentItemId }, { "returnUrl", returnUrl } }); // } return(LocalRedirect(returnUrl)); }