Added possibilities for owners and there addresses

This commit is contained in:
2021-03-08 23:46:24 +01:00
parent b4baefd68f
commit e61719e037
10 changed files with 253 additions and 40 deletions

View File

@ -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;
}
}
}

View File

@ -19,5 +19,57 @@ namespace StockDAL
select prs).FirstOrDefault();
return entity;
}
public IEnumerable<Person> 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;
}
}
}

View File

@ -10,5 +10,6 @@ namespace StockDAL.Interface
public interface IAddressRepository
{
Address GetAddressById(int AddressId);
Address SaveAddress(Address address);
}
}

View File

@ -9,6 +9,8 @@ namespace StockDAL.Interface
{
public interface IPersonRepository
{
IEnumerable<Person> GetAllOwners();
Person GetPersonById(int personId);
Person SavePerson(Person person);
}
}

BIN
StockInfo/Stocks.db-shm Normal file

Binary file not shown.

0
StockInfo/Stocks.db-wal Normal file
View File

View File

@ -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;
}
}

View File

@ -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)
{
}
}
}

View File

@ -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);

View File

@ -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;
}
}
}
}