private async void CreateVote()
        {
            try
            {
                var vote = new Vote();

                var selectedCandidates = Candidates.Where(c => c.IsChecked);
                foreach (var candidate in selectedCandidates)
                {
                    vote.VotedCandidates.Add(new Business.Entities.Candidate()
                    {
                        Name  = candidate.Candidate.name,
                        Party = candidate.Candidate.party
                    });
                }

                vote.IsConfirmed = true;
                vote.PersonId    = ((PersonWrapper)Person).Model.Id;

                _voteRepository.Add(vote);

                await SaveWithOptimisticConcurrencyAsync(_voteRepository.SaveAsync, () =>
                {
                    _regionManager.Regions[RegionNames.ContentRegion].RemoveAll();
                    _regionManager.RequestNavigate(RegionNames.ContentRegion, ViewNames.StatisticsView);
                });
            }
            catch (System.Exception ex)
            {
#if DEBUG
                Console.WriteLine(ex.Message);
#endif
                //TODO: Logger
            }
        }
        private static void CreateAndAddVotingCandidate(int number, int numberOfVotes, IVotingCandidateRepository votingCandidateRepository, IVoteRepository voteRepository, ISongRepository songRepository)
        {
            var song = songRepository.Create();

            song.Title             = "SongRepositoryTests" + number;
            song.DurationInSeconds = 120;
            song.FileName          = "SongRepositoryTests.mp3";

            songRepository.Add(song);

            var votingCandidate = votingCandidateRepository.Create();

            votingCandidate.SongId       = song.Id;
            votingCandidate.Song         = song;
            votingCandidate.DisplayOrder = number;

            votingCandidateRepository.Add(votingCandidate);

            foreach (var index in Enumerable.Range(0, numberOfVotes))
            {
                var vote = voteRepository.Create();
                vote.VotingCandidateId = votingCandidate.Id;
                vote.VotingCandidate   = votingCandidate;
                vote.UserIdentifier    = Guid.NewGuid();

                voteRepository.Add(vote);
            }
        }
Beispiel #3
0
        public async Task <IActionResult> CastVote([FromRoute] string pollId, [FromQuery] string answerId)
        {
            if (!Guid.TryParse(pollId, out var id))
            {
                ModelState.TryAddModelError("ParsingError", $"Poll ID {pollId} is not valid.");
                return(BadRequest(ModelState));
            }

            var poll = await _pollRepository.GetById(id);

            if (poll == null)
            {
                return(NotFound());
            }

            var addressBytes = HttpContext.Connection.RemoteIpAddress.MapToIPv4().GetAddressBytes();

            if (!poll.AllowMultipleVotesPerIp)
            {
                var votes         = poll.Answers.SelectMany(s => s.Votes);
                var sourceIpBytes = addressBytes;
                if (votes.FirstOrDefault(v => v.Source.SequenceEqual(sourceIpBytes)) != null)
                {
                    return(StatusCode(StatusCodes.Status403Forbidden));
                }
            }

            if (!Guid.TryParse(answerId, out var answId))
            {
                ModelState.TryAddModelError("ParsingError", $"Answer ID {answerId} is not valid.");
                return(BadRequest(ModelState));
            }

            var answer = poll.Answers.FirstOrDefault(a => a.Id.Equals(answId));

            if (answer == null)
            {
                return(NotFound());
            }

            if (poll.Duration != null && poll.CreatedOn.Add(poll.Duration.Value) < DateTime.Now)
            {
                // Poll is expired
                return(StatusCode((int)HttpStatusCode.Gone));
            }

            var vote = new Vote
            {
                Id          = Guid.NewGuid(),
                Source      = addressBytes,
                CastedOn    = DateTime.Now,
                OwnerAnswer = answer
            };

            await _voteRepository.Add(vote);

            _messengerHub.Publish(new VoteCastedMessage(poll.Id, answer.Id));

            return(Ok());
        }
Beispiel #4
0
        public async Task <IActionResult> AddUserVote(int postId, string userId, int mark)
        {
            var post = await postRepository.GetPost(postId);

            var user = await appUserRepository.GetUser(userId);

            if (post == null || user == null)
            {
                return(NotFound());
            }

            var vote = await voteRepository.GetVote(postId, userId);

            if (vote != null)
            {
                return(StatusCode(409, "This User has already voted this post."));
            }

            var newVote = new Vote
            {
                PostId    = postId,
                AppUserId = userId,
                Mark      = mark
            };

            voteRepository.Add(newVote);
            await unitOfWork.CompleteAsync();

            return(Ok(newVote.PostId));
        }
        public void AddVote(Guid cattleId, string?name)
        {
            var vote = new Vote
            {
                CattleId = cattleId,
                Name     = name
            };

            _voteRepository.Add(vote);
            _voteRepository.SaveChanges();
        }
        public async Task <Vote> UpdateOrCreateAsync(VotingCandidate votingCandidate, Guid userIdentifier)
        {
            var vote = await _voteRepository.GetByUserIdentifierOrDefaultAsync(userIdentifier);

            if (vote == null)
            {
                vote = _voteRepository.Create();
                _voteRepository.Add(vote);
            }

            vote.Map(votingCandidate, userIdentifier);
            vote.Validate();

            return(vote);
        }
