From a3096138f90c22a607906e612908af863e81b562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 9 Apr 2020 00:05:25 +0200 Subject: [PATCH] Saving a tournament is working --- TrackerLibrary/DataAccess/SqlConnector.cs | 78 ++++++++++++++++++---- TrackerLibrary/Models/MatchupEntryModel.cs | 4 ++ TrackerLibrary/TournamentLogic.cs | 6 +- 3 files changed, 72 insertions(+), 16 deletions(-) diff --git a/TrackerLibrary/DataAccess/SqlConnector.cs b/TrackerLibrary/DataAccess/SqlConnector.cs index b80eeb1..c34d60b 100644 --- a/TrackerLibrary/DataAccess/SqlConnector.cs +++ b/TrackerLibrary/DataAccess/SqlConnector.cs @@ -93,16 +93,17 @@ namespace TrackerLibrary.DataAccess { using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) { - DynamicParameters p = SaveTournament(model, connection); + SaveTournament(model, connection); - p = SaveTournamentPrizes(model, connection, p); + SaveTournamentPrizes(model, connection); - p = SaveTournamentEntries(model, connection, p); + SaveTournamentEntries(model, connection); + SaveTournamentRounds(model, connection); } } - private static DynamicParameters SaveTournament(TournamentModel model, IDbConnection connection) + private void SaveTournament(TournamentModel model, IDbConnection connection) { var p = new DynamicParameters(); p.Add("@TournamentName", model.TournamentName); @@ -112,14 +113,13 @@ namespace TrackerLibrary.DataAccess connection.Execute("dbo.spTournaments_Insert", p, commandType: CommandType.StoredProcedure); model.Id = p.Get("@Id"); - return p; } - private static DynamicParameters SaveTournamentEntries(TournamentModel model, IDbConnection connection, DynamicParameters p) + private static void SaveTournamentEntries(TournamentModel model, IDbConnection connection) { foreach (TeamModel tm in model.EnteredTeams) { - p = new DynamicParameters(); + var p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@TeamId", tm.Id); p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); @@ -127,15 +127,69 @@ namespace TrackerLibrary.DataAccess connection.Execute("dbo.spTournamentEntries_Insert", p, commandType: CommandType.StoredProcedure); } - - return p; } - private static DynamicParameters SaveTournamentPrizes(TournamentModel model, IDbConnection connection, DynamicParameters p) + private static void SaveTournamentRounds(TournamentModel model, IDbConnection connection) + { + // List> rounds + // List Entries + + // Loop through the rounds + // Loop through the matchups + // Save the matchup + // Loop through the entries and save them + + foreach (List round in model.Rounds) + { + foreach (MatchupModel matchup in round) + { + var p = new DynamicParameters(); + p.Add("@TournamentId", model.Id); + p.Add("@MatchupRound", matchup.MatchupRound); + p.Add("@id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); + + connection.Execute("dbo.spMatchups_Insert", p, commandType: CommandType.StoredProcedure); + + matchup.Id = p.Get("@id"); + + foreach (MatchupEntryModel 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("dbo.spMatchupEntries_Insert", p, commandType: CommandType.StoredProcedure); + + entry.Id = p.Get("@id"); + } + + } + } + + } + + private static void SaveTournamentPrizes(TournamentModel model, IDbConnection connection) { foreach (PrizeModel pz in model.Prizes) { - p = new DynamicParameters(); + var p = new DynamicParameters(); p.Add("@TournamentId", model.Id); p.Add("@PrizeId", pz.Id); p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); @@ -143,8 +197,6 @@ namespace TrackerLibrary.DataAccess connection.Execute("dbo.spTournamentPrizes_Insert", p, commandType: CommandType.StoredProcedure); } - - return p; } public List GetPerson_All() diff --git a/TrackerLibrary/Models/MatchupEntryModel.cs b/TrackerLibrary/Models/MatchupEntryModel.cs index 8abb927..5ef7109 100644 --- a/TrackerLibrary/Models/MatchupEntryModel.cs +++ b/TrackerLibrary/Models/MatchupEntryModel.cs @@ -6,6 +6,10 @@ namespace TrackerLibrary.Models { public class MatchupEntryModel { + /// + /// The unique identifier for the matchupEntry + /// + public int Id { get; set; } /// /// Represents one team in the matchup /// diff --git a/TrackerLibrary/TournamentLogic.cs b/TrackerLibrary/TournamentLogic.cs index 1b81419..73c85fa 100644 --- a/TrackerLibrary/TournamentLogic.cs +++ b/TrackerLibrary/TournamentLogic.cs @@ -77,8 +77,8 @@ namespace TrackerLibrary { //Math.Pow(2, rounds); int output = 0; - int totalTeams = 0; - for (int i = 1; i < rounds; i++) + int totalTeams = 1; + for (int i = 1; i <= rounds; i++) { totalTeams *= 2; } @@ -90,7 +90,7 @@ namespace TrackerLibrary private static int FindNumberOfRounds(int teamCount) { - int output = 0; + int output = 1; int val = 2; while (val < teamCount) {