diff --git a/PrecastConcreteFileImplement/DataFileSingleton.cs b/PrecastConcreteFileImplement/DataFileSingleton.cs index afd8cae..c9d656c 100644 --- a/PrecastConcreteFileImplement/DataFileSingleton.cs +++ b/PrecastConcreteFileImplement/DataFileSingleton.cs @@ -14,9 +14,11 @@ namespace PrecastConcretePlantFileImplement private readonly string ComponentFileName = "Component.xml"; private readonly string OrderFileName = "Order.xml"; private readonly string ReinforcedFileName = "Reinforced.xml"; + private readonly string ClientFileName = "Client.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Reinforceds { get; private set; } + public List Clients { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -30,6 +32,8 @@ namespace PrecastConcretePlantFileImplement public void SaveReinforceds() => SaveData(Reinforceds, ReinforcedFileName, "Reiforceds", x => x.GetXElement); public void SaveOrders() => SaveData(Orders, OrderFileName, "Orders", x + => x.GetXElement); + public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); private DataFileSingleton() { @@ -38,6 +42,7 @@ namespace PrecastConcretePlantFileImplement Reinforceds = LoadData(ReinforcedFileName, "Reinforced", x => Reinforced.Create(x)!)!; Orders = LoadData(OrderFileName, "Order", x => Order.Create(x)!)!; + Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/PrecastConcreteFileImplement/Implements/ClientStorage.cs b/PrecastConcreteFileImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..61074f0 --- /dev/null +++ b/PrecastConcreteFileImplement/Implements/ClientStorage.cs @@ -0,0 +1,90 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantFileImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantFileImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataFileSingleton source; + + public ClientStorage() + { + source = DataFileSingleton.GetInstance(); + } + + public List GetFullList() + { + return source.Clients.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (!string.IsNullOrEmpty(model.Email)) + { + return source.Clients + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (model.Id.HasValue) + { + return source.Clients.FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + return source.Clients.FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))?.GetViewModel; + } + return new(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = source.Clients.Count > 0 ? source.Clients.Max(x => x.Id) + 1 : 1; + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + source.Clients.Add(newClient); + source.SaveClients(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + source.SaveClients(); + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client != null) + { + source.Clients.Remove(client); + source.SaveClients(); + return client.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcreteFileImplement/Implements/OrderStorage.cs b/PrecastConcreteFileImplement/Implements/OrderStorage.cs index 66a9c6d..5e98405 100644 --- a/PrecastConcreteFileImplement/Implements/OrderStorage.cs +++ b/PrecastConcreteFileImplement/Implements/OrderStorage.cs @@ -47,18 +47,20 @@ namespace PrecastConcretePlantFileImplement.Implements public List GetFilteredList(OrderSearchModel model) { - if (!model.Id.HasValue && (model.DateFrom == null || model.DateTo == null)) - { - return new(); - } + if (model.Id.HasValue) { return source.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); } - else + else if(model.DateFrom != null && model.DateTo != null) { return source.Orders.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo).Select(x => x.GetViewModel).ToList(); } + else if (model.ClientId.HasValue) + { + return source.Orders.Where(x => x.ClientId == model.ClientId).Select(x => GetViewModel(x)).ToList(); + } + return new(); } public List GetFullList() diff --git a/PrecastConcreteFileImplement/Models/Client.cs b/PrecastConcreteFileImplement/Models/Client.cs new file mode 100644 index 0000000..f30e3f0 --- /dev/null +++ b/PrecastConcreteFileImplement/Models/Client.cs @@ -0,0 +1,70 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Xml.Linq; + +namespace PrecastConcretePlantFileImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; private set; } = string.Empty; + public string Password { get; private set; } = string.Empty; + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password, + }; + } + public static Client? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Client() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ClientFIO = element.Element("ClientFIO")!.Value, + Email = element.Element("Email")!.Value, + Password = element.Element("Password")!.Value + }; + } + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + public XElement GetXElement => new("Client", + new XAttribute("Id", Id), + new XElement("ClientFIO", ClientFIO), + new XElement("Email", Email), + new XElement("Password", Password)); + } +} diff --git a/PrecastConcreteFileImplement/Models/Order.cs b/PrecastConcreteFileImplement/Models/Order.cs index 9175b57..c7db726 100644 --- a/PrecastConcreteFileImplement/Models/Order.cs +++ b/PrecastConcreteFileImplement/Models/Order.cs @@ -13,6 +13,7 @@ namespace PrecastConcretePlantFileImplement.Models { public class Order : IOrderModel { + public int ClientId { get; private set; } public int ReinforcedId { get; private set; } public int Count { get; private set; } @@ -35,6 +36,7 @@ namespace PrecastConcretePlantFileImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, ReinforcedId = model.ReinforcedId, Count = model.Count, Sum = model.Sum, @@ -55,7 +57,7 @@ namespace PrecastConcretePlantFileImplement.Models { Id = Convert.ToInt32(element.Attribute("Id")!.Value), ReinforcedId = Convert.ToInt32(element.Element("ReinforcedId")!.Value), - + ClientId=Convert.ToInt32(element.Element("ClientId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), element.Element("Status")!.Value), @@ -82,6 +84,7 @@ namespace PrecastConcretePlantFileImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, ReinforcedId = ReinforcedId, Count = Count, Sum = Sum, @@ -92,6 +95,7 @@ namespace PrecastConcretePlantFileImplement.Models public XElement GetXElement => new("Order", new XAttribute("Id", Id), + new XAttribute("ClientId",ClientId.ToString()), new XElement("ReinforcedId", ReinforcedId.ToString()), new XElement("Count", Count.ToString()), new XElement("Sum", Sum.ToString()), diff --git a/PrecastConcretePlant/FormClients.Designer.cs b/PrecastConcretePlant/FormClients.Designer.cs new file mode 100644 index 0000000..58b47d9 --- /dev/null +++ b/PrecastConcretePlant/FormClients.Designer.cs @@ -0,0 +1,99 @@ +namespace PrecastConcretePlantView +{ + partial class FormClients + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.dataGridView = new System.Windows.Forms.DataGridView(); + this.buttonDel = new System.Windows.Forms.Button(); + this.buttonRef = new System.Windows.Forms.Button(); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit(); + this.SuspendLayout(); + // + // dataGridView + // + this.dataGridView.AllowUserToAddRows = false; + this.dataGridView.AllowUserToDeleteRows = false; + this.dataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dataGridView.Dock = System.Windows.Forms.DockStyle.Left; + this.dataGridView.Location = new System.Drawing.Point(0, 0); + this.dataGridView.Margin = new System.Windows.Forms.Padding(4); + this.dataGridView.Name = "dataGridView"; + this.dataGridView.ReadOnly = true; + this.dataGridView.RowHeadersVisible = false; + this.dataGridView.RowHeadersWidth = 62; + this.dataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dataGridView.Size = new System.Drawing.Size(408, 360); + this.dataGridView.TabIndex = 0; + // + // buttonDel + // + this.buttonDel.Location = new System.Drawing.Point(429, 14); + this.buttonDel.Margin = new System.Windows.Forms.Padding(4); + this.buttonDel.Name = "buttonDel"; + this.buttonDel.Size = new System.Drawing.Size(88, 20); + this.buttonDel.TabIndex = 3; + this.buttonDel.Text = "Удалить"; + this.buttonDel.UseVisualStyleBackColor = true; + this.buttonDel.Click += new System.EventHandler(this.ButtonDel_Click); + // + // buttonRef + // + this.buttonRef.Location = new System.Drawing.Point(429, 61); + this.buttonRef.Margin = new System.Windows.Forms.Padding(4); + this.buttonRef.Name = "buttonRef"; + this.buttonRef.Size = new System.Drawing.Size(88, 20); + this.buttonRef.TabIndex = 4; + this.buttonRef.Text = "Обновить"; + this.buttonRef.UseVisualStyleBackColor = true; + this.buttonRef.Click += new System.EventHandler(this.ButtonRef_Click); + // + // FormClients + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(541, 360); + this.Controls.Add(this.buttonRef); + this.Controls.Add(this.buttonDel); + this.Controls.Add(this.dataGridView); + this.Margin = new System.Windows.Forms.Padding(4); + this.Name = "FormClients"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Клиенты"; + this.Load += new System.EventHandler(this.FormClients_Load); + ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); + this.ResumeLayout(false); + + } + + #endregion + + private DataGridView dataGridView; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/PrecastConcretePlant/FormClients.cs b/PrecastConcretePlant/FormClients.cs new file mode 100644 index 0000000..e174da1 --- /dev/null +++ b/PrecastConcretePlant/FormClients.cs @@ -0,0 +1,84 @@ +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.BusinessLogicsContracts; + +namespace PrecastConcretePlantView +{ + public partial class FormClients : Form + { + private readonly ILogger _logger; + + private readonly IClientLogic _logic; + + public FormClients(ILogger logger, IClientLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + + private void FormClients_Load(object sender, EventArgs e) + { + LoadData(); + } + + private void LoadData() + { + try + { + var list = _logic.ReadList(null); + if (list != null) + { + dataGridView.DataSource = list; + dataGridView.Columns["Id"].Visible = false; + dataGridView.Columns["ClientFIO"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка клиентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + + private void ButtonDel_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + if (MessageBox.Show("Удалить запись?", "Вопрос", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) + { + int id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value); + try + { + if (!_logic.Delete(new ClientBindingModel { Id = id })) + { + throw new Exception("Ошибка при удалении. Дополнительная информация в логах."); + } + _logger.LogInformation("Удаление клиента"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления клиента"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + LoadData(); + } + } + } + + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} diff --git a/PrecastConcretePlant/FormClients.resx b/PrecastConcretePlant/FormClients.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/PrecastConcretePlant/FormClients.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/PrecastConcretePlant/FormCreateOrder.Designer.cs b/PrecastConcretePlant/FormCreateOrder.Designer.cs index b3f5325..2fd9452 100644 --- a/PrecastConcretePlant/FormCreateOrder.Designer.cs +++ b/PrecastConcretePlant/FormCreateOrder.Designer.cs @@ -36,6 +36,8 @@ this.SumTextBox = new System.Windows.Forms.TextBox(); this.ButtonCancel = new System.Windows.Forms.Button(); this.SaveButton = new System.Windows.Forms.Button(); + this.label1 = new System.Windows.Forms.Label(); + this.ClientComboBox = new System.Windows.Forms.ComboBox(); this.SuspendLayout(); // // ReinforcedNameLabel @@ -91,7 +93,7 @@ // // ButtonCancel // - this.ButtonCancel.Location = new System.Drawing.Point(219, 113); + this.ButtonCancel.Location = new System.Drawing.Point(218, 170); this.ButtonCancel.Name = "ButtonCancel"; this.ButtonCancel.Size = new System.Drawing.Size(75, 23); this.ButtonCancel.TabIndex = 6; @@ -101,7 +103,7 @@ // // SaveButton // - this.SaveButton.Location = new System.Drawing.Point(138, 113); + this.SaveButton.Location = new System.Drawing.Point(137, 170); this.SaveButton.Name = "SaveButton"; this.SaveButton.Size = new System.Drawing.Size(75, 23); this.SaveButton.TabIndex = 7; @@ -109,11 +111,30 @@ this.SaveButton.UseVisualStyleBackColor = true; this.SaveButton.Click += new System.EventHandler(this.SaveButton_Click); // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(11, 108); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(49, 15); + this.label1.TabIndex = 8; + this.label1.Text = "Клиент:"; + // + // ClientComboBox + // + this.ClientComboBox.FormattingEnabled = true; + this.ClientComboBox.Location = new System.Drawing.Point(96, 105); + this.ClientComboBox.Name = "ClientComboBox"; + this.ClientComboBox.Size = new System.Drawing.Size(201, 23); + this.ClientComboBox.TabIndex = 9; + // // FormCreateOrder // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(309, 153); + this.ClientSize = new System.Drawing.Size(309, 205); + this.Controls.Add(this.ClientComboBox); + this.Controls.Add(this.label1); this.Controls.Add(this.SaveButton); this.Controls.Add(this.ButtonCancel); this.Controls.Add(this.SumTextBox); @@ -139,5 +160,7 @@ private TextBox SumTextBox; private Button ButtonCancel; private Button SaveButton; + private Label label1; + private ComboBox ClientComboBox; } } \ No newline at end of file diff --git a/PrecastConcretePlant/FormCreateOrder.cs b/PrecastConcretePlant/FormCreateOrder.cs index b3a89b2..7a95272 100644 --- a/PrecastConcretePlant/FormCreateOrder.cs +++ b/PrecastConcretePlant/FormCreateOrder.cs @@ -19,13 +19,15 @@ namespace PrecastConcretePlantView private readonly ILogger _logger; private readonly IReinforcedLogic _logicP; private readonly IOrderLogic _logicO; + private readonly IClientLogic _logicC; - public FormCreateOrder(ILogger logger, IReinforcedLogic logicP, IOrderLogic logicO) + public FormCreateOrder(ILogger logger, IReinforcedLogic logicP, IOrderLogic logicO, IClientLogic logicC) { InitializeComponent(); _logger = logger; _logicP = logicP; _logicO = logicO; + _logicC = logicC; LoadData(); } @@ -50,6 +52,23 @@ namespace PrecastConcretePlantView _logger.LogError(ex, "Ошибка загрузки списка ЖБИ"); MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } + _logger.LogInformation("Загрузка списка клиентов"); + try + { + var list = _logicC.ReadList(null); + if (list != null) + { + ClientComboBox.DisplayMember = "ClientFIO"; + ClientComboBox.ValueMember = "Id"; + ClientComboBox.DataSource = list; + ClientComboBox.SelectedItem = null; + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки списка клиентов"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + } } private void FormCreateOrder_Load(object sender, EventArgs e) @@ -100,6 +119,11 @@ namespace PrecastConcretePlantView MessageBox.Show("Выберите ЖБИ", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } + if (ClientComboBox.SelectedValue == null) + { + MessageBox.Show("Выберите клиента", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } _logger.LogInformation("Создание заказа"); @@ -108,6 +132,7 @@ namespace PrecastConcretePlantView var operationResult = _logicO.CreateOrder(new OrderBindingModel { ReinforcedId = Convert.ToInt32(ReinforcedComboBox.SelectedValue), + ClientId = Convert.ToInt32(ClientComboBox.SelectedValue), Count = Convert.ToInt32(CountTextBox.Text), Sum = Convert.ToDouble(SumTextBox.Text) }); diff --git a/PrecastConcretePlant/FormMain.Designer.cs b/PrecastConcretePlant/FormMain.Designer.cs index ffd4acc..ed11cf3 100644 --- a/PrecastConcretePlant/FormMain.Designer.cs +++ b/PrecastConcretePlant/FormMain.Designer.cs @@ -42,6 +42,7 @@ this.OrderReadyButton = new System.Windows.Forms.Button(); this.IssuedOrderButton = new System.Windows.Forms.Button(); this.UpdateListButton = new System.Windows.Forms.Button(); + this.клиентыToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.MenuStrip.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.DataGridView)).BeginInit(); this.SuspendLayout(); @@ -61,7 +62,8 @@ // this.СправочникиToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.ИзделияToolStripMenuItem, - this.КомпонентыToolStripMenuItem}); + this.КомпонентыToolStripMenuItem, + this.клиентыToolStripMenuItem}); this.СправочникиToolStripMenuItem.Name = "СправочникиToolStripMenuItem"; this.СправочникиToolStripMenuItem.Size = new System.Drawing.Size(94, 20); this.СправочникиToolStripMenuItem.Text = "Cправочники"; @@ -170,6 +172,13 @@ this.UpdateListButton.UseVisualStyleBackColor = true; this.UpdateListButton.Click += new System.EventHandler(this.UpdateListButton_Click); // + // клиентыToolStripMenuItem + // + this.клиентыToolStripMenuItem.Name = "клиентыToolStripMenuItem"; + this.клиентыToolStripMenuItem.Size = new System.Drawing.Size(180, 22); + this.клиентыToolStripMenuItem.Text = "Клиенты"; + this.клиентыToolStripMenuItem.Click += new System.EventHandler(this.клиентыToolStripMenuItem_Click); + // // FormMain // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -210,5 +219,6 @@ private ToolStripMenuItem списокЖБИToolStripMenuItem; private ToolStripMenuItem компонентыПоИзделиямToolStripMenuItem; private ToolStripMenuItem списокЗаказовToolStripMenuItem; + private ToolStripMenuItem клиентыToolStripMenuItem; } } \ No newline at end of file diff --git a/PrecastConcretePlant/FormMain.cs b/PrecastConcretePlant/FormMain.cs index 8dfcd08..8283c70 100644 --- a/PrecastConcretePlant/FormMain.cs +++ b/PrecastConcretePlant/FormMain.cs @@ -47,6 +47,8 @@ namespace PrecastConcretePlantView { DataGridView.DataSource = list; DataGridView.Columns["ReinforcedId"].Visible = false; + DataGridView.Columns["ClientId"].Visible = false; + DataGridView.Columns["DateImplement"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; } _logger.LogInformation("Загрузка заказов"); @@ -209,5 +211,14 @@ namespace PrecastConcretePlantView form.ShowDialog(); } } + + private void клиентыToolStripMenuItem_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider?.GetService(typeof(FormClients)); + if (service is FormClients form) + { + form.ShowDialog(); + } + } } } diff --git a/PrecastConcretePlant/Program.cs b/PrecastConcretePlant/Program.cs index f4c9080..2002fe6 100644 --- a/PrecastConcretePlant/Program.cs +++ b/PrecastConcretePlant/Program.cs @@ -38,10 +38,12 @@ namespace PrecastConcretePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -56,6 +58,8 @@ namespace PrecastConcretePlant services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + } } } \ No newline at end of file diff --git a/PrecastConcretePlantBusinessLogic/BusinessLogic/ClientLogic.cs b/PrecastConcretePlantBusinessLogic/BusinessLogic/ClientLogic.cs new file mode 100644 index 0000000..0f678f5 --- /dev/null +++ b/PrecastConcretePlantBusinessLogic/BusinessLogic/ClientLogic.cs @@ -0,0 +1,123 @@ +using Microsoft.Extensions.Logging; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.BusinessLogicsContracts; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantBusinessLogic.BusinessLogic +{ + public class ClientLogic : IClientLogic + { + private readonly ILogger _logger; + private readonly IClientStorage _clientStorage; + + public ClientLogic(ILogger logger, IClientStorage clientStorage) + { + _logger = logger; + _clientStorage = clientStorage; + } + + public List? ReadList(ClientSearchModel? model) + { + _logger.LogInformation("ReadList. ClientFIO:{ClientFIO}. Id:{Id}", model?.ClientFIO, model?.Id); + var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ClientViewModel? ReadElement(ClientSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ClientFIO:{ClientFIO}. Id:{Id}", model.ClientFIO, model.Id); + var element = _clientStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ClientBindingModel model) + { + CheckModel(model); + if (_clientStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ClientBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_clientStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ClientBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ClientFIO)) + { + throw new ArgumentNullException("Нет имени клиента", nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Email)) + { + throw new ArgumentNullException("Нет логина клиента", nameof(model.ClientFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля учетной записи клиента", nameof(model.ClientFIO)); + } + _logger.LogInformation("Client. ClientFIO:{ClientFIO}. Email:{Email}. Password:{Password}. Id:{Id}", model.ClientFIO, model.Email, model.Password, model.Id); + var element = _clientStorage.GetElement(new ClientSearchModel + { + Email = model.Email + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Клиент с таким логином уже есть"); + } + } + } +} diff --git a/PrecastConcretePlantContracts/BindingModels/ClientBindingModel.cs b/PrecastConcretePlantContracts/BindingModels/ClientBindingModel.cs new file mode 100644 index 0000000..9be409e --- /dev/null +++ b/PrecastConcretePlantContracts/BindingModels/ClientBindingModel.cs @@ -0,0 +1,17 @@ +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantContracts.BindingModels +{ + public class ClientBindingModel : IClientModel + { + public int Id { get; set; } + public string ClientFIO { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string Password { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/PrecastConcretePlantContracts/BindingModels/OrderBindingModel.cs b/PrecastConcretePlantContracts/BindingModels/OrderBindingModel.cs index ec3ad91..7b2701e 100644 --- a/PrecastConcretePlantContracts/BindingModels/OrderBindingModel.cs +++ b/PrecastConcretePlantContracts/BindingModels/OrderBindingModel.cs @@ -11,6 +11,7 @@ namespace PrecastConcretePlantContracts.BindingModels public class OrderBindingModel : IOrderModel { public int Id { get; set; } + public int ClientId { get; set; } public int ReinforcedId { get; set; } public int Count { get; set; } public double Sum { get; set; } diff --git a/PrecastConcretePlantContracts/BusinessLogicsContracts/IClientLogic.cs b/PrecastConcretePlantContracts/BusinessLogicsContracts/IClientLogic.cs new file mode 100644 index 0000000..6a15885 --- /dev/null +++ b/PrecastConcretePlantContracts/BusinessLogicsContracts/IClientLogic.cs @@ -0,0 +1,20 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantContracts.BusinessLogicsContracts +{ + public interface IClientLogic + { + List? ReadList(ClientSearchModel? model); + ClientViewModel? ReadElement(ClientSearchModel model); + bool Create(ClientBindingModel model); + bool Update(ClientBindingModel model); + bool Delete(ClientBindingModel model); + } +} \ No newline at end of file diff --git a/PrecastConcretePlantContracts/SearchModels/ClientSearchModel.cs b/PrecastConcretePlantContracts/SearchModels/ClientSearchModel.cs new file mode 100644 index 0000000..b597b50 --- /dev/null +++ b/PrecastConcretePlantContracts/SearchModels/ClientSearchModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantContracts.SearchModels +{ + public class ClientSearchModel + { + public int? Id { get; set; } + public string? ClientFIO { get; set; } + public string? Email { get; set; } + public string? Password { get; set; } + } +} \ No newline at end of file diff --git a/PrecastConcretePlantContracts/SearchModels/OrderSearchModel.cs b/PrecastConcretePlantContracts/SearchModels/OrderSearchModel.cs index 5500430..1897b6b 100644 --- a/PrecastConcretePlantContracts/SearchModels/OrderSearchModel.cs +++ b/PrecastConcretePlantContracts/SearchModels/OrderSearchModel.cs @@ -9,6 +9,7 @@ namespace PrecastConcretePlantContracts.SearchModels public class OrderSearchModel { public int? Id { get; set; } + public int? ClientId { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/PrecastConcretePlantContracts/StoragesContracts/IClientStorage.cs b/PrecastConcretePlantContracts/StoragesContracts/IClientStorage.cs new file mode 100644 index 0000000..ba8cfe8 --- /dev/null +++ b/PrecastConcretePlantContracts/StoragesContracts/IClientStorage.cs @@ -0,0 +1,23 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.BusinessLogicsContracts; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantContracts.StoragesContracts +{ + public interface IClientStorage + { + List GetFullList(); + List GetFilteredList(ClientSearchModel model); + ClientViewModel? GetElement(ClientSearchModel model); + ClientViewModel? Insert(ClientBindingModel model); + ClientViewModel? Update(ClientBindingModel model); + ClientViewModel? Delete(ClientBindingModel model); + } + +} diff --git a/PrecastConcretePlantContracts/ViewModels/ClientViewModel.cs b/PrecastConcretePlantContracts/ViewModels/ClientViewModel.cs new file mode 100644 index 0000000..95f9f51 --- /dev/null +++ b/PrecastConcretePlantContracts/ViewModels/ClientViewModel.cs @@ -0,0 +1,21 @@ +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantContracts.ViewModels +{ + public class ClientViewModel : IClientModel + { + public int Id { get; set; } + [DisplayName("ФИО клиента")] + public string ClientFIO { get; set; } = string.Empty; + [DisplayName("Логин (эл. почта)")] + public string Email { get; set; } = string.Empty; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs b/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs index 31571dd..a37bd83 100644 --- a/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs +++ b/PrecastConcretePlantContracts/ViewModels/OrderViewModel.cs @@ -13,6 +13,9 @@ namespace PrecastConcretePlantContracts.ViewModels { [DisplayName("Номер")] public int Id { get; set; } + public int ClientId { get; set; } + [DisplayName("Клиент")] + public string ClientFIO { get; set; } = string.Empty; public int ReinforcedId { get; set; } [DisplayName("ЖБИ")] public string ReinforcedName { get; set; } = string.Empty; diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/ClientStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/ClientStorage.cs new file mode 100644 index 0000000..863b5e9 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Implements/ClientStorage.cs @@ -0,0 +1,101 @@ +using Microsoft.EntityFrameworkCore; +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Implements +{ + public class ClientStorage : IClientStorage + { + public List GetFullList() + { + using var context = new PrecastConcretePlantDatabase(); + return context.Clients + .Include(x => x.Orders) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ClientSearchModel model) + { + if (model == null) + { + return new(); + } + if (!string.IsNullOrEmpty(model.Email)) + { + using var context = new PrecastConcretePlantDatabase(); + return context.Clients + .Include(x => x.Orders) + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + return new(); + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + using var context = new PrecastConcretePlantDatabase(); + if (model.Id.HasValue) + { + return context.Clients + .FirstOrDefault(x => (x.Id == model.Id))?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + return context.Clients + .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password))?.GetViewModel; + } + return new(); + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + using var context = new PrecastConcretePlantDatabase(); + context.Clients.Add(newClient); + context.SaveChanges(); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var client = context.Clients.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + using var context = new PrecastConcretePlantDatabase(); + var element = context.Clients + .Include(x => x.Orders) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Clients.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs index 90bdea5..8192d93 100644 --- a/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs +++ b/PrecastConcretePlantDataBaseImplemet/Implements/OrderStorage.cs @@ -23,6 +23,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements { var deletedElement = context.Orders .Include(x => x.Reinforced) + .Include(x=>x.Client) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; context.Orders.Remove(element); @@ -39,7 +40,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements return null; } using var context = new PrecastConcretePlantDatabase(); - return context.Orders.Include(x => x.Reinforced) + return context.Orders.Include(x => x.Reinforced).Include(x=>x.Client) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; } @@ -55,6 +56,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements { return context.Orders .Include(x => x.Reinforced) + .Include(x => x.Client) .Where(x => x.Id == model.Id) .Select(x => x.GetViewModel) .ToList(); @@ -63,6 +65,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements { return context.Orders .Include(x => x.Reinforced) + .Include(x => x.Client) .Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo) .Select(x => x.GetViewModel) .ToList(); @@ -74,6 +77,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements using var context = new PrecastConcretePlantDatabase(); return context.Orders .Include(x => x.Reinforced) + .Include(x => x.Client) .Select(x => x.GetViewModel) .ToList(); } @@ -90,6 +94,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements context.SaveChanges(); return context.Orders .Include(x => x.Reinforced) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == newOrder.Id) ?.GetViewModel; } @@ -106,6 +111,7 @@ namespace PrecastConcretePlantDatabaseImplement.Implements context.SaveChanges(); return context.Orders .Include(x => x.Reinforced) + .Include(x => x.Client) .FirstOrDefault(x => x.Id == model.Id) ?.GetViewModel; } diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.Designer.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.Designer.cs new file mode 100644 index 0000000..61776cb --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.Designer.cs @@ -0,0 +1,214 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using PrecastConcretePlantDatabaseImplement; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + [DbContext(typeof(PrecastConcretePlantDatabase))] + [Migration("20230404200621_addClient")] + partial class addClient + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.4") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Cost") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("DateCreate") + .HasColumnType("datetime2"); + + b.Property("DateImplement") + .HasColumnType("datetime2"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.Property("Status") + .HasColumnType("int"); + + b.Property("Sum") + .HasColumnType("float"); + + b.HasKey("Id"); + + b.HasIndex("ClientId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("Price") + .HasColumnType("float"); + + b.Property("ReinforcedName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Reinforceds"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("int"); + + b.Property("Count") + .HasColumnType("int"); + + b.Property("ReinforcedId") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ReinforcedId"); + + b.ToTable("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany("Orders") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Client"); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.ReinforcedComponent", b => + { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Component", "Component") + .WithMany("ReinforcedComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") + .WithMany("Components") + .HasForeignKey("ReinforcedId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Reinforced"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => + { + b.Navigation("ReinforcedComponents"); + }); + + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Reinforced", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.cs new file mode 100644 index 0000000..bffdbb0 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/20230404200621_addClient.cs @@ -0,0 +1,68 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace PrecastConcretePlantDatabaseImplement.Migrations +{ + /// + public partial class addClient : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "ClientId", + table: "Orders", + type: "int", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateTable( + name: "Clients", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ClientFIO = table.Column(type: "nvarchar(max)", nullable: false), + Email = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Clients", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ClientId", + table: "Orders", + column: "ClientId"); + + migrationBuilder.AddForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders", + column: "ClientId", + principalTable: "Clients", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Orders_Clients_ClientId", + table: "Orders"); + + migrationBuilder.DropTable( + name: "Clients"); + + migrationBuilder.DropIndex( + name: "IX_Orders_ClientId", + table: "Orders"); + + migrationBuilder.DropColumn( + name: "ClientId", + table: "Orders"); + } + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs b/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs index ff469be..f4a5a4d 100644 --- a/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs +++ b/PrecastConcretePlantDataBaseImplemet/Migrations/PrecastConcretePlantDatabaseModelSnapshot.cs @@ -22,6 +22,31 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Client", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ClientFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Email") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Clients"); + }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => { b.Property("Id") @@ -50,6 +75,9 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + b.Property("ClientId") + .HasColumnType("int"); + b.Property("Count") .HasColumnType("int"); @@ -70,6 +98,8 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.HasKey("Id"); + b.HasIndex("ClientId"); + b.HasIndex("ReinforcedId"); b.ToTable("Orders"); @@ -123,12 +153,20 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Order", b => { + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Client", "Client") + .WithMany("Orders") + .HasForeignKey("ClientId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + b.HasOne("PrecastConcretePlantDatabaseImplement.Models.Reinforced", "Reinforced") .WithMany("Orders") .HasForeignKey("ReinforcedId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.Navigation("Client"); + b.Navigation("Reinforced"); }); @@ -151,6 +189,11 @@ namespace PrecastConcretePlantDatabaseImplement.Migrations b.Navigation("Reinforced"); }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Client", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("PrecastConcretePlantDatabaseImplement.Models.Component", b => { b.Navigation("ReinforcedComponents"); diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Client.cs b/PrecastConcretePlantDataBaseImplemet/Models/Client.cs new file mode 100644 index 0000000..e8c7a06 --- /dev/null +++ b/PrecastConcretePlantDataBaseImplemet/Models/Client.cs @@ -0,0 +1,64 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDatabaseImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; set; } + + [Required] + public string ClientFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + [ForeignKey("ClientId")] + public virtual List Orders { get; set; } = new(); + + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs index c81dbd0..cdca361 100644 --- a/PrecastConcretePlantDataBaseImplemet/Models/Order.cs +++ b/PrecastConcretePlantDataBaseImplemet/Models/Order.cs @@ -15,6 +15,8 @@ namespace PrecastConcretePlantDatabaseImplement.Models { public int Id { get; private set; } [Required] + public int ClientId { get; set; } + [Required] public int ReinforcedId { get; private set; } [Required] public int Count { get; private set; } @@ -26,6 +28,7 @@ namespace PrecastConcretePlantDatabaseImplement.Models public DateTime DateCreate { get; private set; } = DateTime.Now; public DateTime? DateImplement { get; private set; } public virtual Reinforced Reinforced { get; set; } + public virtual Client Client { get; set; } public static Order? Create(OrderBindingModel model) { @@ -36,6 +39,7 @@ namespace PrecastConcretePlantDatabaseImplement.Models return new Order() { Id = model.Id, + ClientId=model.ClientId, ReinforcedId = model.ReinforcedId, Count = model.Count, Sum = model.Sum, @@ -57,6 +61,8 @@ namespace PrecastConcretePlantDatabaseImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId=ClientId, + ClientFIO=Client.ClientFIO, ReinforcedId = ReinforcedId, ReinforcedName = Reinforced.ReinforcedName, Count = Count, diff --git a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs index 8cf3f6c..37066ae 100644 --- a/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs +++ b/PrecastConcretePlantDataBaseImplemet/PrecastConcretePlantDatabase.cs @@ -23,5 +23,6 @@ namespace PrecastConcretePlantDatabaseImplement public virtual DbSet Reinforceds { set; get; } public virtual DbSet ReinforcedComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Clients { set; get; } } } diff --git a/PrecastConcretePlantDataModels/Models/IClientModel .cs b/PrecastConcretePlantDataModels/Models/IClientModel .cs new file mode 100644 index 0000000..dd0de6b --- /dev/null +++ b/PrecastConcretePlantDataModels/Models/IClientModel .cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantDataModels.Models +{ + public interface IClientModel:IId + { + string ClientFIO { get; } + string Email { get; } + string Password { get; } + } +} diff --git a/PrecastConcretePlantDataModels/Models/IOrderModel.cs b/PrecastConcretePlantDataModels/Models/IOrderModel.cs index db89e4e..d1aa7b0 100644 --- a/PrecastConcretePlantDataModels/Models/IOrderModel.cs +++ b/PrecastConcretePlantDataModels/Models/IOrderModel.cs @@ -10,6 +10,7 @@ namespace PrecastConcretePlantDataModels.Models public interface IOrderModel : IId { int ReinforcedId { get; } + int ClientId { get; } int Count { get; } double Sum { get; } OrderStatus Status { get; } diff --git a/PrecastConcretePlantListImplement/DataListSingleton.cs b/PrecastConcretePlantListImplement/DataListSingleton.cs index 591c0d5..1a9a863 100644 --- a/PrecastConcretePlantListImplement/DataListSingleton.cs +++ b/PrecastConcretePlantListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace PrecastConcretePlantListImplement public List Components { get; set; } public List Orders { get; set; } public List Reinforceds { get; set; } + public List Clients { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Reinforceds = new List(); + Clients = new List(); } public static DataListSingleton GetInstance() { diff --git a/PrecastConcretePlantListImplement/Implements/ClientStorage.cs b/PrecastConcretePlantListImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..af1ede3 --- /dev/null +++ b/PrecastConcretePlantListImplement/Implements/ClientStorage.cs @@ -0,0 +1,120 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.SearchModels; +using PrecastConcretePlantContracts.StoragesContracts; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantListImplement.Implements +{ + public class ClientStorage : IClientStorage + { + private readonly DataListSingleton _source; + public ClientStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var result = new List(); + foreach (var client in _source.Clients) + { + result.Add(client.GetViewModel); + } + return result; + } + + public List GetFilteredList(ClientSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.Email)) + { + return result; + } + foreach (var client in _source.Clients) + { + if (client.Email.Contains(model.Email)) + { + result.Add(client.GetViewModel); + } + } + return result; + } + + public ClientViewModel? GetElement(ClientSearchModel model) + { + if (model.Id.HasValue) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + return client.GetViewModel; + } + } + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + foreach (var client in _source.Clients) + { + if (client.Email == model.Email && client.Password == model.Password) + { + return client.GetViewModel; + } + } + } + return null; + } + + public ClientViewModel? Insert(ClientBindingModel model) + { + model.Id = 1; + foreach (var client in _source.Clients) + { + if (model.Id <= client.Id) + { + model.Id = client.Id + 1; + } + } + var newClient = Client.Create(model); + if (newClient == null) + { + return null; + } + _source.Clients.Add(newClient); + return newClient.GetViewModel; + } + + public ClientViewModel? Update(ClientBindingModel model) + { + foreach (var client in _source.Clients) + { + if (client.Id == model.Id) + { + client.Update(model); + return client.GetViewModel; + } + } + return null; + } + + public ClientViewModel? Delete(ClientBindingModel model) + { + for (int i = 0; i < _source.Clients.Count; ++i) + { + if (_source.Clients[i].Id == model.Id) + { + var element = _source.Clients[i]; + _source.Clients.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/PrecastConcretePlantListImplement/Implements/OrderStorage.cs b/PrecastConcretePlantListImplement/Implements/OrderStorage.cs index 225056c..f368705 100644 --- a/PrecastConcretePlantListImplement/Implements/OrderStorage.cs +++ b/PrecastConcretePlantListImplement/Implements/OrderStorage.cs @@ -61,16 +61,19 @@ namespace PrecastConcretePlantListImplement.Implements return result; } - foreach (var order in _source.Orders) + if (model.Id.HasValue) { - if (model.Id.HasValue && order.Id == model.Id) + foreach (var order in _source.Orders) { - result.Add(order.GetViewModel); + if (model.Id.HasValue && order.Id == model.Id) + { + result.Add(order.GetViewModel); + } } } - if (!model.Id.HasValue) + else if (model.DateFrom != null && model.DateTo != null) { - foreach(var order in _source.Orders) + foreach (var order in _source.Orders) { if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo) { @@ -78,6 +81,16 @@ namespace PrecastConcretePlantListImplement.Implements } } } + else if (model.ClientId.HasValue) + { + foreach (var order in _source.Orders) + { + if (order.ClientId == model.ClientId) + { + result.Add((order.GetViewModel)); + } + } + } return result; } diff --git a/PrecastConcretePlantListImplement/Models/Client.cs b/PrecastConcretePlantListImplement/Models/Client.cs new file mode 100644 index 0000000..fbe5b0f --- /dev/null +++ b/PrecastConcretePlantListImplement/Models/Client.cs @@ -0,0 +1,50 @@ +using PrecastConcretePlantContracts.BindingModels; +using PrecastConcretePlantContracts.ViewModels; +using PrecastConcretePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PrecastConcretePlantListImplement.Models +{ + public class Client : IClientModel + { + public int Id { get; private set; } + public string ClientFIO { get; private set; } = string.Empty; + public string Email { get; private set; } = string.Empty; + public string Password { get; private set; } = string.Empty; + public static Client? Create(ClientBindingModel? model) + { + if (model == null) + { + return null; + } + return new Client() + { + Id = model.Id, + ClientFIO = model.ClientFIO, + Email = model.Email, + Password = model.Password, + }; + } + public void Update(ClientBindingModel? model) + { + if (model == null) + { + return; + } + ClientFIO = model.ClientFIO; + Email = model.Email; + Password = model.Password; + } + public ClientViewModel GetViewModel => new() + { + Id = Id, + ClientFIO = ClientFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/PrecastConcretePlantListImplement/Models/Order.cs b/PrecastConcretePlantListImplement/Models/Order.cs index 004364e..66b52a6 100644 --- a/PrecastConcretePlantListImplement/Models/Order.cs +++ b/PrecastConcretePlantListImplement/Models/Order.cs @@ -12,6 +12,7 @@ namespace PrecastConcretePlantListImplement.Models { public class Order : IOrderModel { + public int ClientId { get; private set; } public int ReinforcedId { get; private set; } public int Count { get; private set; } @@ -34,6 +35,7 @@ namespace PrecastConcretePlantListImplement.Models return new Order() { Id = model.Id, + ClientId = model.ClientId, ReinforcedId = model.ReinforcedId, Count = model.Count, Sum = model.Sum, @@ -56,6 +58,7 @@ namespace PrecastConcretePlantListImplement.Models public OrderViewModel GetViewModel => new() { Id = Id, + ClientId = ClientId, ReinforcedId = ReinforcedId, Count = Count, Sum = Sum,