Handle the text file part of saving
This commit is contained in:
@ -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<PrizeModel> 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<string>
|
||||
// Save the list<String> to the text file
|
||||
prizes.SaveToPrizeFile(PrizesFile);
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
|
||||
58
TrackerLibrary/DataAccess/TextConnectorProcessor.cs
Normal file
58
TrackerLibrary/DataAccess/TextConnectorProcessor.cs
Normal file
@ -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<string> LoadFile(this string file)
|
||||
{
|
||||
if (!File.Exists(file))
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
return File.ReadAllLines(file).ToList();
|
||||
}
|
||||
|
||||
public static List<PrizeModel> ConvertToPrizeModels(this List<string> lines)
|
||||
{
|
||||
List<PrizeModel> output = new List<PrizeModel>();
|
||||
|
||||
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<PrizeModel> models, string fileName)
|
||||
{
|
||||
List<string> lines = new List<string>();
|
||||
|
||||
foreach (PrizeModel p in models)
|
||||
{
|
||||
lines.Add($"{p.Id},{p.PlaceNumber},{p.PlaceName}, {p.PrizeAmount}, {p.PrizePercentage}");
|
||||
}
|
||||
File.WriteAllLines(fileName.FullFilePath(), lines);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -48,6 +48,7 @@
|
||||
<Compile Include="DataAccess\IDataConnection.cs" />
|
||||
<Compile Include="DataAccess\SqlConnector.cs" />
|
||||
<Compile Include="DataAccess\TextConnector.cs" />
|
||||
<Compile Include="DataAccess\TextConnectorProcessor.cs" />
|
||||
<Compile Include="Enums.cs" />
|
||||
<Compile Include="GlobalConfig.cs" />
|
||||
<Compile Include="Models\MatchupEntryModel.cs" />
|
||||
|
||||
@ -1,5 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<configuration>
|
||||
<appSettings>
|
||||
<add key="filePath" value="D:\data\TournamentTracker"/>
|
||||
</appSettings>
|
||||
<connectionStrings>
|
||||
<add name="Tournaments" connectionString="Server=.\SQLEXPR2017;Database=Tournaments;Trusted_Connection=True;" providerName="System.Data.SqlClient"/>
|
||||
</connectionStrings>
|
||||
|
||||
@ -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());
|
||||
|
||||
Reference in New Issue
Block a user