public Tournament CreateTournament(Tournament model) { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnString(db))) { var p = new DynamicParameters(); p.Add("@TournamentName", model.TournamentName); p.Add("@EntryFee", model.EntryFee); p.Add("@Active", model.Active); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.spTournament_Insert", p, commandType: CommandType.StoredProcedure); model.Id = p.Get <int>("@id"); foreach (Team t in model.EnteredTeams) { p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@TeamId", t.Id); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("dbo.spTournamentEntry_Insert", p, commandType: CommandType.StoredProcedure); } foreach (Prize z in model.Prizes) { p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@PrizeId", z.Id); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("spTournamentPrize_Insert", p, commandType: CommandType.StoredProcedure); } //List<List<Matchup>> Rounds //List<MatchupEntry> Entries //Loop through the rounds //Loop through the matchups //Save the matchup //Loop through the entries and save them foreach (List <Matchup> matchups in model.Rounds) { foreach (Matchup matchup in matchups) { p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@MatchupRound", matchup.Round); p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("spMatchup_Insert", p, commandType: CommandType.StoredProcedure); matchup.Id = p.Get <int>("@id"); foreach (MatchupEntry entry in matchup.Entries) { p = new DynamicParameters(); p.Add("@MatchupId", matchup.Id); if (entry.ParentMatchup == null) { p.Add("@ParentMatchupId", null); } else { p.Add("@ParentMatchupId", entry.ParentMatchup.Id); } if (entry.TeamCompeting == null) { p.Add("@TeamCompetingId", null); } else { p.Add("@TeamCompetingId", entry.TeamCompeting.Id); } p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); connection.Execute("spMatchupEntry_Insert", p, commandType: CommandType.StoredProcedure); entry.Id = p.Get <int>("@id"); } } } return(model); } }
private static void CompleteTournament(Tournament model) { GlobalConfig.Connection.CompleteTournament(model); Team winners = model.Rounds.Last().First().Winner; Team runnerUp = model.Rounds .Last() .First() .Entrys .Where(x => x.TeamCompeting != winners) .First() .TeamCompeting; decimal winnerPrize = 0; decimal runnerUpPrize = 0; if (model.Prizes.Count > 0) { decimal totalIncome = model.EnteredTeams.Count * model.EntryFee; Prize firstPlacePrize = model.Prizes.Where(x => x.PlaceNumber == 1).FirstOrDefault(); if (firstPlacePrize != null) { winnerPrize = firstPlacePrize.CalculatePrizePayout(totalIncome); } Prize secoundPlacePrize = model.Prizes.Where(x => x.PlaceNumber == 2).FirstOrDefault(); if (firstPlacePrize != null) { runnerUpPrize = secoundPlacePrize.CalculatePrizePayout(totalIncome); } } // Send Email to all tournament string subject = ""; StringBuilder body = new StringBuilder(); subject = $"In {model.TournamentName}, {winners.TeamName} has won!"; body.AppendLine("<h1>We have a WINNER</h1>"); body.AppendLine("<p>Congratiulations to our winner on a great tournament.</p>"); body.AppendLine("<br />"); if (winnerPrize > 0) { body.AppendLine($"<p>{winners.TeamName} will receive ${winnerPrize} </p>"); } if (runnerUpPrize > 0) { body.AppendLine($"<p>{runnerUp.TeamName} will receive ${runnerUpPrize} </p>"); } body.AppendLine("<p> Thanks for a great tournament everyone!</p>"); body.AppendLine("~Tournament Tracker"); List <string> bcc = new List <string>(); foreach (Team t in model.EnteredTeams) { foreach (Person p in t.TeamMembers) { if (p.EmailAdress.Length > 0) { bcc.Add(p.EmailAdress); } } } EmailLogic.SendEmail(new List <string>(), bcc, subject, body.ToString()); // Complete Tournametn model.CompleteTournament(); }