Two big refactorings done

This commit is contained in:
2020-04-15 21:51:58 +02:00
parent 8758ec170d
commit 1361fa048e
6 changed files with 54 additions and 66 deletions

View File

@ -7,9 +7,9 @@ namespace TrackerLibrary.DataAccess
{ {
public interface IDataConnection public interface IDataConnection
{ {
PrizeModel CreatePrize(PrizeModel model); void CreatePrize(PrizeModel model);
PersonModel CreatePerson(PersonModel model); void CreatePerson(PersonModel model);
TeamModel CreateTeam(TeamModel model); void CreateTeam(TeamModel model);
void CreateTournament(TournamentModel model); void CreateTournament(TournamentModel model);
void UpdateMatchup(MatchupModel model); void UpdateMatchup(MatchupModel model);
List<TeamModel> GetTeam_All(); List<TeamModel> GetTeam_All();

View File

@ -18,7 +18,7 @@ namespace TrackerLibrary.DataAccess
public class SqlConnector : IDataConnection public class SqlConnector : IDataConnection
{ {
private const string db = "Tournaments"; private const string db = "Tournaments";
public PersonModel CreatePerson(PersonModel model) public void CreatePerson(PersonModel model)
{ {
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{ {
@ -32,8 +32,6 @@ namespace TrackerLibrary.DataAccess
connection.Execute("dbo.spPeople_Insert", p, commandType: CommandType.StoredProcedure); connection.Execute("dbo.spPeople_Insert", p, commandType: CommandType.StoredProcedure);
model.Id = p.Get<int>("@Id"); model.Id = p.Get<int>("@Id");
return model;
} }
} }
@ -43,7 +41,7 @@ namespace TrackerLibrary.DataAccess
/// </summary> /// </summary>
/// <param name="model">The prize information.</param> /// <param name="model">The prize information.</param>
/// <returns>The prize information, including the unique identifier.</returns> /// <returns>The prize information, including the unique identifier.</returns>
public Models.PrizeModel CreatePrize(Models.PrizeModel model) public void CreatePrize(Models.PrizeModel model)
{ {
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{ {
@ -58,11 +56,10 @@ namespace TrackerLibrary.DataAccess
model.Id = p.Get<int>("@Id"); model.Id = p.Get<int>("@Id");
return model;
} }
} }
public TeamModel CreateTeam(TeamModel model) public void CreateTeam(TeamModel model)
{ {
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db))) using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{ {
@ -84,7 +81,6 @@ namespace TrackerLibrary.DataAccess
} }
return model;
} }
} }

View File

@ -9,18 +9,10 @@ namespace TrackerLibrary.DataAccess
{ {
public class TextConnector : IDataConnection public class TextConnector : IDataConnection
{ {
private const string PrizesFile = "PrizeModels.csv";
private const string PeopleFile = "PersonModels.csv";
private const string TeamFile = "TeamModels.csv";
private const string TournamentFile = "TournamentModels.csv";
private const string MatchupFile = "MatchupModels.csv";
private const string MatchupEntryFile = "MatchupEntryModels.csv";
public void CreatePerson(PersonModel model)
public PersonModel CreatePerson(PersonModel model)
{ {
List<PersonModel> people = PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels(); List<PersonModel> people = GlobalConfig.PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
int currentId = 1; int currentId = 1;
if (people.Count > 0) if (people.Count > 0)
@ -32,17 +24,15 @@ namespace TrackerLibrary.DataAccess
people.Add(model); people.Add(model);
people.SaveToPeopleFile(PeopleFile); people.SaveToPeopleFile();
return model;
} }
// TODO - Wire up the createPrize for textFiles // TODO - Wire up the createPrize for textFiles
public PrizeModel CreatePrize(PrizeModel model) public void CreatePrize(PrizeModel model)
{ {
// Load the text file // Load the text file
// Convert the text to a List>PrizeModel> // Convert the text to a List>PrizeModel>
List<PrizeModel> prizes = PrizesFile.FullFilePath().LoadFile().ConvertToPrizeModels(); List<PrizeModel> prizes = GlobalConfig.PrizesFile.FullFilePath().LoadFile().ConvertToPrizeModels();
// Find the max Id // Find the max Id
@ -60,16 +50,15 @@ namespace TrackerLibrary.DataAccess
// Convert the prizes to a List<string> // Convert the prizes to a List<string>
// Save the list<String> to the text file // Save the list<String> to the text file
prizes.SaveToPrizeFile(PrizesFile); prizes.SaveToPrizeFile();
return model;
} }
public TeamModel CreateTeam(TeamModel model) public void CreateTeam(TeamModel model)
{ {
// Load the text file // Load the text file
// Convert the text to a List>PrizeModel> // Convert the text to a List>PrizeModel>
List<TeamModel> teams = TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(PeopleFile); List<TeamModel> teams = GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels();
// Find the max Id // Find the max Id
@ -89,26 +78,25 @@ namespace TrackerLibrary.DataAccess
// Convert the prizes to a List<string> // Convert the prizes to a List<string>
// Save the list<String> to the text file // Save the list<String> to the text file
teams.SaveToTeamFile(TeamFile); teams.SaveToTeamFile();
return model;
} }
public List<PersonModel> GetPerson_All() public List<PersonModel> GetPerson_All()
{ {
return PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels(); return GlobalConfig.PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
} }
public List<TeamModel> GetTeam_All() public List<TeamModel> GetTeam_All()
{ {
return TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(PeopleFile); return GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels();
} }
public void CreateTournament(TournamentModel model) public void CreateTournament(TournamentModel model)
{ {
List<TournamentModel> tournaments = TournamentFile List<TournamentModel> tournaments = GlobalConfig.TournamentFile
.FullFilePath() .FullFilePath()
.LoadFile() .LoadFile()
.ConvertToTournamentModels(TeamFile, PeopleFile, PrizesFile); .ConvertToTournamentModels();
int currentId = 1; int currentId = 1;
if (tournaments.Count > 0) if (tournaments.Count > 0)
@ -117,19 +105,19 @@ namespace TrackerLibrary.DataAccess
} }
model.Id = currentId; model.Id = currentId;
model.SaveRoundsToFile( MatchupFile, MatchupEntryFile); model.SaveRoundsToFile();
tournaments.Add(model); tournaments.Add(model);
tournaments.SaveToTournamentFile(TournamentFile); tournaments.SaveToTournamentFile();
} }
public List<TournamentModel> GetTournament_All() public List<TournamentModel> GetTournament_All()
{ {
return TournamentFile return GlobalConfig.TournamentFile
.FullFilePath() .FullFilePath()
.LoadFile() .LoadFile()
.ConvertToTournamentModels(TeamFile, PeopleFile, PrizesFile); .ConvertToTournamentModels();
} }
public void UpdateMatchup(MatchupModel model) public void UpdateMatchup(MatchupModel model)

View File

@ -7,6 +7,13 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using TrackerLibrary.Models; using TrackerLibrary.Models;
/*
* Title :
* Author :
* Date :
* Purpose:
*/
namespace TrackerLibrary.DataAccess.TextHelpers namespace TrackerLibrary.DataAccess.TextHelpers
{ {
public static class TextConnectorProcessor public static class TextConnectorProcessor
@ -61,12 +68,12 @@ namespace TrackerLibrary.DataAccess.TextHelpers
return output; return output;
} }
public static List<TeamModel> ConvertToTeamModels(this List<string> lines, string peopleFileName) public static List<TeamModel> ConvertToTeamModels(this List<string> lines)
{ {
// id, team name,list of ids separated by pipe // id, team name,list of ids separated by pipe
// 3,Start Team, 1|4 // 3,Start Team, 1|4
List<TeamModel> output = new List<TeamModel>(); List<TeamModel> output = new List<TeamModel>();
List<PersonModel> people = peopleFileName.FullFilePath().LoadFile().ConvertToPersonModels(); List<PersonModel> people = GlobalConfig.PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
foreach (string line in lines) foreach (string line in lines)
{ {
@ -91,10 +98,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
} }
public static List<TournamentModel> ConvertToTournamentModels( public static List<TournamentModel> ConvertToTournamentModels(
this List<string> lines, this List<string> lines
string teamFileName,
string peopleFileName,
string prizesFileName
) )
{ {
// id = 0 // id = 0
@ -106,8 +110,8 @@ namespace TrackerLibrary.DataAccess.TextHelpers
// id, TournamentName, EntryFee, (id|id|id - entered teams), (id|id|id - entered prizes), (rounds - id^id^id|id^id^id|id^id^id) // id, TournamentName, EntryFee, (id|id|id - entered teams), (id|id|id - entered prizes), (rounds - id^id^id|id^id^id|id^id^id)
List<TournamentModel> output = new List<TournamentModel>(); List<TournamentModel> output = new List<TournamentModel>();
List<TeamModel> teams = teamFileName.FullFilePath().LoadFile().ConvertToTeamModels(peopleFileName); List<TeamModel> teams = GlobalConfig.TeamFile.FullFilePath().LoadFile().ConvertToTeamModels();
List<PrizeModel> prizes = prizesFileName.FullFilePath().LoadFile().ConvertToPrizeModels(); List<PrizeModel> prizes = GlobalConfig.PrizesFile.FullFilePath().LoadFile().ConvertToPrizeModels();
List<MatchupModel> matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); List<MatchupModel> matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels();
foreach (string line in lines) foreach (string line in lines)
@ -150,7 +154,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
return output; return output;
} }
public static void SaveToPrizeFile(this List<PrizeModel> models, string fileName) public static void SaveToPrizeFile(this List<PrizeModel> models)
{ {
List<string> lines = new List<string>(); List<string> lines = new List<string>();
@ -158,10 +162,10 @@ namespace TrackerLibrary.DataAccess.TextHelpers
{ {
lines.Add($"{p.Id},{p.PlaceNumber},{p.PlaceName}, {p.PrizeAmount}, {p.PrizePercentage}"); lines.Add($"{p.Id},{p.PlaceNumber},{p.PlaceName}, {p.PrizeAmount}, {p.PrizePercentage}");
} }
File.WriteAllLines(fileName.FullFilePath(), lines); File.WriteAllLines(GlobalConfig.PrizesFile.FullFilePath(), lines);
} }
public static void SaveToPeopleFile(this List<PersonModel> models, string fileName) public static void SaveToPeopleFile(this List<PersonModel> models)
{ {
List<string> lines = new List<string>(); List<string> lines = new List<string>();
foreach (PersonModel p in models) foreach (PersonModel p in models)
@ -169,11 +173,11 @@ namespace TrackerLibrary.DataAccess.TextHelpers
lines.Add($"{p.Id},{p.FirstName},{p.LastName},{p.EmailAddress},{p.CellPhoneNumber}"); lines.Add($"{p.Id},{p.FirstName},{p.LastName},{p.EmailAddress},{p.CellPhoneNumber}");
} }
File.WriteAllLines(fileName.FullFilePath(), lines); File.WriteAllLines(GlobalConfig.PeopleFile.FullFilePath(), lines);
} }
public static void SaveToTeamFile(this List<TeamModel> models, string fileName) public static void SaveToTeamFile(this List<TeamModel> models)
{ {
List<string> lines = new List<string>(); List<string> lines = new List<string>();
foreach (TeamModel t in models) foreach (TeamModel t in models)
@ -181,10 +185,10 @@ namespace TrackerLibrary.DataAccess.TextHelpers
lines.Add($"{t.Id},{t.TeamName},{ConvertPeopleListToString(t.TeamMembers)}"); lines.Add($"{t.Id},{t.TeamName},{ConvertPeopleListToString(t.TeamMembers)}");
} }
File.WriteAllLines(fileName.FullFilePath(), lines); File.WriteAllLines(GlobalConfig.TeamFile.FullFilePath(), lines);
} }
public static void SaveRoundsToFile(this TournamentModel model,string matchupFile,string matchupEntryFile) public static void SaveRoundsToFile(this TournamentModel model)
{ {
// Loop through each round // Loop through each round
// Loop through each matchup // Loop through each matchup
@ -198,7 +202,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
// Get the top id and add one // Get the top id and add one
// Store the id // Store the id
// Svae the matchup record // Svae the matchup record
matchup.SaveMatchupToFile(matchupFile, matchupEntryFile); matchup.SaveMatchupToFile();
} }
@ -270,7 +274,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
{ {
List<string> matchingTeams = new List<string>(); List<string> matchingTeams = new List<string>();
matchingTeams.Add(team); matchingTeams.Add(team);
return matchingTeams.ConvertToTeamModels(GlobalConfig.PeopleFile).First(); return matchingTeams.ConvertToTeamModels().First();
} }
} }
return null; return null;
@ -318,7 +322,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
return output; return output;
} }
public static void SaveMatchupToFile(this MatchupModel matchup, string matchupFile, string matchupEntryFile) public static void SaveMatchupToFile(this MatchupModel matchup)
{ {
List<MatchupModel> matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels(); List<MatchupModel> matchups = GlobalConfig.MatchupFile.FullFilePath().LoadFile().ConvertToMatchupModels();
int currentId = 1; int currentId = 1;
@ -332,7 +336,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
foreach (MatchupEntryModel entry in matchup.Entries) foreach (MatchupEntryModel entry in matchup.Entries)
{ {
entry.SaveEntryToFile(matchupEntryFile); entry.SaveEntryToFile();
} }
List<string> lines = new List<string>(); List<string> lines = new List<string>();
@ -385,7 +389,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
File.WriteAllLines(GlobalConfig.MatchupFile.FullFilePath(), lines); File.WriteAllLines(GlobalConfig.MatchupFile.FullFilePath(), lines);
} }
public static void SaveEntryToFile(this MatchupEntryModel entry, string matchupEntryFile) public static void SaveEntryToFile(this MatchupEntryModel entry)
{ {
List<MatchupEntryModel> entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntryModels(); List<MatchupEntryModel> entries = GlobalConfig.MatchupEntryFile.FullFilePath().LoadFile().ConvertToMatchupEntryModels();
int currentId = 1; int currentId = 1;
@ -452,7 +456,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
} }
public static void SaveToTournamentFile(this List<TournamentModel> models, string fileName) public static void SaveToTournamentFile(this List<TournamentModel> models)
{ {
List<string> lines = new List<string>(); List<string> lines = new List<string>();
@ -461,7 +465,7 @@ namespace TrackerLibrary.DataAccess.TextHelpers
lines.Add($@"{tm.Id},{tm.TournamentName},{tm.EntryFee},{ConvertTeamListToString(tm.EnteredTeams)},{ConvertPrizeListToString(tm.Prizes)},{ConvertRoundListToString(tm.Rounds)}"); lines.Add($@"{tm.Id},{tm.TournamentName},{tm.EntryFee},{ConvertTeamListToString(tm.EnteredTeams)},{ConvertPrizeListToString(tm.Prizes)},{ConvertRoundListToString(tm.Rounds)}");
} }
File.WriteAllLines(fileName.FullFilePath(), lines); File.WriteAllLines(GlobalConfig.TournamentFile.FullFilePath(), lines);
} }
private static string ConvertRoundListToString(List<List<Models.MatchupModel>> rounds) private static string ConvertRoundListToString(List<List<Models.MatchupModel>> rounds)