Beispiel #7
0
        public async Task Vote(int poolId, int userId, int restaurantId)
        {
            if ((await CanVote(poolId, userId)).Any())
            {
                throw new InvalidOperationException();
            }

            Pool pool = await poolRepository.Get(poolId);

            User user = await userAppService.GetUser(userId);

            Restaurant restaurant = await restaurantAppService.Get(restaurantId);

            pool.AddVote(new Vote(user, restaurant));
            await voteRepository.Add(user, pool, restaurant);
        }
        public async Task <IActionResult> Modify([FromBody] Vote item)
        {
            try
            {
                var res = await voteRepository.Add(item);

                var u = userRepository.GetAll().FirstOrDefault(a => a.Id == res.UserId);

                res.UserProfileImage = mediaRepository.GetByID(u.MediaId);

                return(Ok(res));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.GetBaseException().Message));
            }
        }
Beispiel #9
0
        public async Task <IActionResult> addvote(VoteDto votes)
        {
            var vote = _voteRepository.Add(votes);
            await _context.SaveChangesAsync();

            if (votes.voteLists != null)
            {
                foreach (var temp in votes.voteLists)
                {
                    temp.VoteId = vote.Id;
                    _voteListRepository.Add(temp);
                }
            }
            await _context.SaveChangesAsync();

            return(Ok());
        }
        public ActionResult Submit()
        {
            var cart = cartProvider.GetCart(this);

            if (cart.CharacterIds.Count == 0)
            {
                return(RedirectToAction("Manage"));
            }

            var vote = new Vote();

            vote.User      = userProvider.GetUserName(this);
            vote.Week      = weekProvider.GetCurrentWeek();
            vote.VoteItems = GetVoteItems(cart.CharacterIds, vote);
            voteRepository.Add(vote);

            return(RedirectToAction("Manage"));
        }
Beispiel #11
0
        /// <summary>
        /// Add a new vote
        /// </summary>
        /// <param name="vote"></param>
        /// <returns></returns>
        public Vote Add(Vote vote)
        {
            var e = new VoteEventArgs {
                Vote = vote, Api = _api
            };

            EventManager.Instance.FireBeforeVoteMade(this, e);

            if (!e.Cancel)
            {
                _voteRepository.Add(vote);

                EventManager.Instance.FireAfterVoteMade(this, new VoteEventArgs {
                    Vote = vote, Api = _api
                });
            }

            return(vote);
        }
        public RedirectResult SubmitVotes(string returnUrl)
        {
            var weekId = weekProvider.GetWeek();
            var userId = userProvider.GetId(this);
            var vote   = new Vote()
            {
                UserID = userId, WeekId = weekId
            };

            if (voteRepository.Contains(weekId, userId))
            {
                return(new RedirectResult(returnUrl));
            }

            var cart = GetCart();

            voteRepository.Add(vote, cart.CharactersIds);
            cartProvider.SetCart(this, new Cart());
            return(new RedirectResult(returnUrl));
        }
Beispiel #13
0
        public async Task <IHttpActionResult> Vote(int warId, Models.VoteRequest request)
        {
            if (request == null)
            {
                return(BadRequest("Could not deserialize request."));
            }

            if (!await IsValidWarId(warId))
            {
                return(NotFound());
            }

            var existingMatch = await _matchRepository.Get(warId, request.MatchId);

            ValidateModel(request, existingMatch);
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = _mapper.Map <ClaimsPrincipal, War.Users.User>(User as ClaimsPrincipal);

            if (!IsUserWhoCreatedMatch(existingMatch, user))
            {
                return(Unauthorized());
            }

            var votes = await _voteRepository.Get(warId, request.MatchId);

            if (votes.Any())
            {
                return(Conflict());
            }

            var voteRequest = _mapper.Map <Models.VoteRequest, VoteRequest>(request);

            voteRequest.UserIdentifier = user.Id;
            await _voteRepository.Add(warId, voteRequest);

            return(StatusCode(System.Net.HttpStatusCode.NoContent));
        }