public ElectionVoteViewModel ValidateToken(string tokenString, string ip) { var token = _tokenHandler.BuildToken(tokenString, ip); var model = new ElectionVoteViewModel { TokenString = tokenString }; var electionId = token.GetElectionId(); var wahlkreisId = token.GetWahlkreisId(); using (var context = new ElectionDBEntities()) { var election = context.Elections.Single(e => e.Id == electionId); var wahlkreis = context.Wahlkreis.Single(w => w.Id == wahlkreisId); var allParties = context.Parties; var parties = GetElectableParties(context, electionId, wahlkreis.Bundesland_Id); var people = GetElectablePeople(context, electionId, wahlkreisId); var partyVm = ViewModelMap.ViewModelMap.GetPartyViewModels(parties).ToList(); var peopleVm = ViewModelMap.ViewModelMap.GetPersonWithPartyViewModels(electionId, people, allParties).ToList(); model.Election = ViewModelMap.ViewModelMap.GetElectionViewModel(election); model.Wahlkreis = ViewModelMap.ViewModelMap.GetWahlkreisViewModel(wahlkreis); model.Parties = partyVm.OrderBy(r => r.Name); model.People = peopleVm.OrderBy(r => r.Party.Name); } return(model); }
public ActionResult PerformVote(ElectionVoteViewModel model) { var successful = CallService(() => Service.PerformVote(model, Request.UserHostAddress)); if (successful) { GetMessageBag().Success.Add("Sie haben Ihre Stimme erfolgreich abgegeben."); return(RedirectToAction("Index")); } return(RedirectToAction("Elect", new { txtToken = model.TokenString })); }
public bool PerformVote(ElectionVoteViewModel model, string ip) { var token = _tokenHandler.BuildToken(model.TokenString, ip); var wahlkreis = GetWahlkreis(token.GetWahlkreisId()); using (var context = new ElectionDBEntities()) { //Validate voted person and party var electablePeople = GetElectablePeople(context, token.GetElectionId(), token.GetWahlkreisId()); var electableParties = GetElectableParties(context, token.GetElectionId(), wahlkreis.BundeslandId); if (model.VotedPersonId > 0 && !electablePeople.Select(p => p.Id).Contains(model.VotedPersonId ?? -1)) { throw new PublicException( string.Format( "Die Person mit Id {0} darf in diesem Wahlkreis bei dieser Wahl nicht gewählt werden.", model.VotedPersonId)); } if (model.VotedPartyId > 0 && !electableParties.Select(p => p.Id).Contains(model.VotedPartyId ?? -1)) { throw new PublicException( string.Format( "Die Partei mit Id {0} darf in diesem Wahlkreis bei dieser Wahl nicht gewählt werden.", model.VotedPartyId)); } context.Erststimmes.Add(new Erststimme { Election_Id = token.GetElectionId(), Wahlkreis_Id = token.GetWahlkreisId(), Person_Id = model.VotedPersonId > 0 ? model.VotedPersonId : null, }); context.Zweitstimmes.Add(new Zweitstimme { Election_Id = token.GetElectionId(), Wahlkreis_Id = token.GetWahlkreisId(), Party_Id = model.VotedPartyId > 0 ? model.VotedPartyId : null }); context.SaveChanges(); _tokenHandler.FinishedToken(token); } return(true); }