diff --git a/StockDAL/AddressRepository.cs b/StockDAL/AddressRepository.cs index 5e4b9ac..8d71f46 100644 --- a/StockDAL/AddressRepository.cs +++ b/StockDAL/AddressRepository.cs @@ -19,5 +19,37 @@ namespace StockDAL select adr).FirstOrDefault(); return entity; } + + public Address SaveAddress(Address address) + { + using var context = new StockContext(); + var entity = (from adr in context.Addresses + where adr.Id == address.Id + select adr).FirstOrDefault(); + + if (entity == null) + { + entity = new Address + { + Street = address.Street, + Street2 = address.Street2, + Zipcode = address.Zipcode, + Destination = address.Destination, + Nation = address.Nation + }; + context.Addresses.Add(entity); + } + else + { + entity.Street = address.Street; + entity.Street2 = address.Street2; + entity.Zipcode = address.Zipcode; + entity.Destination = address.Destination; + entity.Nation = address.Nation; + } + + context.SaveChanges(); + return entity; + } } } diff --git a/StockDAL/PersonRepository.cs b/StockDAL/PersonRepository.cs index acea091..3ada103 100644 --- a/StockDAL/PersonRepository.cs +++ b/StockDAL/PersonRepository.cs @@ -18,6 +18,58 @@ namespace StockDAL where prs.Id == personId select prs).FirstOrDefault(); return entity; - } + } + + public IEnumerable GetAllOwners() + { + using var context = new StockContext(); + var output = context.Persons; + return output.ToList(); + } + + + public Person SavePerson(Person person) + { + using var context = new StockContext(); + + var entity = (from prs in context.Persons + where prs.Id == person.Id + select prs).FirstOrDefault(); + + if (entity == null) + { + entity = new Person + { + AccountNo = person.AccountNo, + Born = person.Born, + ClearingNo = person.ClearingNo, + Comments = person.Comments, + FirstName = person.FirstName, + HomeAddress = person.HomeAddress, + InvoiceAddress = person.InvoiceAddress, + LastName = person.LastName, + NickName = person.NickName + }; + context.Persons.Add(entity); + } + else + { + entity.AccountNo = person.AccountNo; + entity.Born = person.Born; + entity.ClearingNo = person.ClearingNo; + entity.Comments = person.Comments; + entity.FirstName = person.FirstName; + entity.HomeAddress = person.HomeAddress; + entity.InvoiceAddress = person.InvoiceAddress; + entity.LastName = person.LastName; + entity.NickName = person.NickName; + } + + context.SaveChanges(); + + return entity; + } } + + } diff --git a/StockDal.Interface/IAddressRepository.cs b/StockDal.Interface/IAddressRepository.cs index f9bef5e..65e6688 100644 --- a/StockDal.Interface/IAddressRepository.cs +++ b/StockDal.Interface/IAddressRepository.cs @@ -10,5 +10,6 @@ namespace StockDAL.Interface public interface IAddressRepository { Address GetAddressById(int AddressId); + Address SaveAddress(Address address); } } diff --git a/StockDal.Interface/IPersonRepository.cs b/StockDal.Interface/IPersonRepository.cs index 2f895bd..ac99c40 100644 --- a/StockDal.Interface/IPersonRepository.cs +++ b/StockDal.Interface/IPersonRepository.cs @@ -9,6 +9,8 @@ namespace StockDAL.Interface { public interface IPersonRepository { + IEnumerable GetAllOwners(); Person GetPersonById(int personId); + Person SavePerson(Person person); } } diff --git a/StockInfo/Stocks.db-shm b/StockInfo/Stocks.db-shm new file mode 100644 index 0000000..fe9ac28 Binary files /dev/null and b/StockInfo/Stocks.db-shm differ diff --git a/StockInfo/Stocks.db-wal b/StockInfo/Stocks.db-wal new file mode 100644 index 0000000..e69de29 diff --git a/StockInfo/frmInitial.Designer.cs b/StockInfo/frmInitial.Designer.cs index 8ca45a5..4a9565d 100644 --- a/StockInfo/frmInitial.Designer.cs +++ b/StockInfo/frmInitial.Designer.cs @@ -47,9 +47,9 @@ namespace StockInfo this.ofdOpener = new System.Windows.Forms.OpenFileDialog(); this.lblStockRows = new System.Windows.Forms.Label(); this.gpOwners = new System.Windows.Forms.GroupBox(); - this.comboBox1 = new System.Windows.Forms.ComboBox(); - this.btnEditPerson = new System.Windows.Forms.Button(); this.btnConnShares = new System.Windows.Forms.Button(); + this.btnEditPerson = new System.Windows.Forms.Button(); + this.cmbOwners = new System.Windows.Forms.ComboBox(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); this.gB1.SuspendLayout(); this.gbStockMgmnt.SuspendLayout(); @@ -230,7 +230,7 @@ namespace StockInfo // this.gpOwners.Controls.Add(this.btnConnShares); this.gpOwners.Controls.Add(this.btnEditPerson); - this.gpOwners.Controls.Add(this.comboBox1); + this.gpOwners.Controls.Add(this.cmbOwners); this.gpOwners.Location = new System.Drawing.Point(589, 385); this.gpOwners.Name = "gpOwners"; this.gpOwners.Size = new System.Drawing.Size(252, 141); @@ -238,13 +238,16 @@ namespace StockInfo this.gpOwners.TabStop = false; this.gpOwners.Text = "ShareOwners"; // - // comboBox1 + // btnConnShares // - this.comboBox1.FormattingEnabled = true; - this.comboBox1.Location = new System.Drawing.Point(6, 33); - this.comboBox1.Name = "comboBox1"; - this.comboBox1.Size = new System.Drawing.Size(152, 23); - this.comboBox1.TabIndex = 0; + this.btnConnShares.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); + this.btnConnShares.Location = new System.Drawing.Point(120, 82); + this.btnConnShares.Name = "btnConnShares"; + this.btnConnShares.Size = new System.Drawing.Size(108, 23); + this.btnConnShares.TabIndex = 4; + this.btnConnShares.Text = "Connect Shares"; + this.btnConnShares.UseVisualStyleBackColor = true; + this.btnConnShares.Click += new System.EventHandler(this.btnConnShares_Click); // // btnEditPerson // @@ -257,16 +260,14 @@ namespace StockInfo this.btnEditPerson.UseVisualStyleBackColor = true; this.btnEditPerson.Click += new System.EventHandler(this.btnEditPerson_Click); // - // btnConnShares + // cmbOwners // - this.btnConnShares.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); - this.btnConnShares.Location = new System.Drawing.Point(120, 82); - this.btnConnShares.Name = "btnConnShares"; - this.btnConnShares.Size = new System.Drawing.Size(108, 23); - this.btnConnShares.TabIndex = 4; - this.btnConnShares.Text = "Connect Shares"; - this.btnConnShares.UseVisualStyleBackColor = true; - this.btnConnShares.Click += new System.EventHandler(this.btnConnShares_Click); + this.cmbOwners.FormattingEnabled = true; + this.cmbOwners.Location = new System.Drawing.Point(6, 33); + this.cmbOwners.Name = "cmbOwners"; + this.cmbOwners.Size = new System.Drawing.Size(152, 23); + this.cmbOwners.TabIndex = 0; + this.cmbOwners.SelectedIndexChanged += new System.EventHandler(this.cmbOwners_SelectedIndexChanged); // // frmInitial // @@ -288,6 +289,7 @@ namespace StockInfo this.Text = "Stock Overview"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.frmInitial_FormClosing); this.Load += new System.EventHandler(this.Form1_Load); + this.Shown += new System.EventHandler(this.frmInitial_Shown); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.gB1.ResumeLayout(false); this.gB1.PerformLayout(); @@ -320,7 +322,7 @@ namespace StockInfo private System.Windows.Forms.GroupBox gpOwners; private System.Windows.Forms.Button btnConnShares; private System.Windows.Forms.Button btnEditPerson; - private System.Windows.Forms.ComboBox comboBox1; + private System.Windows.Forms.ComboBox cmbOwners; } } diff --git a/StockInfo/frmInitial.cs b/StockInfo/frmInitial.cs index 35fe90e..3e480c0 100644 --- a/StockInfo/frmInitial.cs +++ b/StockInfo/frmInitial.cs @@ -178,5 +178,27 @@ namespace StockInfo { } + + private void frmInitial_Shown(object sender, EventArgs e) + { + LoadCmbOwners(); + } + + private void LoadCmbOwners() + { + var owners = _personRepository.GetAllOwners(); + if (owners.Count() > 0) + { + foreach (var person in owners) + { + var itemIndex = cmbOwners.Items.Add($"{person.FirstName} {person.LastName} ({person.NickName})"); + } + } + } + + private void cmbOwners_SelectedIndexChanged(object sender, EventArgs e) + { + + } } } diff --git a/StockInfo/frmPerson.Designer.cs b/StockInfo/frmPerson.Designer.cs index 238ece8..fe27c90 100644 --- a/StockInfo/frmPerson.Designer.cs +++ b/StockInfo/frmPerson.Designer.cs @@ -40,6 +40,7 @@ namespace StockInfo this.txtLastName = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.gbAddress = new System.Windows.Forms.GroupBox(); + this.txtDestination = new System.Windows.Forms.TextBox(); this.txtNation = new System.Windows.Forms.TextBox(); this.label10 = new System.Windows.Forms.Label(); this.txtZipCode = new System.Windows.Forms.TextBox(); @@ -55,7 +56,6 @@ namespace StockInfo this.txtAccountNr = new System.Windows.Forms.TextBox(); this.btnAddSave = new System.Windows.Forms.Button(); this.btnClose = new System.Windows.Forms.Button(); - this.txtDestination = new System.Windows.Forms.TextBox(); this.gbAddress.SuspendLayout(); this.SuspendLayout(); // @@ -80,7 +80,7 @@ namespace StockInfo this.txtComment.Location = new System.Drawing.Point(113, 162); this.txtComment.Name = "txtComment"; this.txtComment.Size = new System.Drawing.Size(188, 23); - this.txtComment.TabIndex = 3; + this.txtComment.TabIndex = 5; // // label2 // @@ -96,7 +96,7 @@ namespace StockInfo this.txtPersonNr.Location = new System.Drawing.Point(113, 133); this.txtPersonNr.Name = "txtPersonNr"; this.txtPersonNr.Size = new System.Drawing.Size(188, 23); - this.txtPersonNr.TabIndex = 5; + this.txtPersonNr.TabIndex = 4; // // label3 // @@ -112,7 +112,7 @@ namespace StockInfo this.txtNickName.Location = new System.Drawing.Point(113, 104); this.txtNickName.Name = "txtNickName"; this.txtNickName.Size = new System.Drawing.Size(188, 23); - this.txtNickName.TabIndex = 7; + this.txtNickName.TabIndex = 3; // // label4 // @@ -128,7 +128,7 @@ namespace StockInfo this.txtLastName.Location = new System.Drawing.Point(113, 75); this.txtLastName.Name = "txtLastName"; this.txtLastName.Size = new System.Drawing.Size(188, 23); - this.txtLastName.TabIndex = 9; + this.txtLastName.TabIndex = 2; // // label5 // @@ -159,12 +159,19 @@ namespace StockInfo this.gbAddress.TabStop = false; this.gbAddress.Text = "Addresses"; // + // txtDestination + // + this.txtDestination.Location = new System.Drawing.Point(148, 106); + this.txtDestination.Name = "txtDestination"; + this.txtDestination.Size = new System.Drawing.Size(146, 23); + this.txtDestination.TabIndex = 12; + // // txtNation // this.txtNation.Location = new System.Drawing.Point(84, 135); this.txtNation.Name = "txtNation"; this.txtNation.Size = new System.Drawing.Size(210, 23); - this.txtNation.TabIndex = 9; + this.txtNation.TabIndex = 13; // // label10 // @@ -180,7 +187,7 @@ namespace StockInfo this.txtZipCode.Location = new System.Drawing.Point(84, 106); this.txtZipCode.Name = "txtZipCode"; this.txtZipCode.Size = new System.Drawing.Size(58, 23); - this.txtZipCode.TabIndex = 7; + this.txtZipCode.TabIndex = 11; // // label9 // @@ -196,7 +203,7 @@ namespace StockInfo this.txtStreet2.Location = new System.Drawing.Point(84, 77); this.txtStreet2.Name = "txtStreet2"; this.txtStreet2.Size = new System.Drawing.Size(210, 23); - this.txtStreet2.TabIndex = 5; + this.txtStreet2.TabIndex = 10; // // label8 // @@ -212,7 +219,7 @@ namespace StockInfo this.txtStreet.Location = new System.Drawing.Point(84, 48); this.txtStreet.Name = "txtStreet"; this.txtStreet.Size = new System.Drawing.Size(210, 23); - this.txtStreet.TabIndex = 3; + this.txtStreet.TabIndex = 9; // // label7 // @@ -232,6 +239,7 @@ namespace StockInfo this.rdbInvoiceAddr.TabIndex = 1; this.rdbInvoiceAddr.Text = "Invoice Address"; this.rdbInvoiceAddr.UseVisualStyleBackColor = true; + this.rdbInvoiceAddr.CheckedChanged += new System.EventHandler(this.rdbInvoiceAddr_CheckedChanged); // // rdbHome // @@ -240,17 +248,18 @@ namespace StockInfo this.rdbHome.Location = new System.Drawing.Point(7, 23); this.rdbHome.Name = "rdbHome"; this.rdbHome.Size = new System.Drawing.Size(103, 19); - this.rdbHome.TabIndex = 0; + this.rdbHome.TabIndex = 8; this.rdbHome.TabStop = true; this.rdbHome.Text = "Home Address"; this.rdbHome.UseVisualStyleBackColor = true; + this.rdbHome.CheckedChanged += new System.EventHandler(this.rdbInvoiceAddr_CheckedChanged); // // txtClearingNo // this.txtClearingNo.Location = new System.Drawing.Point(113, 191); this.txtClearingNo.Name = "txtClearingNo"; this.txtClearingNo.Size = new System.Drawing.Size(42, 23); - this.txtClearingNo.TabIndex = 12; + this.txtClearingNo.TabIndex = 6; // // label6 // @@ -266,7 +275,7 @@ namespace StockInfo this.txtAccountNr.Location = new System.Drawing.Point(161, 191); this.txtAccountNr.Name = "txtAccountNr"; this.txtAccountNr.Size = new System.Drawing.Size(152, 23); - this.txtAccountNr.TabIndex = 13; + this.txtAccountNr.TabIndex = 7; // // btnAddSave // @@ -287,13 +296,6 @@ namespace StockInfo this.btnClose.Text = "Close"; this.btnClose.UseVisualStyleBackColor = true; // - // txtDestination - // - this.txtDestination.Location = new System.Drawing.Point(148, 106); - this.txtDestination.Name = "txtDestination"; - this.txtDestination.Size = new System.Drawing.Size(146, 23); - this.txtDestination.TabIndex = 10; - // // frmPerson // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); diff --git a/StockInfo/frmPerson.cs b/StockInfo/frmPerson.cs index 880a2bc..b9eb87b 100644 --- a/StockInfo/frmPerson.cs +++ b/StockInfo/frmPerson.cs @@ -1,4 +1,5 @@ using DataDomain; +using Helpers; using StockDAL.Interface; using System; using System.Collections.Generic; @@ -16,6 +17,7 @@ namespace StockInfo { private readonly IPersonRepository _personRepository; private readonly IAddressRepository _addressRepository; + private bool radioOk = false; public frmPerson(IPersonRepository personRepository, IAddressRepository addressRepository) { @@ -32,6 +34,66 @@ namespace StockInfo private void btnAddSave_Click(object sender, EventArgs e) { + (Person person, bool OK) = validatePerson(); + if (OK) + { + person = _personRepository.SavePerson(person); + PersonId = person.Id; + } + else + { + + } + + } + + private (Person person, bool OK) validatePerson() + { + var person = new Person(); + person.Id = PersonId; + person.FirstName = txtFirstName.Text; + person.LastName = txtLastName.Text; + person.NickName = txtNickName.Text; + person.ClearingNo = txtClearingNo.Text.IsNumeric()?int.Parse(txtClearingNo.Text):0; + person.AccountNo = txtAccountNr.Text.IsNumeric() ? int.Parse(txtAccountNr.Text) : 0; + person.Born = txtPersonNr.Text; + person.Comments = txtComment.Text; + HomeAddressId = AddressSave(); + person.HomeAddress = HomeAddressId; + person.InvoiceAddress = InvoiceAddressId; + return (person, true); + } + + private int AddressSave() + { + var retval = 0; + bool changed = false; + Address address = new Address(); + (address.Street, changed) = checkString(txtStreet.Text); + (address.Street2, changed) = checkString(txtStreet2.Text); + (address.Zipcode, changed) = checkInt(txtZipCode.Text); + (address.Destination, changed) = checkString(txtDestination.Text); + (address.Nation, changed) = checkString(txtNation.Text); + if (changed) + { + retval =_addressRepository.SaveAddress(address).Id; + } + return retval; + } + + private (string, bool ) checkString(string text) + { + bool chgd = !string.IsNullOrWhiteSpace(text); + return (text, chgd); + } + private (int, bool) checkInt(string text) + { + bool chgd = false; + if(!string.IsNullOrWhiteSpace(text)) + { + chgd = int.Parse(text) > 0; + } + return (chgd?int.Parse(text):0, chgd); } private void frmPerson_Load(object sender, EventArgs e) @@ -55,7 +117,12 @@ namespace StockInfo txtComment.Text = person.Comments; txtClearingNo.Text = person.ClearingNo.ToString(); txtAccountNr.Text = person.AccountNo.ToString(); - var address = _addressRepository.GetAddressById(HomeAddressId); + ShowAddressFrom(HomeAddressId); + } + + private void ShowAddressFrom(int AddrId) + { + var address = _addressRepository.GetAddressById(AddrId); if (address != null) { txtStreet.Text = address.Street; @@ -75,11 +142,44 @@ namespace StockInfo txtComment.Text = ""; txtClearingNo.Text = ""; txtAccountNr.Text = ""; + ClearAddress(); + HomeAddressId = 0; + InvoiceAddressId = 0; + rdbHome.Checked = true; + } + + private void ClearAddress() + { txtStreet.Text = ""; txtStreet2.Text = ""; txtZipCode.Text = ""; txtDestination.Text = ""; txtNation.Text = ""; } + + private void rdbInvoiceAddr_CheckedChanged(object sender, EventArgs e) + { + if (!radioOk) + { + if (rdbInvoiceAddr.Checked) + { + HomeAddressId = AddressSave(); + ClearAddress(); + ShowAddressFrom(InvoiceAddressId); + radioOk = true; + } + else + { + InvoiceAddressId = AddressSave(); + ClearAddress(); + ShowAddressFrom(HomeAddressId); + radioOk = true; + } + } + else + { + radioOk = false; + } + } } }