private static void VoteStatistics(ElectionDbContext context) { GetDbVotesStatistics statistics = new GetDbVotesStatistics(context); Console.ForegroundColor = ConsoleColor.DarkCyan; Console.WriteLine($"Total votes: {statistics.GetVotesCount()}"); var validVotes = statistics.GetValidVotesCount(); if (validVotes.Item1 != 0) { Console.WriteLine($"Valid votes: {validVotes.Item1} ({validVotes.Item2:F4}%)"); (int invalidVotes, double invalidVotesPercent) = statistics.GetInvalidVotesCount(); Console.WriteLine($"Invalid votes: {invalidVotes} ({invalidVotesPercent:F4}%)"); List <CandidateResult> votesResults = statistics.GetVotesResults(); Console.ForegroundColor = ConsoleColor.DarkRed; Console.WriteLine("-----------------------------------------------------------------------------"); Console.WriteLine(" No | {0,-50} | Votes | Votes [%]", "Candidate / Party"); Console.WriteLine("-----------------------------------------------------------------------------"); Console.ForegroundColor = ConsoleColor.DarkCyan; int counter = 1; foreach (var vote in votesResults) { var name = $"{vote.CandidateName} / {vote.PartyName}"; Console.WriteLine(" {0,2:D} | {1,-50} | {2,5} | {3,8:F4}%", counter++, name, vote.VotesCount, vote.VotesPercent); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ElectionDbContext dbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } dbContext.Database.Migrate(); app.UseMvc(); }
public VoteToDbService(bool logged, LoginCredentials loginCredentials, ILoginValidatorService loginValidatorService, ElectionDbContext context, WebserviceRawCommunication webservice) { _logged = logged; _loginCredentials = loginCredentials; _loginValidatorService = loginValidatorService; _context = context; _webservice = webservice; }
public User(LoginCredentials loginCredentials, ElectionDbContext context, WebserviceRawCommunication webservice) { _loginCredentials = loginCredentials; FirstName = loginCredentials.FirstName; LastName = loginCredentials.LastName; Pesel = loginCredentials.Pesel; _context = context; _webservice = webservice; _checkUserAlreadyVotedService = new CheckUserAlreadyVotedFromDbService(loginCredentials, context); _checkUserIsDisallowdToVoteService = new CheckUserIsDisallowedToVoteService(webservice); _loginValidatorService = new LoginValidatorService(loginCredentials.FirstName, loginCredentials.LastName, Pesel, _checkUserIsDisallowdToVoteService, _checkUserAlreadyVotedService); }
public DatabaseAndWebserviceFixture() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); Context = new ElectionDbContext(Configuration.GetConnectionString("FpElectionDatabase")); Webservice = new WebserviceRawCommunication(); // Try to create database (if not exists) and populate with candidates data var dbInitializer = new DatabaseInitializer(Context, new GetJsonCandidateListFromWsService(Webservice)); dbInitializer.DeleteTablesInDatabase(); dbInitializer.InitializeDbWithCandidatesAndParties(); }
static void Main(string[] args) { ConsoleProlog(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("- Reading application settings - please wait..."); var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); Console.WriteLine("- Connecting to database - please wait..."); // Preparing services var context = new ElectionDbContext(Configuration.GetConnectionString("FpElectionDatabase")); using (context) { Console.WriteLine("- Initializing webservice - please wait..."); var webservice = new WebserviceRawCommunication(); Console.WriteLine("- Initializing database - please wait..."); // Try to create database (if not exists) and populate with candidates data var dbInitializer = new DatabaseInitializer(context, new GetJsonCandidateListFromWsService(webservice)); //dbInitializer.DeleteTablesInDatabase(); dbInitializer.InitializeDbWithCandidatesAndParties(); KeyOption key; do { key = ReadKeyOption(); switch (key) { case KeyOption.Login: LoginUserMethod(context, webservice); break; case KeyOption.VoteStatistics: VoteStatistics(context); break; case KeyOption.Quit: break; } } while (key != KeyOption.Quit); } }
public CheckUserAlreadyVotedFromDbService(LoginCredentials loginCredentials, ElectionDbContext context) { _loginCredentials = loginCredentials; _context = context; }
public VotingService(ElectionDbContext context) { _context = context; }
public GetDbVotesStatistics(ElectionDbContext context) { _context = context; }
//private static readonly IService _context=new _context(); public LiveViewService(ElectionDbContext context) { _context = context; }
private static void LoginUserMethod(ElectionDbContext context, WebserviceRawCommunication webservice) { Console.ForegroundColor = ConsoleColor.Magenta; Console.WriteLine("----- Login to system -------------------------------------------------------"); LoginCredentials loginCredentials = GetCredentialsFromConsole(); User user = new Domain.Models.User(loginCredentials, context, webservice); LoginValidation loginValidation = user.Login(); if (loginValidation.Error) { Console.ForegroundColor = ConsoleColor.Red; foreach (var error in loginValidation.LoginErrors) { Console.WriteLine($"- Error: {LoginErrorDescription.GetDescription(error)}"); } } if (loginValidation.Warning) { Console.ForegroundColor = ConsoleColor.DarkYellow; foreach (var warning in loginValidation.LoginWarnings) { Console.WriteLine($"- Warning: {LoginWarningDescription.GetDescription(warning)}"); } } if (!loginValidation.Error) { if (user.Logged) { Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine($"- User logged : {user.FirstName} / {user.LastName} / {user.Pesel}"); if (!loginValidation.LoginWarnings.Contains(LoginWarning.UserAlreadyVoted) && !loginValidation.LoginWarnings.Contains(LoginWarning.UserIsDisallowedToVote)) { IList <Candidate> candidates = context.Candidates.ToList(); IList <Party> parties = context.Parties.ToList(); Console.ForegroundColor = ConsoleColor.Magenta; for (int i = 0; i < candidates.Count; i++) { Console.WriteLine( $"{i + 1}] {candidates[i].Name} / {parties.Single(x => x.PartyId == candidates[i].PartyId).Name}"); } int[] parsedChoice; do { Console.ForegroundColor = ConsoleColor.Yellow; Console.Write("Please enter you candidate (or candidates) no [eg. \"1,5,10\"]: "); Console.ForegroundColor = ConsoleColor.White; string candidatesRead = Console.ReadLine(); parsedChoice = ChoiceParser.Parse(candidatesRead, candidates.Count); if (parsedChoice == null) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine($"- Error: Illegal characters in your response"); } } while (parsedChoice == null); Console.ForegroundColor = ConsoleColor.Yellow; for (int i = candidates.Count; i > 0; i--) { if (!parsedChoice.Any(x => x == i)) { candidates.RemoveAt(i - 1); } } Console.ForegroundColor = ConsoleColor.DarkGray; if (candidates.Count == 0) { Console.WriteLine($"You voted to nobody"); } foreach (var c in candidates) { Console.WriteLine( $"You voted to {c.Name} / {parties.Single(x => x.PartyId == c.PartyId).Name}"); } user.Vote(candidates); } // Get Vote statistics VoteStatistics(context); user.Logout(); Console.ForegroundColor = ConsoleColor.Cyan; Console.WriteLine("- User logout"); } } }
public RaportGeneratorService(ElectionDbContext context) => _context = context;
public ElectionsController(ElectionDbContext context) { this.context = context; }
public ElectionConfigurationService(ElectionDbContext context) { _context = context; }
public CreateUserInDbService(ElectionDbContext context) { _context = context; }