public async Task <VotesDto> VoteAsync(VotesDto vote) { string userId = _securityService.GetUserId(); Rating rating = _dbContext.Ratings .Where(x => x.User.UserId == userId && x.Question.Id == vote.IdQuestion) .SingleOrDefault(); if (rating == null) { Question question = await _dbContext.FindAsync <Question>(vote.IdQuestion); CranUser user = await _userService.GetOrCreateCranUserAsync(); rating = new Rating { Question = question, User = user, QuestionRating = 0, }; _dbContext.Ratings.Add(rating); } int r = vote.MyVote > 0 ? 1 : (vote.MyVote < 0 ? -1 : 0); rating.QuestionRating = r; await _dbContext.SaveChangesAsync(); vote = await GetVoteAsync(vote.IdQuestion); return(vote); }
public IActionResult Vote([FromBody] VotesDto voteDto) { // map dto to entity var vote = Mapper.Map <Vote>(voteDto); try { // save AddToKeylessTable.AddToTable_Vote(vote.IdUser, vote.IdCandidate); return(Ok()); } catch (Exception ex) { // return error message if there was an exception return(BadRequest(new { message = ex.Message })); } }
public async Task <VotesDto> GetVoteAsync(int idQuestion) { int upRatings = await _dbContext.Ratings.Where(x => x.Question.Id == idQuestion && x.QuestionRating > 0) .CountAsync(); int downRatings = await _dbContext.Ratings.Where(x => x.Question.Id == idQuestion && x.QuestionRating < 0) .CountAsync(); string userId = this._securityService.GetUserId(); int? myRating = await _dbContext.Ratings.Where(x => x.User.UserId == userId && x.Question.Id == idQuestion) .Select(x => x.QuestionRating).SingleOrDefaultAsync(); VotesDto result = new VotesDto() { IdQuestion = idQuestion, MyVote = myRating ?? 0, DownVotes = downRatings, UpVotes = upRatings, }; return(result); }
public async Task <VotesDto> Vote([FromBody] VotesDto vote) { return(await _commentsService.VoteAsync(vote)); }