Files
TournamentTracker/TrackerLibrary/DataAccess/TextConnector.cs

141 lines
4.0 KiB
C#

using System;
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";
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 PersonModel CreatePerson(PersonModel model)
{
List<PersonModel> people = PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
int currentId = 1;
if (people.Count > 0)
{
currentId = people.OrderByDescending(x => x.Id).First().Id + 1;
}
model.Id = currentId;
people.Add(model);
people.SaveToPeopleFile(PeopleFile);
return model;
}
// TODO - Wire up the createPrize for textFiles
public PrizeModel CreatePrize(PrizeModel model)
{
// 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;
}
public TeamModel CreateTeam(TeamModel model)
{
// Load the text file
// Convert the text to a List>PrizeModel>
List<TeamModel> teams = TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(PeopleFile);
// Find the max Id
int currentId = 1;
if (teams.Count > 0)
{
currentId = teams.OrderByDescending(x => x.Id).First().Id + 1;
}
model.Id = currentId;
// Add the new record with the new ID (max +1)
teams.Add(model);
// Convert the prizes to a List<string>
// Save the list<String> to the text file
teams.SaveToTeamFile(TeamFile);
return model;
}
public List<PersonModel> GetPerson_All()
{
return PeopleFile.FullFilePath().LoadFile().ConvertToPersonModels();
}
public List<TeamModel> GetTeam_All()
{
return TeamFile.FullFilePath().LoadFile().ConvertToTeamModels(PeopleFile);
}
public void CreateTournament(TournamentModel model)
{
List<TournamentModel> tournaments = TournamentFile
.FullFilePath()
.LoadFile()
.ConvertToTournamentModels(TeamFile, PeopleFile, PrizesFile);
int currentId = 1;
if (tournaments.Count > 0)
{
currentId = tournaments.OrderByDescending(x => x.Id).First().Id + 1;
}
model.Id = currentId;
model.SaveRoundsToFile( MatchupFile, MatchupEntryFile);
tournaments.Add(model);
tournaments.SaveToTournamentFile(TournamentFile);
}
public List<TournamentModel> GetTournament_All()
{
return TournamentFile
.FullFilePath()
.LoadFile()
.ConvertToTournamentModels(TeamFile, PeopleFile, PrizesFile);
}
public void UpdateMatchup(MatchupModel model)
{
model.UpdateMatchupToFile();
}
}
}