From 1a686aba608d6d9a23b61cf8255784fc4d761f3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Thu, 26 Mar 2020 20:49:46 +0100 Subject: [PATCH] Personmodel handled as dbtabel and as file --- TrackerLibrary/DataAccess/IDataConnection.cs | 1 + TrackerLibrary/DataAccess/SqlConnector.cs | 19 +++++++ TrackerLibrary/DataAccess/TextConnector.cs | 20 ++++++++ .../DataAccess/TextConnectorProcessor.cs | 29 +++++++++++ TrackerLibrary/Models/PersonModel.cs | 4 ++ TrackerUI/CreateTeamForm.Designer.cs | 19 +++---- TrackerUI/CreateTeamForm.cs | 51 +++++++++++++++++++ TrackerUI/Program.cs | 2 +- 8 files changed, 135 insertions(+), 10 deletions(-) diff --git a/TrackerLibrary/DataAccess/IDataConnection.cs b/TrackerLibrary/DataAccess/IDataConnection.cs index a1394e3..ff38a7e 100644 --- a/TrackerLibrary/DataAccess/IDataConnection.cs +++ b/TrackerLibrary/DataAccess/IDataConnection.cs @@ -8,5 +8,6 @@ namespace TrackerLibrary.DataAccess public interface IDataConnection { PrizeModel CreatePrize(PrizeModel model); + PersonModel CreatePerson(PersonModel model); } } diff --git a/TrackerLibrary/DataAccess/SqlConnector.cs b/TrackerLibrary/DataAccess/SqlConnector.cs index 14a0123..9a3ffc6 100644 --- a/TrackerLibrary/DataAccess/SqlConnector.cs +++ b/TrackerLibrary/DataAccess/SqlConnector.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; using System.Data; using System.Text; +using TrackerLibrary.Models; //@PlaceNumber int, @@ -15,6 +16,24 @@ namespace TrackerLibrary.DataAccess { public class SqlConnector : IDataConnection { + public PersonModel CreatePerson(PersonModel model) + { + using (IDbConnection connection = new System.Data.SqlClient.SqlConnection(GlobalConfig.CnnString("Tournaments"))) { + var p = new DynamicParameters(); + p.Add("@FirstName", model.FirstName); + p.Add("@LastName", model.LastName); + p.Add("@EmailAddress", model.EmailAddress); + p.Add("@CellPhoneNumber", model.CellPhoneNumber); + p.Add("@Id", 0, dbType: DbType.Int32, direction: ParameterDirection.Output); + + connection.Execute("dbo.spPeople_Insert", p, commandType: CommandType.StoredProcedure); + + model.Id = p.Get("@Id"); + + return model; + } + } + // TODO - Make the CreatePrize method actually save to the database /// /// Saves a prize to the database diff --git a/TrackerLibrary/DataAccess/TextConnector.cs b/TrackerLibrary/DataAccess/TextConnector.cs index cecfc5b..9430182 100644 --- a/TrackerLibrary/DataAccess/TextConnector.cs +++ b/TrackerLibrary/DataAccess/TextConnector.cs @@ -10,6 +10,26 @@ namespace TrackerLibrary.DataAccess public class TextConnector : IDataConnection { private const string PrizesFile = "PrizeModels.csv"; + private const string PeopleFile = "PersonModels.csv"; + + public PersonModel CreatePerson(PersonModel model) + { + List 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) diff --git a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs index 0f24152..20610b7 100644 --- a/TrackerLibrary/DataAccess/TextConnectorProcessor.cs +++ b/TrackerLibrary/DataAccess/TextConnectorProcessor.cs @@ -44,6 +44,23 @@ namespace TrackerLibrary.DataAccess.TextHelpers return output; } + public static List ConvertToPersonModels(this List lines) + { + List output = new List(); + foreach(string line in lines) + { + string[] cols = line.Split(','); + PersonModel p = new PersonModel(); + p.Id = int.Parse(cols[0]); + p.FirstName = cols[1]; + p.LastName = cols[2]; + p.EmailAddress = cols[3]; + p.CellPhoneNumber = cols[4]; + output.Add(p); + } + return output; + } + public static void SaveToPrizeFile(this List models, string fileName) { List lines = new List(); @@ -54,5 +71,17 @@ namespace TrackerLibrary.DataAccess.TextHelpers } File.WriteAllLines(fileName.FullFilePath(), lines); } + + public static void SaveToPeopleFile(this List models, string fileName) + { + List lines = new List(); + foreach(PersonModel p in models) + { + lines.Add($"{p.Id},{p.FirstName},{p.LastName},{p.EmailAddress},{p.CellPhoneNumber}"); + } + + File.WriteAllLines(fileName.FullFilePath(), lines); + + } } } diff --git a/TrackerLibrary/Models/PersonModel.cs b/TrackerLibrary/Models/PersonModel.cs index 79eda44..05598ca 100644 --- a/TrackerLibrary/Models/PersonModel.cs +++ b/TrackerLibrary/Models/PersonModel.cs @@ -6,6 +6,10 @@ namespace TrackerLibrary.Models { public class PersonModel { + /// + /// The unique identifier for the prize + /// + public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string EmailAddress { get; set; } diff --git a/TrackerUI/CreateTeamForm.Designer.cs b/TrackerUI/CreateTeamForm.Designer.cs index 41c7d03..7d7e7e8 100644 --- a/TrackerUI/CreateTeamForm.Designer.cs +++ b/TrackerUI/CreateTeamForm.Designer.cs @@ -37,7 +37,7 @@ this.selectTeamMemberLabel = new System.Windows.Forms.Label(); this.addNewMemberGroupBox = new System.Windows.Forms.GroupBox(); this.createMemberButton = new System.Windows.Forms.Button(); - this.cellPhoneNumber = new System.Windows.Forms.TextBox(); + this.cellphoneValue = new System.Windows.Forms.TextBox(); this.cellPhoneLabel = new System.Windows.Forms.Label(); this.emailValue = new System.Windows.Forms.TextBox(); this.emailLabel = new System.Windows.Forms.Label(); @@ -116,7 +116,7 @@ // addNewMemberGroupBox // this.addNewMemberGroupBox.Controls.Add(this.createMemberButton); - this.addNewMemberGroupBox.Controls.Add(this.cellPhoneNumber); + this.addNewMemberGroupBox.Controls.Add(this.cellphoneValue); this.addNewMemberGroupBox.Controls.Add(this.cellPhoneLabel); this.addNewMemberGroupBox.Controls.Add(this.emailValue); this.addNewMemberGroupBox.Controls.Add(this.emailLabel); @@ -147,14 +147,15 @@ this.createMemberButton.TabIndex = 20; this.createMemberButton.Text = "Create Member"; this.createMemberButton.UseVisualStyleBackColor = true; + this.createMemberButton.Click += new System.EventHandler(this.createMemberButton_Click); // - // cellPhoneNumber + // cellphoneValue // - this.cellPhoneNumber.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.cellPhoneNumber.Location = new System.Drawing.Point(183, 169); - this.cellPhoneNumber.Name = "cellPhoneNumber"; - this.cellPhoneNumber.Size = new System.Drawing.Size(185, 35); - this.cellPhoneNumber.TabIndex = 16; + this.cellphoneValue.Font = new System.Drawing.Font("Segoe UI", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.cellphoneValue.Location = new System.Drawing.Point(183, 169); + this.cellphoneValue.Name = "cellphoneValue"; + this.cellphoneValue.Size = new System.Drawing.Size(185, 35); + this.cellphoneValue.TabIndex = 16; // // cellPhoneLabel // @@ -301,7 +302,7 @@ private System.Windows.Forms.Label selectTeamMemberLabel; private System.Windows.Forms.GroupBox addNewMemberGroupBox; private System.Windows.Forms.Button createMemberButton; - private System.Windows.Forms.TextBox cellPhoneNumber; + private System.Windows.Forms.TextBox cellphoneValue; private System.Windows.Forms.Label cellPhoneLabel; private System.Windows.Forms.TextBox emailValue; private System.Windows.Forms.Label emailLabel; diff --git a/TrackerUI/CreateTeamForm.cs b/TrackerUI/CreateTeamForm.cs index b2b0859..a2c9f95 100644 --- a/TrackerUI/CreateTeamForm.cs +++ b/TrackerUI/CreateTeamForm.cs @@ -7,6 +7,8 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; +using TrackerLibrary; +using TrackerLibrary.Models; namespace TrackerUI { @@ -16,5 +18,54 @@ namespace TrackerUI { InitializeComponent(); } + + private void createMemberButton_Click(object sender, EventArgs e) + { + if (ValidateForm()) + { + PersonModel p = new PersonModel(); + + p.FirstName = firstNameValue.Text; + p.LastName = lastNameValue.Text; + p.EmailAddress = emailValue.Text; + p.CellPhoneNumber = cellphoneValue.Text; + + GlobalConfig.Connection.CreatePerson(p); + + firstNameValue.Text = string.Empty; + lastNameValue.Text = string.Empty; + emailValue.Text = string.Empty; + cellphoneValue.Text = string.Empty; + + } + else + { + MessageBox.Show("You need to fill in all of the fiealds."); + } + + } + + private bool ValidateForm() + { + // TODO - add validation to the form + if(firstNameValue.Text.Length == 0) + { + return false; + } + if (lastNameValue.Text.Length == 0) + { + return false; + } + if (emailValue.Text.Length == 0) + { + return false; + } + if (cellphoneValue.Text.Length == 0) + { + return false; + } + + return true; + } } } diff --git a/TrackerUI/Program.cs b/TrackerUI/Program.cs index 55e3356..d0d883d 100644 --- a/TrackerUI/Program.cs +++ b/TrackerUI/Program.cs @@ -20,7 +20,7 @@ namespace TrackerUI // Initialize the database connections GlobalConfig.InitializeConnections(DatabaseType.TextFile); - Application.Run(new CreatePrizeForm()); + Application.Run(new CreateTeamForm()); //Application.Run(new TournamentDashboardForm()); }