public async Task <ActionResult <VoteResponseModel> > Index(VoteInputModel input) { if (this.ModelState.IsValid) { if (input == null) { return(this.BadRequest()); } var userId = this.UserManager.GetUserId(this.User); if (string.IsNullOrEmpty(userId)) { return(this.BadRequest()); } if (string.IsNullOrEmpty(input.ImageId)) { return(this.BadRequest()); } var dbImage = this.ImagesService.GetImageById <Image>(input.ImageId); if (dbImage == null) { return(this.BadRequest()); } bool result = await this.VotesService.VoteAsync(input.ImageId, userId, input.IsLike); return(new VoteResponseModel { Result = result }); } return(this.View()); }
public async Task <ActionResult <VoteResultsModel> > Vote(VoteInputModel input) { var userId = this.userManager.GetUserId(this.User); var result = new VoteResultsModel(); if (input.DiscussionId != default) { await this.voteServices.VoteDiscussionAsync(userId, input.DiscussionId, input.VoteType); var votesDto = this.voteServices.GetDiscussionVotes(input.DiscussionId); result.Likes = votesDto.Likes; result.Dislikes = votesDto.Dislikes; } else if (input.CommentId != default) { await this.voteServices.VoteCommensAsync(userId, input.CommentId, input.VoteType); var votesDto = this.voteServices.GetCommentVotes(input.CommentId); result.Likes = votesDto.Likes; result.Dislikes = votesDto.Dislikes; } return(result); }
public ActionResult Vote(VoteInputModel model) { if (model != null && this.ModelState.IsValid) { var userId = this.User.Identity.GetUserId(); if (!this.Data.Votes.All().Any(x => x.TeamId == model.TeamId && x.UserId == userId)) { var vote = new Vote() { TeamId = model.TeamId, UserId = userId, Value = 1 }; this.Data.Votes.Add(vote); this.Data.SaveChanges(); var newVotes = this.Data.Votes.All().Where(x => x.TeamId == model.TeamId) .Sum(x => x.Value); return(this.Json(newVotes)); } } var votes = this.Data.Votes.All().Where(x => x.TeamId == model.TeamId) .Sum(x => x.Value); return(this.Json(votes)); }
public IActionResult VoteMod([FromBody] VoteInputModel model) { var currentUser = this.userService.GetUserByName(this.User.Identity.Name); var mod = this.modService.GetById(model.ModId); if (model.Value == true) { var vote = new Vote { UserId = currentUser.Id, ModId = model.ModId, }; this.voteService.Create(vote); } else { var vote = this.voteService.GetVoteOfUser(model.ModId, currentUser.Id); this.voteService.Delete(vote); } return(new JsonResult(mod.VoteCount)); // return this.RedirectToAction(nameof(this.PostDetails), new { Id = model.Id }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel model) { var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(model.CommentId, userId, model.Type); this.votesService.GetVotes(model.CommentId); return(this.Redirect($"/News/ById/26")); }
public async Task <ActionResult <int> > Post(VoteInputModel input) { var user = await this.userManager.GetUserAsync(this.User); await this.voteService.VoteAsync(input.ArticleId, input.IsUpVote, user.Id); var votes = this.voteService.GetVotes(input.ArticleId); return(votes); }
public async Task <ActionResult <VoteOutputModel> > Post(VoteInputModel input) { var userId = this.userManager.GetUserId(this.User); await this.votesService.CreateVoteAsync(input.VotedObject, input.VotedObjectId, input.IsUpVote, userId); var result = this.votesService.GetVotesCount(input.VotedObject, input.VotedObjectId); return(result); }
public async Task<ActionResult<VoteResponseModel>> Post(VoteInputModel model) { var user = await this.userManaganer.GetUserAsync(this.User); await this.voteService.VoteAsync(model.NewsFeedPostId, user.Id, model.IsUpVote); var upVotes = this.voteService.GetUpVotes(model.NewsFeedPostId); var downVotes = this.voteService.GetDownVotes(model.NewsFeedPostId); return new VoteResponseModel { UpVotes = upVotes, DownVotes = downVotes }; }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { var userId = _userManager.GetUserId(User); await _votesService.VoteAsync(input.CommentId, userId, input.IsUpVote); var votesScore = _votesService.GetVotes(input.CommentId); return(new VoteResponseModel { VotesCount = votesScore }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel inputModel) { string userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(inputModel.PostId, userId, inputModel.IsUpVote); int votes = this.votesService.VotesCount(inputModel.PostId); return(new VoteResponseModel { VotesCount = votes, }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); await this.votesService.VoteAsync(input.PostId, userId, input.IsUpVote); var votes = this.votesService.GetVotes(input.PostId); return(new VoteResponseModel { VotesCount = votes }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { var user = await this.userManager.GetUserAsync(this.User); await this.votesService.VoteAsync(input.ArticleId, user.Id, input.StarsCount); var averageStarsVote = this.votesService.GetAverageStarsFromVotes(input.ArticleId); return(new VoteResponseModel { AverageStarsVote = averageStarsVote }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(input.PostId, userId, input.IsUpVote); var votes = this.votesService.GetVotes(input.PostId); return(new VoteResponseModel { VotesCount = votes }); }
public async Task <ActionResult <VotesResponseModel> > Post(VoteInputModel model) { var userId = this.manager.GetUserId(this.User); await this.service.VoteAsync(userId, model.CommentId, model.IsUpvote); var votes = this.service.GetVotes(model.CommentId); return(new VotesResponseModel { VotesCount = votes, }); }
public async Task <ActionResult <VoteResponseModel> > VotePost(VoteInputModel input) { var user = await this.usermanager.GetUserAsync(this.User); await this.votesService.VoteAsync(input.PostId, user.Id, input.IsUpVote); var votes = new VoteResponseModel() { VotesCount = this.votesService.GetVotes(input.PostId), }; return(votes); }
public async Task <ActionResult <VoteOutputModel> > Post(VoteInputModel model) { var winner = await _db.SortObject.FirstOrDefaultAsync(u => u.Name == model.Input[0]); var loser = await _db.SortObject.FirstOrDefaultAsync(u => u.Name == model.Input[1]); if (winner == null) { SortObject temp = new SortObject(); temp.Rating = 1500; temp.Name = model.Input[0]; await _db.AddAsync(temp); await _db.SaveChangesAsync(); winner = await _db.SortObject.FirstOrDefaultAsync(u => u.Name == model.Input[0]); } if (loser == null) { SortObject temp = new SortObject(); temp.Rating = 1500; temp.Name = model.Input[1]; await _db.AddAsync(temp); await _db.SaveChangesAsync(); loser = await _db.SortObject.FirstOrDefaultAsync(u => u.Name == model.Input[1]); } double coef = 2.0; double winner_rtg = winner.Rating; double loser_rtg = loser.Rating; double p = prob(winner_rtg - loser_rtg, coef); double winner_delta = kfactor * p; double loser_delta = -kfactor * p; double winner_newrtg = winner_rtg + winner_delta; double loser_newrtg = loser_rtg + loser_delta; winner.Rating = winner_newrtg; loser.Rating = loser_newrtg; winner.Wins++; loser.Losses++; // Change ratings await _db.SaveChangesAsync(); return(new VoteOutputModel() { Response = winner.Name + ": " + Math.Floor(winner_newrtg) + " (+" + Math.Floor(winner_delta) + ")" + ";" + loser.Name + ": " + Math.Floor(loser_newrtg) + " (" + Math.Floor(loser_delta) + ")" }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { string userId = this.userManager.GetUserId(this.User); if (userId == null) { return(this.Unauthorized()); } Vote vote = this.dbContext.Votes.FirstOrDefault(v => v.ArticleId == input.ArticleId && v.AuthorId == userId); VoteType voteType = input.IsUp ? VoteType.Up : VoteType.Down; var responseModel = new VoteResponseModel { IsUpvoted = input.IsUp, IsDownvoted = !input.IsUp, }; if (vote == null) { vote = new Vote { Type = voteType, ArticleId = input.ArticleId, AuthorId = userId, }; this.dbContext.Votes.Add(vote); } else { if (vote.Type == VoteType.Up && input.IsUp || vote.Type == VoteType.Down && !input.IsUp) { vote.Type = VoteType.None; responseModel.IsUpvoted = false; responseModel.IsDownvoted = false; } else { vote.Type = voteType; } } await this.dbContext.SaveChangesAsync(); responseModel.Upvotes = this.articlesService.GetUpvotesCount(input.ArticleId); responseModel.Downvotes = this.articlesService.GetDownvotesCount(input.ArticleId); return(responseModel); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel inputModel) { var user = await this.userManager.GetUserAsync(this.User); await this.votesService.VoteAsync(inputModel.DiseaseId, user.Id, inputModel.IsUpVote); var votes = this.votesService.GetVotes(inputModel.DiseaseId); VoteResponseModel voteResponseModel = new VoteResponseModel { VotesCount = votes, }; return(voteResponseModel); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(input.VideoId, userId, input.IsUpVote); var viewModel = new VoteResponseModel { UpVotesCount = this.votesService.GetUpVotes(input.VideoId), DownVotesCount = this.votesService.GetDownVotes(input.VideoId), }; return(viewModel); }
public async Task <ActionResult <VoteResponseViewModel> > Post(VoteInputModel voteInputModel) { var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value; var votesServiceModel = AutoMapperConfig.MapperInstance.Map <VotesServiceModel>(voteInputModel); votesServiceModel.UserId = userId; await this.votesService.CreateVoteAsync(votesServiceModel); var votes = await this.votesService.GetVotesAsync <int>(voteInputModel.ServiceId); return(new VoteResponseViewModel { VotesCount = votes }); }
public async Task <ActionResult <VotesCountModel> > Post(VoteInputModel input) { var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(input.AdId, userId, input.IsUpVote); var upVotes = await this.votesService.GetUpVotesAsync(input.AdId); var downVotes = await this.votesService.GetDownVotesAsync(input.AdId); return(new VotesCountModel { UpVotesCount = upVotes, DownVotesCount = downVotes, }); }
public async Task <ActionResult <VoteCounterModel> > Vote(VoteInputModel inputModel) { var user = await this.userManager.GetUserAsync(this.User); var hasUserVoted = await this.votesService.AddVote(inputModel.RecipeId, user.Id, inputModel.IsUpVote); int positiveVotes = this.votesService.CountVotes(inputModel.RecipeId)[0]; int negativeVotes = this.votesService.CountVotes(inputModel.RecipeId)[1]; return(new VoteCounterModel { PositiveVotes = positiveVotes, NegativeVotes = negativeVotes, HasUserVoted = hasUserVoted, }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel input) { if (!await this.postService.DoesItExist(input.PostId)) { return(this.NotFound()); } var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync(input.PostId, userId, input.IsUpVote); var votes = this.votesService.GetVotesFromPost(input.PostId); return(new VoteResponseModel { VotesCount = votes }); }
public IActionResult Post([FromBody] VoteInputModel inputModel) { if (this.ModelState.IsValid) { // TODO: Prevent user to vote more then once. var email = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var userId = this.users.All().Where(u => u.Email == email).Select(u => u.Id).FirstOrDefault(); var vote = new Vote { LaptopId = inputModel.LaptopId, UserId = userId }; this.votes.Add(vote); this.votes.SaveChanges(); // We don't have view of single comments return(this.Created(string.Empty, vote)); } return(this.BadRequest("Invalid input")); }
public async Task <ActionResult <VoteViewModel> > VoteAsync(VoteInputModel inputModel) { var isPostIdExist = this.postService.IsExist(inputModel.PostId); if (isPostIdExist == false) { return(this.NotFound(inputModel.PostId)); } var user = await this.userManager.GetUserAsync(this.User); await this.voteService.VoteAsync(inputModel.PostId, user.Id.ToString(), inputModel.IsUpVote); var votes = this.voteService.GetVotesCount(inputModel.PostId); return(new VoteViewModel() { VotesCount = votes }); }
public async Task <ActionResult <VoteResponseModel> > Post(VoteInputModel voteInputModel) { if (!this.ModelState.IsValid) { return(this.Redirect("Error")); } var userId = this.userManager.GetUserId(this.User); await this.votesService.VoteAsync( voteInputModel.PostId, userId, voteInputModel.IsUpVote); var votes = this.votesService.GetVotes(voteInputModel.PostId); return(new VoteResponseModel { VotesCount = votes }); }
public async Task <ActionResult> Post(VoteInputModel input) { int data; string message; var userId = this.User .FindFirstValue(ClaimTypes.NameIdentifier); var vote = this.forumVoteService .Get <VoteModel>(input.PostId, userId); if (vote == null) { data = await this.forumVoteService .CreateAsync(input.VoteValue, input.PostId, userId); message = vote.Value switch { 1 => string.Format(Message.SuccessfullVote, VoteType.Up.ToString().ToLower()), -1 => string.Format(Message.SuccessfullVote, VoteType.Down.ToString().ToLower()), _ => ErrorMessage.Default, }; } else { if (input.VoteValue == vote.Value) { return(this.Ok(new { Message = string.Format(ErrorMessage.AlreadyVoted, vote.Type.ToLower()) })); } data = await this.forumVoteService .UpdateAsync(input.VoteValue, vote.Id); message = vote.Value switch { 1 => string.Format(Message.SuccessfullVote, VoteType.Down.ToString().ToLower()), -1 => string.Format(Message.SuccessfullVote, VoteType.Up.ToString().ToLower()), _ => ErrorMessage.Default, }; } return(this.Ok(new { Data = data, Message = message })); }
public async void TestVoteFunctionality() { UserManager <ApplicationUser> userManager = MockUserManager(); var mockVoteService = new Mock <IVotesService>(); mockVoteService.Setup(x => x.VoteAsync(VotesControllerTest.FirstTestImageId, It.IsAny <string>(), true)) .ReturnsAsync(false); mockVoteService.Setup(x => x.VoteAsync(VotesControllerTest.FirstTestImageId, It.IsAny <string>(), false)) .ReturnsAsync(true); var mockImageService = new Mock <IImagesService>(); mockImageService.Setup(x => x.GetImageById <Image>(It.IsAny <string>())) .Returns(new Image() { Id = VotesControllerTest.FirstTestImageId, }); VotesController controller = new VotesController( userManager, mockVoteService.Object, mockImageService.Object).WithIdentity(FirstTestUserId, "TestUser"); VoteInputModel model = new VoteInputModel() { ImageId = VotesControllerTest.FirstTestImageId, IsLike = false, }; ActionResult <VoteResponseModel> result = await controller.Index(model); Assert.True(result.Value.Result); model.IsLike = true; result = await controller.Index(model); Assert.False(result.Value.Result); }
public async Task Post_Should_() { var path = "Api/Votes"; var baseAddress = new Uri("https://localhost:5005"); var client = new HttpClient(); client.BaseAddress = baseAddress; var viewModel = new VoteInputModel(); viewModel.PostId = 1; viewModel.IsUpVote = true; var viewModelAsJSON = JsonConvert.SerializeObject(viewModel); var buffer = System.Text.Encoding.UTF8.GetBytes(viewModelAsJSON); var byteContent = new ByteArrayContent(buffer); byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = await client.PostAsync(path, byteContent); }
public ActionResult Vote(VoteInputModel model) { if (this.Data.Votes.All().Any(x => x.TeamId == model.TeamId && x.UserId == model.UserId)) { this.ModelState.AddModelError(string.Empty, "The user has already vote for this team."); } if (model != null && this.ModelState.IsValid) { var vote = Mapper.Map <Vote>(model); vote.Value = 1; this.Data.Votes.Add(vote); this.Data.SaveChanges(); var votesCount = this.Data.Votes .All() .Where(x => x.TeamId == model.TeamId) .Sum(x => x.Value); return(this.Content(votesCount.ToString())); } return(this.Json(this.ModelState, JsonRequestBehavior.AllowGet)); }
public ActionResult Vote(VoteInputModel model) { if (this.Data.Votes.All().Any(x => x.TeamId == model.TeamId && x.UserId == model.UserId)) { this.ModelState.AddModelError(string.Empty, "The user has already vote for this team."); } if (model != null && this.ModelState.IsValid) { var vote = Mapper.Map<Vote>(model); vote.Value = 1; this.Data.Votes.Add(vote); this.Data.SaveChanges(); var votesCount = this.Data.Votes .All() .Where(x => x.TeamId == model.TeamId) .Sum(x => x.Value); return this.Content(votesCount.ToString()); } return this.Json(this.ModelState, JsonRequestBehavior.AllowGet); }