diff --git a/StockBL.Interface/IPersonStockFacade.cs b/StockBL.Interface/IPersonStockFacade.cs index 2c985af..a8462f8 100644 --- a/StockBL.Interface/IPersonStockFacade.cs +++ b/StockBL.Interface/IPersonStockFacade.cs @@ -1,8 +1,12 @@ using System; +using System.Collections.Generic; +using DataDomain; namespace StockBL.Interface { - public class IPersonStockFacade + public interface IPersonStockFacade { + IEnumerable GetAllSharesConnectedTo(int personId); + System.Collections.Generic.IEnumerable GetUnconnectedShares(); } } diff --git a/StockBL.Interface/StockBL.Interface.csproj b/StockBL.Interface/StockBL.Interface.csproj index f208d30..a3f6e2a 100644 --- a/StockBL.Interface/StockBL.Interface.csproj +++ b/StockBL.Interface/StockBL.Interface.csproj @@ -4,4 +4,8 @@ net5.0 + + + + diff --git a/StockBL/PersonStockFacade.cs b/StockBL/PersonStockFacade.cs index fba7ba3..4bb078e 100644 --- a/StockBL/PersonStockFacade.cs +++ b/StockBL/PersonStockFacade.cs @@ -1,10 +1,47 @@ -using StockBL.Interface; +using DataDomain; +using StockBL.Interface; +using StockDAL.Interface; using System; +using System.Collections.Generic; +using System.Linq; namespace StockBL { public class PersonStockFacade : IPersonStockFacade { + private readonly IStockPersonConnect _stockPersonConnect; + private readonly IStockRepository _stockRepository; + public PersonStockFacade(IStockPersonConnect stockPersonConnect, IStockRepository stockRepository) + { + _stockPersonConnect = stockPersonConnect; + _stockRepository = stockRepository; + } + + public IEnumerable GetUnconnectedShares() + { + var stockList = _stockRepository.GetAllStocks(); + var connectList = _stockPersonConnect.GetAllConnectedStocks(); + + + var stcList = (from st in stockList + where connectList.Any(co => st.Id == co.StockId) + select st).ToList(); + + var sList = stockList.Except(stcList).ToList(); + + return sList; + } + + public IEnumerable GetAllSharesConnectedTo(int personId) + { + var personConnections = _stockPersonConnect.GetAllConnectionsByPersId(personId); + var stockList = _stockRepository.GetAllStocks(); + + var stcList = (from st in stockList + where personConnections.Any(pc => st.Id == pc.StockId) + select st).ToList(); + return stcList; + } } } diff --git a/StockBL/StockBL.csproj b/StockBL/StockBL.csproj index 5517349..087b712 100644 --- a/StockBL/StockBL.csproj +++ b/StockBL/StockBL.csproj @@ -5,7 +5,10 @@ + + + diff --git a/StockDAL/StockPersonConnect.cs b/StockDAL/StockPersonConnect.cs index 2580fad..afd9634 100644 --- a/StockDAL/StockPersonConnect.cs +++ b/StockDAL/StockPersonConnect.cs @@ -20,6 +20,15 @@ namespace StockDAL return connections; } + public IEnumerable GetAllConnectedStocks() + { + using var context = new StockContext(); + var entities = (from spc in context.PersonStocks + where spc.PersonId != 0 + select spc).ToList(); + return entities; + } + public PersonStock SavePersonStockConnection(PersonStock personStock) { using var context = new StockContext(); @@ -46,5 +55,18 @@ namespace StockDAL return entity; } + + public void RemoveConnectedShare(PersonStock personStock) + { + using var context = new StockContext(); + var entity = (from ps in context.PersonStocks + where ps.StockId == personStock.StockId + select ps).FirstOrDefault(); + if (entity != null) + { + context.PersonStocks.Remove(entity); + context.SaveChanges(); + } + } } -} +} \ No newline at end of file diff --git a/StockDal.Interface/IStockPersonConnect.cs b/StockDal.Interface/IStockPersonConnect.cs index 9c4ec5a..45cb133 100644 --- a/StockDal.Interface/IStockPersonConnect.cs +++ b/StockDal.Interface/IStockPersonConnect.cs @@ -9,7 +9,9 @@ namespace StockDAL.Interface { public interface IStockPersonConnect { + IEnumerable GetAllConnectedStocks(); IEnumerable GetAllConnectionsByPersId(int personId); + void RemoveConnectedShare(PersonStock personStock); PersonStock SavePersonStockConnection(PersonStock personStock); } } diff --git a/StockInfo/Program.cs b/StockInfo/Program.cs index dcd1d12..e6fec06 100644 --- a/StockInfo/Program.cs +++ b/StockInfo/Program.cs @@ -1,4 +1,6 @@ using Autofac; +using StockBL; +using StockBL.Interface; using StockDal; using StockDal.Interface; using StockDAL; @@ -24,7 +26,14 @@ namespace StockInfo Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Container = Configure(); - Application.Run(new frmInitial( Container.Resolve(), Container.Resolve(), Container.Resolve(), Container.Resolve(), Container.Resolve())); + Application.Run(new frmInitial( + Container.Resolve(), + Container.Resolve(), + Container.Resolve(), + Container.Resolve(), + Container.Resolve(), + Container.Resolve() + )); } static IContainer Configure() @@ -35,6 +44,7 @@ namespace StockInfo builder.RegisterType().As(); builder.RegisterType().As(); builder.RegisterType().As(); + builder.RegisterType().As(); builder.RegisterType(); return builder.Build(); } diff --git a/StockInfo/StockInfo.csproj b/StockInfo/StockInfo.csproj index d99e4bb..2d037a9 100644 --- a/StockInfo/StockInfo.csproj +++ b/StockInfo/StockInfo.csproj @@ -26,6 +26,8 @@ + + diff --git a/StockInfo/frmInitial.cs b/StockInfo/frmInitial.cs index 5e5692b..9235594 100644 --- a/StockInfo/frmInitial.cs +++ b/StockInfo/frmInitial.cs @@ -13,6 +13,7 @@ using System.Windows.Forms; using System.Text.Json; using System.IO; using Helpers; +using StockBL.Interface; namespace StockInfo { @@ -23,6 +24,7 @@ namespace StockInfo private readonly IPersonRepository _personRepository; private readonly IAddressRepository _addressRepository; private readonly IStockPersonConnect _stockPersonConnect; + private readonly IPersonStockFacade _personStockFacade; private frmRegisterStock regWindow; private frmMyStocks stockWindow; private frmSelling sellWindow; @@ -35,7 +37,8 @@ namespace StockInfo IStockMarketRepository stockMarketRepository, IPersonRepository personRepository, IAddressRepository addressRepository, - IStockPersonConnect stockPersonConnect) + IStockPersonConnect stockPersonConnect, + IPersonStockFacade personStockFacade) { InitializeComponent(); _stockRepository = stockMemberRepository; @@ -43,6 +46,7 @@ namespace StockInfo _personRepository = personRepository; _addressRepository = addressRepository; _stockPersonConnect = stockPersonConnect; + _personStockFacade = personStockFacade; } private void Form1_Load(object sender, EventArgs e) @@ -191,7 +195,7 @@ namespace StockInfo else { var person = _personRepository.GetPersonById(SelectedPersonId); - personShareConnect = new frmPersonShareConnect(); + personShareConnect = new frmPersonShareConnect(_personStockFacade,_stockPersonConnect,_stockRepository); Cursor.Current = Cursors.WaitCursor; personShareConnect.ConnectPerson = person; Cursor.Current = DefaultCursor; diff --git a/StockInfo/frmPersonShareConnect.Designer.cs b/StockInfo/frmPersonShareConnect.Designer.cs index eec3bc6..729a89c 100644 --- a/StockInfo/frmPersonShareConnect.Designer.cs +++ b/StockInfo/frmPersonShareConnect.Designer.cs @@ -31,11 +31,12 @@ namespace StockInfo { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmPersonShareConnect)); this.lstShares = new System.Windows.Forms.ListBox(); - this.listBox1 = new System.Windows.Forms.ListBox(); - this.btnToPerson = new System.Windows.Forms.Button(); + this.lstPersConnected = new System.Windows.Forms.ListBox(); + this.btnDisconnect = new System.Windows.Forms.Button(); this.lblShareHolder = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); - this.button1 = new System.Windows.Forms.Button(); + this.btnConnect = new System.Windows.Forms.Button(); + this.btnClose = new System.Windows.Forms.Button(); this.SuspendLayout(); // // lstShares @@ -47,23 +48,24 @@ namespace StockInfo this.lstShares.Size = new System.Drawing.Size(173, 349); this.lstShares.TabIndex = 0; // - // listBox1 + // lstPersConnected // - this.listBox1.FormattingEnabled = true; - this.listBox1.ItemHeight = 15; - this.listBox1.Location = new System.Drawing.Point(270, 35); - this.listBox1.Name = "listBox1"; - this.listBox1.Size = new System.Drawing.Size(173, 349); - this.listBox1.TabIndex = 1; + this.lstPersConnected.FormattingEnabled = true; + this.lstPersConnected.ItemHeight = 15; + this.lstPersConnected.Location = new System.Drawing.Point(270, 35); + this.lstPersConnected.Name = "lstPersConnected"; + this.lstPersConnected.Size = new System.Drawing.Size(173, 349); + this.lstPersConnected.TabIndex = 1; // - // btnToPerson + // btnDisconnect // - this.btnToPerson.Image = ((System.Drawing.Image)(resources.GetObject("btnToPerson.Image"))); - this.btnToPerson.Location = new System.Drawing.Point(214, 202); - this.btnToPerson.Name = "btnToPerson"; - this.btnToPerson.Size = new System.Drawing.Size(49, 23); - this.btnToPerson.TabIndex = 2; - this.btnToPerson.UseVisualStyleBackColor = true; + this.btnDisconnect.Image = ((System.Drawing.Image)(resources.GetObject("btnDisconnect.Image"))); + this.btnDisconnect.Location = new System.Drawing.Point(214, 202); + this.btnDisconnect.Name = "btnDisconnect"; + this.btnDisconnect.Size = new System.Drawing.Size(49, 23); + this.btnDisconnect.TabIndex = 2; + this.btnDisconnect.UseVisualStyleBackColor = true; + this.btnDisconnect.Click += new System.EventHandler(this.btnDisconnect_Click); // // lblShareHolder // @@ -83,25 +85,37 @@ namespace StockInfo this.label1.TabIndex = 4; this.label1.Text = "Uncoupled Shares"; // - // button1 + // btnConnect // - this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image"))); - this.button1.Location = new System.Drawing.Point(215, 173); - this.button1.Name = "button1"; - this.button1.Size = new System.Drawing.Size(49, 23); - this.button1.TabIndex = 5; - this.button1.UseVisualStyleBackColor = true; + this.btnConnect.Image = ((System.Drawing.Image)(resources.GetObject("btnConnect.Image"))); + this.btnConnect.Location = new System.Drawing.Point(215, 173); + this.btnConnect.Name = "btnConnect"; + this.btnConnect.Size = new System.Drawing.Size(49, 23); + this.btnConnect.TabIndex = 5; + this.btnConnect.UseVisualStyleBackColor = true; + this.btnConnect.Click += new System.EventHandler(this.btnConnect_Click); + // + // btnClose + // + this.btnClose.Location = new System.Drawing.Point(422, 415); + this.btnClose.Name = "btnClose"; + this.btnClose.Size = new System.Drawing.Size(75, 23); + this.btnClose.TabIndex = 6; + this.btnClose.Text = "Close"; + this.btnClose.UseVisualStyleBackColor = true; + this.btnClose.Click += new System.EventHandler(this.btnClose_Click); // // frmPersonShareConnect // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(509, 450); - this.Controls.Add(this.button1); + this.Controls.Add(this.btnClose); + this.Controls.Add(this.btnConnect); this.Controls.Add(this.label1); this.Controls.Add(this.lblShareHolder); - this.Controls.Add(this.btnToPerson); - this.Controls.Add(this.listBox1); + this.Controls.Add(this.btnDisconnect); + this.Controls.Add(this.lstPersConnected); this.Controls.Add(this.lstShares); this.Name = "frmPersonShareConnect"; this.Text = "frmPersonShareConnect"; @@ -114,10 +128,11 @@ namespace StockInfo #endregion private System.Windows.Forms.ListBox lstShares; - private System.Windows.Forms.ListBox listBox1; - private System.Windows.Forms.Button btnToPerson; + private System.Windows.Forms.ListBox lstPersConnected; + private System.Windows.Forms.Button btnDisconnect; private System.Windows.Forms.Label lblShareHolder; private System.Windows.Forms.Label label1; - private System.Windows.Forms.Button button1; + private System.Windows.Forms.Button btnConnect; + private System.Windows.Forms.Button btnClose; } } \ No newline at end of file diff --git a/StockInfo/frmPersonShareConnect.cs b/StockInfo/frmPersonShareConnect.cs index 97f7d06..5e96679 100644 --- a/StockInfo/frmPersonShareConnect.cs +++ b/StockInfo/frmPersonShareConnect.cs @@ -1,4 +1,6 @@ using DataDomain; +using StockBL.Interface; +using StockDAL.Interface; using System; using System.Collections.Generic; using System.ComponentModel; @@ -13,16 +15,75 @@ namespace StockInfo { public partial class frmPersonShareConnect : Form { + private readonly IPersonStockFacade _personStockFacade; + private readonly IStockPersonConnect _stockPersonConnect; + private readonly IStockRepository _stockRepository; + public Person ConnectPerson { get; set; } - public frmPersonShareConnect() + public frmPersonShareConnect(IPersonStockFacade personStockFacade, IStockPersonConnect stockPersonConnect,IStockRepository stockRepository) { InitializeComponent(); + _personStockFacade = personStockFacade; + _stockPersonConnect = stockPersonConnect; + _stockRepository = stockRepository; } private void frmPersonShareConnect_Shown(object sender, EventArgs e) { this.Text = $"{ConnectPerson.Id} - {ConnectPerson.FirstName} {ConnectPerson.LastName}'s Shares"; this.lblShareHolder.Text = $"{ConnectPerson.FirstName} {ConnectPerson.LastName}'s Shares"; + RefreshShareList(); + RefreshConnectedList(); + } + + private void RefreshShareList() + { + var dataSource = _personStockFacade.GetUnconnectedShares(); + lstShares.DataSource = dataSource; + lstShares.DisplayMember = "StockId"; + lstShares.ValueMember = "Id"; + lstShares.Refresh(); + } + + private void RefreshConnectedList() + { + var dataSource = _personStockFacade.GetAllSharesConnectedTo(ConnectPerson.Id); + lstPersConnected.DataSource = dataSource; + lstPersConnected.DisplayMember = "StockId"; + lstPersConnected.ValueMember = "Id"; + lstPersConnected.Refresh(); + } + + + private void btnConnect_Click(object sender, EventArgs e) + { + if (lstShares.SelectedIndex != -1) + { + PersonStock ps = new PersonStock(); + ps.PersonId = ConnectPerson.Id; + ps.StockId = int.Parse(lstShares.SelectedValue.ToString()); + _stockPersonConnect.SavePersonStockConnection(ps); + RefreshShareList(); + RefreshConnectedList(); + } + } + + private void btnDisconnect_Click(object sender, EventArgs e) + { + if (lstPersConnected.SelectedIndex != -1) + { + PersonStock ps = new PersonStock(); + ps.PersonId = ConnectPerson.Id; + ps.StockId = int.Parse(lstPersConnected.SelectedValue.ToString()); + _stockPersonConnect.RemoveConnectedShare(ps); + RefreshShareList(); + RefreshConnectedList(); + } + } + + private void btnClose_Click(object sender, EventArgs e) + { + this.Close(); } } } diff --git a/StockInfo/frmPersonShareConnect.resx b/StockInfo/frmPersonShareConnect.resx index bf93be3..32d80a8 100644 --- a/StockInfo/frmPersonShareConnect.resx +++ b/StockInfo/frmPersonShareConnect.resx @@ -58,22 +58,22 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - + - iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 - YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACiSURBVDhPY0AH37594wPie0D8H4ihoiQAoKYlq1evBmkm - 3QCghqgbN2789/DwIN0AoGKFj0CQlJT038nJCWYAQQzTzAzERydOnAjWjI5BCnGxYQbUHzlyBC6BjvEa - ACSsnj9//ic4OBgugY4JGXCvoqICLogN09wAyrxAcSBCDaAsGkEAyCE/IcEAUAAjKRPCGAAoiJKZCGEM - ABQkITszMAAAZBgNueqz2kUAAAAASUVORK5CYII= + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO + wAAADsABataJCQAAAKJJREFUOE9jQAffvn3jA+J7QPwfiKGiJACgpiWrV68GaSbdAKCGqBs3bvz38PAg + 3QCgYoWPQJCUlPTfyckJZgBBDNPMDMRHJ06cCNaMjkEKcbFhBtQfOXIELoGO8RoAJKyeP3/+Jzg4GC6B + jgkZcK+iogIuiA3T3ADKvEBxIEINoCwaQQDIIT8hwQBQACMpE8IYACiIkpkIYQwAFCQhOzMwAABkGA25 + 6rPaRQAAAABJRU5ErkJggg== - + - iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 - YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAACmSURBVDhPYyAGfPv27T8Q3wNiPqgQaQBkwNq1a0GGLIEK - kQZABnh4ePy/efMmyJAoqDAEgCSJwU5OTv9TUlL+fwQCIF8Bqh1iAEgShHGxkfHkyZNBckeBmJksA0D4 - 6NGjIPl6sg0ICQn5/+LFiz9ANVZkGQDClZWVIDX3BsYAir1AUSBijUZiMEgz1oT0//9/kCGEMO6kDAJo - irFhyjMTEGPJzgwMAF29/539maLfAAAAAElFTkSuQmCC + iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAAO + wAAADsABataJCQAAAKZJREFUOE9jIAZ8+/btPxDfA2I+qBBpAGTA2rVrQYYsgQqRBkAGeHh4/L958ybI + kCioMASAJInBTk5O/1NSUv5/BAIgXwGqHWIASBKEcbGR8eTJk0FyR4GYmSwDQPjo0aMg+XqyDQgJCfn/ + 4sWLP0A1VmQZAMKVlZUgNfcGxgCKvUBRIGKNRmIwSDPWhPT//3+QIYQw7qQMAmiKsWHKMxMQY8nODAwA + Xb3/nf2Zot8AAAAASUVORK5CYII= \ No newline at end of file