Create team form is ready (nearly)

This commit is contained in:
2020-04-02 23:22:00 +02:00
parent 9d80089a23
commit eecd527e69
7 changed files with 132 additions and 2 deletions

View File

@ -9,6 +9,7 @@ namespace TrackerLibrary.DataAccess
{
PrizeModel CreatePrize(PrizeModel model);
PersonModel CreatePerson(PersonModel model);
TeamModel CreateTeam(TeamModel model);
List<PersonModel> GetPerson_All();
}
}

View File

@ -20,7 +20,8 @@ namespace TrackerLibrary.DataAccess
private const string db = "Tournaments";
public PersonModel 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)))
{
var p = new DynamicParameters();
p.Add("@FirstName", model.FirstName);
p.Add("@LastName", model.LastName);
@ -61,6 +62,33 @@ namespace TrackerLibrary.DataAccess
}
}
public TeamModel CreateTeam(TeamModel model)
{
using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString(db)))
{
var p = new DynamicParameters();
p.Add("@TeamName", model.TeamName);
p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output);
connection.Execute("dbo.spTeams_Insert", p, commandType: CommandType.StoredProcedure);
model.Id = p.Get<int>("@Id");
foreach(PersonModel tm in model.TeamMembers)
{
p = new DynamicParameters();
p.Add("@TeamId", model.Id);
p.Add("@PersonId", tm.Id);
connection.Execute("dbo.spTeamMembers_Insert", p, commandType: CommandType.StoredProcedure);
}
return model;
}
}
public List<PersonModel> GetPerson_All()
{

View File

@ -11,6 +11,7 @@ namespace TrackerLibrary.DataAccess
{
private const string PrizesFile = "PrizeModels.csv";
private const string PeopleFile = "PersonModels.csv";
private const string TeamFile = "TeamModels.csv";
public PersonModel CreatePerson(PersonModel model)
{
@ -59,6 +60,35 @@ namespace TrackerLibrary.DataAccess
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();

View File

@ -61,6 +61,37 @@ namespace TrackerLibrary.DataAccess.TextHelpers
return output;
}
public static List<TeamModel> ConvertToTeamModels(this List<string> lines, string peopleFileName)
{
// id, team name,list of ids separated by pipe
// 3,Start Team, 1|4
List<TeamModel> output = new List<TeamModel>();
List<PersonModel> people = peopleFileName.FullFilePath().LoadFile().ConvertToPersonModels();
foreach (string line in lines)
{
string[] cols = line.Split(',');
TeamModel t = new TeamModel();
t.Id = int.Parse(cols[0]);
t.TeamName = cols[1];
string[] personIds = cols[2].Split('|');
foreach(string id in personIds)
{
t.TeamMembers.Add(people.Where(x => x.Id == int.Parse(id)).First());
}
output.Add(t);
}
return output;
}
public static void SaveToPrizeFile(this List<PrizeModel> models, string fileName)
{
List<string> lines = new List<string>();
@ -83,5 +114,32 @@ namespace TrackerLibrary.DataAccess.TextHelpers
File.WriteAllLines(fileName.FullFilePath(), lines);
}
public static void SaveToTeamFile(this List<TeamModel> models, string fileName)
{
List<string> lines = new List<string>();
foreach(TeamModel t in models)
{
lines.Add($"{t.Id},{t.TeamName},{ConvertPeopleListToString(t.TeamMembers)}");
}
File.WriteAllLines(fileName.FullFilePath(), lines);
}
private static string ConvertPeopleListToString(List<PersonModel> people)
{
string output = "";
if (people.Count==0)
{
return "";
}
foreach(PersonModel p in people)
{
output += $"{p.Id}|";
}
output = output.Substring(0, output.Length - 1);
return output;
}
}
}

View File

@ -6,6 +6,7 @@ namespace TrackerLibrary.Models
{
public class TeamModel
{
public int Id { get; set; }
public List<PersonModel> TeamMembers { get; set; } = new List<PersonModel>();
public string TeamName { get; set; }
}

View File

@ -265,6 +265,7 @@
this.createTeamButton.TabIndex = 23;
this.createTeamButton.Text = "Create Team";
this.createTeamButton.UseVisualStyleBackColor = true;
this.createTeamButton.Click += new System.EventHandler(this.createTeamButton_Click);
//
// CreateTeamForm
//

View File

@ -121,5 +121,16 @@ namespace TrackerUI
}
}
private void createTeamButton_Click(object sender, EventArgs e)
{
TeamModel t = new TeamModel();
t.TeamName = createdTeamNameValue.Text;
t.TeamMembers = selectedTeamMembers;
t = GlobalConfig.Connection.CreateTeam(t);
// TODO - if we arent closing the form after insertion , reset the form
}
}
}