public async Task <string> SubmitVote(SubmitVoteParameter model) { BaseResponse response = new BaseResponse(); try { model.UserId = Int32.Parse(UserId); model.VotingDate = DateTime.Now.Date; var data = UserId; response = await _votingService.PostAsync(_configuration.GetValue <string>("VotingEndPoint:SubmitVote"), model); } catch (Exception ex) { if (ex.Message.Contains("Input string was not in a correct format")) { response.Code = 6002; response.Status = HttpStatusCode.BadRequest.ToString(); response.Message = "Please login before you submit your vote."; } else { response.Message = ex.Message; } } return(JsonConvert.SerializeObject(response)); }
public async Task <BaseResponse> SubmitVote(SubmitVoteParameter param) { return(await _votingRepository.SubmitVote(param)); }
public async Task <BaseResponse> SubmitVote(SubmitVoteParameter param) { BaseResponse response = new BaseResponse(); using (var dbcxtransaction = await _context.Database.BeginTransactionAsync()) { try { var checkDuplicateVote = await _context.MappingVotingUsers.FirstOrDefaultAsync(n => n.UserId == param.UserId && n.VotingProcessId == param.VotingProcessId); if (checkDuplicateVote != null) { dbcxtransaction.Rollback(); response.Code = 6001; response.Status = HttpStatusCode.BadRequest.ToString(); response.Message = "You cant vote one item two times."; } else { // Save new voter MappingVotingUsers userVote = new MappingVotingUsers { UserId = param.UserId, VotingProcessId = param.VotingProcessId }; await _context.MappingVotingUsers.AddAsync(userVote); await _context.SaveChangesAsync(); // Update voting process var voting = await _context.MasterVotingProcess.FindAsync(param.VotingProcessId); if (param.VotingDate.Date > voting.DueDate) { dbcxtransaction.Rollback(); response.Code = 6000; response.Status = HttpStatusCode.BadRequest.ToString(); response.Message = "You can't vote expired item."; } else { voting.SupportersCount = voting.SupportersCount + param.VotingValue; voting.ModifiedDate = DateTime.Now; voting.ModifiedBy = "Admin"; _context.Entry(voting).State = EntityState.Modified; await _context.SaveChangesAsync(); dbcxtransaction.Commit(); // Get new response after voting var responseData = await(from vote in _context.MasterVotingProcess where vote.VotingProcessId == param.VotingProcessId select new { vote.VotingProcessId, Reviewers = voting.MappingVotingUsers.Count(), StarValue = vote.SupportersCount.Value / vote.MappingVotingUsers.Count() }).FirstOrDefaultAsync(); response.Data = responseData; response.Code = (int)HttpStatusCode.OK; response.Status = HttpStatusCode.OK.ToString(); response.Message = "Thank you for your feedback."; } } } catch (Exception ex) { response.Message = ex.ToString(); response.Code = (int)HttpStatusCode.InternalServerError; response.Status = HttpStatusCode.InternalServerError.ToString(); dbcxtransaction.Rollback(); } } return(response); }