diff --git a/TrackerLibrary/EmailLogic.cs b/TrackerLibrary/EmailLogic.cs new file mode 100644 index 0000000..f8272b2 --- /dev/null +++ b/TrackerLibrary/EmailLogic.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net.Mail; +namespace TrackerLibrary +{ + public static class EmailLogic + { + public static void SendEmail(List to,string subject, string body) + { + MailAddress fromMailAddress = new MailAddress(GlobalConfig.AppKeyLookup("senderEmail"), GlobalConfig.AppKeyLookup("senderDisplayName")); + MailMessage mail = new MailMessage(); + to.ForEach(x => mail.To.Add(x)); + mail.From = fromMailAddress; + mail.Subject = subject; + mail.Body = body; + mail.IsBodyHtml = true; + + SmtpClient client = new SmtpClient(); + client.Send(mail); + } + } +} diff --git a/TrackerLibrary/GlobalConfig.cs b/TrackerLibrary/GlobalConfig.cs index 91f7fda..b439171 100644 --- a/TrackerLibrary/GlobalConfig.cs +++ b/TrackerLibrary/GlobalConfig.cs @@ -43,5 +43,10 @@ namespace TrackerLibrary { return ConfigurationManager.ConnectionStrings[name].ConnectionString; } + + public static string AppKeyLookup(string key) + { + return ConfigurationManager.AppSettings[key]; + } } } diff --git a/TrackerLibrary/TournamentLogic.cs b/TrackerLibrary/TournamentLogic.cs index c1d1461..2765b6e 100644 --- a/TrackerLibrary/TournamentLogic.cs +++ b/TrackerLibrary/TournamentLogic.cs @@ -27,6 +27,7 @@ namespace TrackerLibrary public static void UpdateTournamentResults(TournamentModel model) { + int startingRound = model.CheckCurrentRound(); List toScore = new List(); foreach (List round in model.Rounds) @@ -45,7 +46,78 @@ namespace TrackerLibrary AdvanceWinners(toScore, model); toScore.ForEach(x => GlobalConfig.Connection.UpdateMatchup(x)); + int endigRound = model.CheckCurrentRound(); + if (endigRound > startingRound) + { + model.AlertUsersToNewRound(); + } + } + public static void AlertUsersToNewRound(this TournamentModel model) + { + int currentRoundNumber = model.CheckCurrentRound(); + List currentRound = model.Rounds.Where(x => x.First().MatchupRound == currentRoundNumber).First(); + foreach (MatchupModel matchup in currentRound) + { + foreach (MatchupEntryModel me in matchup.Entries) + { + foreach (PersonModel p in me.TeamCompeting.TeamMembers) + { + AlertPersonToNewRound(p, me.TeamCompeting.TeamName, matchup.Entries.Where(x => x.TeamCompeting != me.TeamCompeting).FirstOrDefault()); + } + } + } + + } + + private static void AlertPersonToNewRound(PersonModel p, string teamName, MatchupEntryModel competitor) + { + if (p.EmailAddress.Length == 0) + { + return; + } + + List to = new List(); + string subject; + StringBuilder body = new StringBuilder(); + + if (competitor != null) + { + subject = $"You have a new matchup with {competitor.TeamCompeting.TeamName}"; + + body.AppendLine("

You Have a new matchup

"); + body.Append("Competitor: "); + body.Append(competitor.TeamCompeting.TeamName); + body.AppendLine(); + body.AppendLine(); + body.AppendLine("Have a great time!"); + body.AppendLine("~Tournament Tracker"); + } + else + { + subject = "You have a bye week this round"; + + body.AppendLine("Enjoy your round off!"); + body.AppendLine("~Tournament Tracker"); + } + + to.Add(p.EmailAddress); + + + EmailLogic.SendEmail( to, subject, body.ToString()); ; + } + + private static int CheckCurrentRound(this TournamentModel model) + { + int output = 1; + foreach (List round in model.Rounds) + { + if (round.All(x => x.Winner != null)) + { + output += 1; + } + } + return output; } private static void AdvanceWinners(List models, TournamentModel tournament) @@ -120,21 +192,6 @@ namespace TrackerLibrary } } - //if (teamOneScore > teamTwoScore) - //{ - // // Team one winns - // m.Winner = m.Entries[0].TeamCompeting; - //} - //else - // if (teamTwoScore > teamOneScore) - //{ - // // Team one winns - // m.Winner = m.Entries[1].TeamCompeting; - //} - //else - //{ - // MessageBox.Show("I dont handle tie games."); - //} } private static void CreateOtherRounds(TournamentModel model, int rounds) diff --git a/TrackerLibrary/TrackerLibrary.csproj b/TrackerLibrary/TrackerLibrary.csproj index e01eaf1..5c34207 100644 --- a/TrackerLibrary/TrackerLibrary.csproj +++ b/TrackerLibrary/TrackerLibrary.csproj @@ -52,6 +52,7 @@ + diff --git a/TrackerUI/App.config b/TrackerUI/App.config index df0fb63..071a665 100644 --- a/TrackerUI/App.config +++ b/TrackerUI/App.config @@ -4,13 +4,22 @@ + + - - - + + + + + + + + + + \ No newline at end of file diff --git a/TrackerUI/CreateTournamentForm.cs b/TrackerUI/CreateTournamentForm.cs index c4d1741..8c499bc 100644 --- a/TrackerUI/CreateTournamentForm.cs +++ b/TrackerUI/CreateTournamentForm.cs @@ -133,7 +133,7 @@ namespace TrackerUI // Create all of team entries GlobalConfig.Connection.CreateTournament(tm); - + tm.AlertUsersToNewRound(); TournamentViewerForm frm = new TournamentViewerForm(tm); frm.Show(); diff --git a/TrackerUI/TournamentViewerForm.cs b/TrackerUI/TournamentViewerForm.cs index 2b3aa9a..134c445 100644 --- a/TrackerUI/TournamentViewerForm.cs +++ b/TrackerUI/TournamentViewerForm.cs @@ -154,9 +154,44 @@ namespace TrackerUI LoadMatchups((int)roundDropDown.SelectedItem); } + private string ValidateData() + { + string output = ""; + double teamOneScore = 0; + double teamTwoScore = 0; + + bool scoreOneValid = double.TryParse(teamOneScoreValue.Text, out teamOneScore); + bool scoreTwoValid = double.TryParse(teamTwoScoreValue.Text, out teamTwoScore); + + if (!scoreOneValid ) + { + output = "The score One is not a valid number."; + } + else if (!scoreTwoValid) + { + output = "The score Tow is not a valid number."; + } + else if (teamOneScore == 0 && teamTwoScore == 0) + { + output = "You did not enter a score for either team."; + } + else if (teamOneScore == teamTwoScore) + { + output = "We do not allow ties in this application."; + } + + return output; + } private void scoreButton_Click(object sender, EventArgs e) { + var errorMessage = ValidateData(); + if (errorMessage.Length>0) + { + MessageBox.Show($"Input Error: {errorMessage}"); + return; + } + MatchupModel m = (MatchupModel)MatchUpListBox.SelectedItem; double teamOneScore = 0; double teamTwoScore = 0; @@ -199,8 +234,16 @@ namespace TrackerUI } } - TournamentLogic.UpdateTournamentResults(tournament); + try + { + TournamentLogic.UpdateTournamentResults(tournament); + } + catch (Exception ex) + { + MessageBox.Show($"The application had the following error :{ex.Message}"); + return; + } LoadMatchups((int)roundDropDown.SelectedItem); }