View File

@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8" ?>
<configuration> <configuration>
<appSettings> <appSettings>
<!--<add key="filePath" value="D:\data\TournamentTracker"/>--> <add key="filePath" value="D:\data\TournamentTracker"/>
<add key="filePath" value="C:\AppData\TournamentTracker"/> <!--<add key="filePath" value="C:\AppData\TournamentTracker"/>-->
</appSettings> </appSettings>
<connectionStrings> <connectionStrings>
<add name="Tournaments" connectionString="Server=TOMMYASUS\SQLEXPR2017;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/> <!--<add name="Tournaments" connectionString="Server=TOMMYASUS\SQLEXPR2017;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>-->
<!--<add name="Tournaments" connectionString="Server=.\SQLEXPR2017;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>--> <add name="Tournaments" connectionString="Server=.\SQLEXPR2017;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
<!--Data Source=TOMMYASUS\SQLEXPR2017;Initial Catalog=Tournaments;Integrated Security=True--> <!--Data Source=TOMMYASUS\SQLEXPR2017;Initial Catalog=Tournaments;Integrated Security=True-->
</connectionStrings> </connectionStrings>
<startup> <startup>

View File

@ -19,7 +19,7 @@ namespace TrackerUI
Application.SetCompatibleTextRenderingDefault(false); Application.SetCompatibleTextRenderingDefault(false);
// Initialize the database connections // Initialize the database connections
GlobalConfig.InitializeConnections(DatabaseType.Sql); GlobalConfig.InitializeConnections(DatabaseType.TextFile);
Application.Run(new TournamentDashboardForm()); Application.Run(new TournamentDashboardForm());
} }
} }