From 27f406b836ad031aa33145cfd50c327f9f0025e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Mon, 23 Mar 2020 23:00:58 +0100 Subject: [PATCH] Handle the text file part of saving --- TrackerLibrary/DataAccess/TextConnector.cs | 27 ++++++++- .../DataAccess/TextConnectorProcessor.cs | 58 +++++++++++++++++++ TrackerLibrary/TrackerLibrary.csproj | 1 + TrackerUI/App.config | 3 + TrackerUI/Program.cs | 2 +- 5 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 TrackerLibrary/DataAccess/TextConnectorProcessor.cs diff --git a/TrackerLibrary/DataAccess/TextConnector.cs b/TrackerLibrary/DataAccess/TextConnector.cs index 436e470..cecfc5b 100644 --- a/TrackerLibrary/DataAccess/TextConnector.cs +++ b/TrackerLibrary/DataAccess/TextConnector.cs @@ -2,15 +2,40 @@ using System.Collections.Generic; using System.Text; using TrackerLibrary.Models; +using TrackerLibrary.DataAccess.TextHelpers; +using System.Linq; namespace TrackerLibrary.DataAccess { public class TextConnector : IDataConnection { + private const string PrizesFile = "PrizeModels.csv"; + // TODO - Wire up the createPrize for textFiles public PrizeModel CreatePrize(PrizeModel model) { - model.Id = 1; + // Load the text file + // Convert the text to a List>PrizeModel> + List prizes = PrizesFile.FullFilePath().LoadFile().ConvertToPrizeModels(); + + // Find the max Id + + int currentId = 1; + + if (prizes.Count > 0) + { + currentId = prizes.OrderByDescending(x => x.Id).First().Id + 1; + } + + model.Id = currentId; + + // Add the new record with the new ID (max +1) + prizes.Add(model); + + // Convert the prizes to a List + // Save the list to the text file + prizes.SaveToPrizeFile(PrizesFile); + return model; } } diff --git a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs new file mode 100644 index 0000000..0f24152 --- /dev/null +++ b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TrackerLibrary.Models; + +namespace TrackerLibrary.DataAccess.TextHelpers +{ + public static class TextConnectorProcessor + { + public static string FullFilePath(this string fileName) // PrizeModel.csv + { + return $"{ConfigurationManager.AppSettings["filePath"]}\\{fileName}"; + } + + public static List LoadFile(this string file) + { + if (!File.Exists(file)) + { + return new List(); + } + return File.ReadAllLines(file).ToList(); + } + + public static List ConvertToPrizeModels(this List lines) + { + List output = new List(); + + foreach (string line in lines) + { + string[] cols = line.Split(','); + PrizeModel p = new PrizeModel(); + p.Id = int.Parse(cols[0]); + p.PlaceNumber = int.Parse(cols[1]); + p.PlaceName = cols[2]; + p.PrizeAmount = decimal.Parse(cols[3]); + p.PrizePercentage = double.Parse(cols[4]); + output.Add(p); + } + + return output; + } + + public static void SaveToPrizeFile(this List models, string fileName) + { + List lines = new List(); + + foreach (PrizeModel p in models) + { + lines.Add($"{p.Id},{p.PlaceNumber},{p.PlaceName}, {p.PrizeAmount}, {p.PrizePercentage}"); + } + File.WriteAllLines(fileName.FullFilePath(), lines); + } + } +} diff --git a/TrackerLibrary/TrackerLibrary.csproj b/TrackerLibrary/TrackerLibrary.csproj index 7027b92..89b8610 100644 --- a/TrackerLibrary/TrackerLibrary.csproj +++ b/TrackerLibrary/TrackerLibrary.csproj @@ -48,6 +48,7 @@ + diff --git a/TrackerUI/App.config b/TrackerUI/App.config index 8683d13..37a8b0a 100644 --- a/TrackerUI/App.config +++ b/TrackerUI/App.config @@ -1,5 +1,8 @@  + + + diff --git a/TrackerUI/Program.cs b/TrackerUI/Program.cs index 4668d4c..55e3356 100644 --- a/TrackerUI/Program.cs +++ b/TrackerUI/Program.cs @@ -19,7 +19,7 @@ namespace TrackerUI Application.SetCompatibleTextRenderingDefault(false); // Initialize the database connections - GlobalConfig.InitializeConnections(DatabaseType.Sql); + GlobalConfig.InitializeConnections(DatabaseType.TextFile); Application.Run(new CreatePrizeForm()); //Application.Run(new TournamentDashboardForm());