diff --git a/Confectionery/Confectionery/FormImplementer.Designer.cs b/Confectionery/Confectionery/FormImplementer.Designer.cs new file mode 100644 index 0000000..a1b8a32 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementer.Designer.cs @@ -0,0 +1,174 @@ +using System.Windows.Forms; + +namespace ConfectioneryView +{ + partial class FormImplementer + { + /// + /// 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() + { + textBoxFIO = new TextBox(); + textBoxPassword = new TextBox(); + textBoxQualification = new TextBox(); + textBoxWorkExperience = new TextBox(); + labelFIO = new Label(); + labelPassword = new Label(); + labelQualification = new Label(); + labelWorkExperience = new Label(); + buttonSave = new Button(); + buttonCancel = new Button(); + SuspendLayout(); + // + // textBoxFIO + // + textBoxFIO.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxFIO.Location = new Point(170, 11); + textBoxFIO.Name = "textBoxFIO"; + textBoxFIO.Size = new Size(396, 31); + textBoxFIO.TabIndex = 0; + // + // textBoxPassword + // + textBoxPassword.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxPassword.Location = new Point(170, 57); + textBoxPassword.Name = "textBoxPassword"; + textBoxPassword.Size = new Size(396, 31); + textBoxPassword.TabIndex = 1; + // + // textBoxQualification + // + textBoxQualification.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxQualification.Location = new Point(170, 102); + textBoxQualification.Name = "textBoxQualification"; + textBoxQualification.Size = new Size(396, 31); + textBoxQualification.TabIndex = 2; + // + // textBoxWorkExperience + // + textBoxWorkExperience.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + textBoxWorkExperience.Location = new Point(170, 149); + textBoxWorkExperience.Name = "textBoxWorkExperience"; + textBoxWorkExperience.Size = new Size(159, 31); + textBoxWorkExperience.TabIndex = 3; + // + // labelFIO + // + labelFIO.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + labelFIO.AutoSize = true; + labelFIO.Location = new Point(16, 14); + labelFIO.Name = "labelFIO"; + labelFIO.Size = new Size(56, 25); + labelFIO.TabIndex = 4; + labelFIO.Text = "ФИО:"; + // + // labelPassword + // + labelPassword.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + labelPassword.AutoSize = true; + labelPassword.Location = new Point(16, 61); + labelPassword.Name = "labelPassword"; + labelPassword.Size = new Size(78, 25); + labelPassword.TabIndex = 5; + labelPassword.Text = "Пароль:"; + // + // labelQualification + // + labelQualification.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + labelQualification.AutoSize = true; + labelQualification.Location = new Point(16, 108); + labelQualification.Name = "labelQualification"; + labelQualification.Size = new Size(134, 25); + labelQualification.TabIndex = 6; + labelQualification.Text = "Квалификация:"; + // + // labelWorkExperience + // + labelWorkExperience.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + labelWorkExperience.AutoSize = true; + labelWorkExperience.Location = new Point(16, 153); + labelWorkExperience.Name = "labelWorkExperience"; + labelWorkExperience.Size = new Size(122, 25); + labelWorkExperience.TabIndex = 7; + labelWorkExperience.Text = "Стаж работы:"; + // + // buttonSave + // + buttonSave.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonSave.Location = new Point(336, 148); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(112, 34); + buttonSave.TabIndex = 2; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += ButtonSave_Click; + // + // buttonCancel + // + buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right; + buttonCancel.Location = new Point(454, 148); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(112, 34); + buttonCancel.TabIndex = 3; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += ButtonCancel_Click; + // + // FormImplementer + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(578, 194); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(labelWorkExperience); + Controls.Add(labelQualification); + Controls.Add(labelPassword); + Controls.Add(labelFIO); + Controls.Add(textBoxWorkExperience); + Controls.Add(textBoxQualification); + Controls.Add(textBoxPassword); + Controls.Add(textBoxFIO); + Name = "FormImplementer"; + Text = "Исполнитель"; + Load += FormImplementer_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBoxFIO; + private TextBox textBoxPassword; + private TextBox textBoxQualification; + private TextBox textBoxWorkExperience; + private Label labelFIO; + private Label labelPassword; + private Label labelQualification; + private Label labelWorkExperience; + private Button buttonSave; + private Button buttonCancel; + } +} \ No newline at end of file diff --git a/Confectionery/Confectionery/FormImplementer.cs b/Confectionery/Confectionery/FormImplementer.cs new file mode 100644 index 0000000..ece7386 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementer.cs @@ -0,0 +1,100 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using Microsoft.Extensions.Logging; +namespace ConfectioneryView +{ + public partial class FormImplementer : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormImplementer(ILogger logger, + IImplementerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormImplementer_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + _logger.LogInformation("Получение исполнителя"); + var view = _logic.ReadElement(new ImplementerSearchModel + { + Id = _id.Value + }); + if (view != null) + { + textBoxFIO.Text = view.ImplementerFIO; + textBoxPassword.Text = view.Password; + textBoxQualification.Text = + view.Qualification.ToString(); + textBoxWorkExperience.Text = + view.WorkExperience.ToString(); + } + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void ButtonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxFIO.Text)) + { + MessageBox.Show("Заполните ФИО", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + if (string.IsNullOrEmpty(textBoxPassword.Text)) + { + MessageBox.Show("Заполните пароль", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + _logger.LogInformation("Сохранение компонента"); + try + { + var model = new ImplementerBindingModel + { + Id = _id ?? 0, + ImplementerFIO = textBoxFIO.Text, + Password = textBoxPassword.Text, + Qualification = Convert.ToInt32(textBoxQualification.Text), + WorkExperience = Convert.ToInt32( + textBoxWorkExperience.Text), + }; + var operationResult = _id.HasValue ? _logic.Update(model) : + _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. " + + "Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка сохранения исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} \ No newline at end of file diff --git a/Confectionery/Confectionery/FormImplementer.resx b/Confectionery/Confectionery/FormImplementer.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementer.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Confectionery/Confectionery/FormImplementers.Designer.cs b/Confectionery/Confectionery/FormImplementers.Designer.cs new file mode 100644 index 0000000..8cb76d2 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementers.Designer.cs @@ -0,0 +1,119 @@ +namespace ConfectioneryView +{ + partial class FormImplementers + { + /// + /// 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() + { + dataGridView = new DataGridView(); + buttonAdd = new Button(); + buttonUpd = new Button(); + buttonDel = new Button(); + buttonRef = new Button(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // buttonAdd + // + buttonAdd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonAdd.Location = new Point(754, 12); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(112, 56); + buttonAdd.TabIndex = 0; + buttonAdd.Text = "Добавить"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += ButtonAdd_Click; + // + // buttonUpd + // + buttonUpd.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonUpd.Location = new Point(754, 74); + buttonUpd.Name = "buttonUpd"; + buttonUpd.Size = new Size(112, 56); + buttonUpd.TabIndex = 1; + buttonUpd.Text = "Изменить"; + buttonUpd.UseVisualStyleBackColor = true; + buttonUpd.Click += ButtonUpd_Click; + // + // buttonDel + // + buttonDel.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonDel.Location = new Point(754, 198); + buttonDel.Name = "buttonDel"; + buttonDel.Size = new Size(112, 56); + buttonDel.TabIndex = 2; + buttonDel.Text = "Удалить"; + buttonDel.UseVisualStyleBackColor = true; + buttonDel.Click += ButtonDel_Click; + // + // buttonRef + // + buttonRef.Anchor = AnchorStyles.Top | AnchorStyles.Right; + buttonRef.Location = new Point(754, 136); + buttonRef.Name = "buttonRef"; + buttonRef.Size = new Size(112, 56); + buttonRef.TabIndex = 3; + buttonRef.Text = "Обновить"; + buttonRef.UseVisualStyleBackColor = true; + buttonRef.Click += ButtonRef_Click; + // + // dataGridView + // + dataGridView.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + dataGridView.BackgroundColor = SystemColors.Window; + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(0, 0); + dataGridView.Name = "dataGridView"; + dataGridView.RowHeadersWidth = 62; + dataGridView.Size = new Size(742, 444); + dataGridView.TabIndex = 4; + // + // FormImplementers + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(878, 444); + Controls.Add(buttonRef); + Controls.Add(buttonDel); + Controls.Add(buttonUpd); + Controls.Add(buttonAdd); + Controls.Add(dataGridView); + Name = "FormImplementers"; + Text = "Исполнители"; + Load += FormImplementers_Load; + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); + } + + #endregion + + private DataGridView dataGridView; + private Button buttonAdd; + private Button buttonUpd; + private Button buttonDel; + private Button buttonRef; + } +} \ No newline at end of file diff --git a/Confectionery/Confectionery/FormImplementers.cs b/Confectionery/Confectionery/FormImplementers.cs new file mode 100644 index 0000000..ac1fe38 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementers.cs @@ -0,0 +1,114 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using Microsoft.Extensions.Logging; +namespace ConfectioneryView +{ + public partial class FormImplementers : Form + { + private readonly ILogger _logger; + private readonly IImplementerLogic _logic; + public FormImplementers(ILogger logger, + IImplementerLogic logic) + { + InitializeComponent(); + _logger = logger; + _logic = logic; + } + private void FormImplementers_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["ImplementerFIO"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Password"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["Qualification"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + dataGridView.Columns["WorkExperience"].AutoSizeMode = + DataGridViewAutoSizeColumnMode.Fill; + } + _logger.LogInformation("Загрузка компонентов"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка загрузки исполнителей"); + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void ButtonAdd_Click(object sender, EventArgs e) + { + var service = Program.ServiceProvider? + .GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + private void ButtonUpd_Click(object sender, EventArgs e) + { + if (dataGridView.SelectedRows.Count == 1) + { + var service = Program.ServiceProvider? + .GetService(typeof(FormImplementer)); + if (service is FormImplementer form) + { + form.Id = Convert.ToInt32(dataGridView + .SelectedRows[0].Cells["Id"].Value); + if (form.ShowDialog() == DialogResult.OK) + { + LoadData(); + } + } + } + } + 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); + _logger.LogInformation("Удаление компонента"); + try + { + if (!_logic.Delete(new ImplementerBindingModel + { + Id = id + })) + { + throw new Exception("Ошибка при удалении. " + + "Дополнительная информация в логах."); + } + LoadData(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления исполнителя"); + MessageBox.Show(ex.Message, "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } + } + private void ButtonRef_Click(object sender, EventArgs e) + { + LoadData(); + } + } +} \ No newline at end of file diff --git a/Confectionery/Confectionery/FormImplementers.resx b/Confectionery/Confectionery/FormImplementers.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Confectionery/Confectionery/FormImplementers.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/Confectionery/Confectionery/FormMain.Designer.cs b/Confectionery/Confectionery/FormMain.Designer.cs index 79d0ada..bed3658 100644 --- a/Confectionery/Confectionery/FormMain.Designer.cs +++ b/Confectionery/Confectionery/FormMain.Designer.cs @@ -39,10 +39,12 @@ componentsToolStripMenuItem = new ToolStripMenuItem(); pastriesToolStripMenuItem = new ToolStripMenuItem(); clientsToolStripMenuItem = new ToolStripMenuItem(); - отчётыToolStripMenuItem = new ToolStripMenuItem(); + reportsToolStripMenuItem = new ToolStripMenuItem(); listOfComponentsToolStripMenuItem = new ToolStripMenuItem(); ComponentsByPastryToolStripMenuItem = new ToolStripMenuItem(); listOfOrdersToolStripMenuItem = new ToolStripMenuItem(); + startWorkToolStripMenuItem = new ToolStripMenuItem(); + implementersToolStripMenuItem = new ToolStripMenuItem(); ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); menuStrip.SuspendLayout(); SuspendLayout(); @@ -117,16 +119,16 @@ // menuStrip.Dock = DockStyle.None; menuStrip.ImageScalingSize = new Size(24, 24); - menuStrip.Items.AddRange(new ToolStripItem[] { referencesToolStripMenuItem, отчётыToolStripMenuItem }); + menuStrip.Items.AddRange(new ToolStripItem[] { referencesToolStripMenuItem, reportsToolStripMenuItem, startWorkToolStripMenuItem }); menuStrip.Location = new Point(0, 0); menuStrip.Name = "menuStrip"; - menuStrip.Size = new Size(415, 33); + menuStrip.Size = new Size(551, 33); menuStrip.TabIndex = 8; menuStrip.Text = "menuStrip1"; // // referencesToolStripMenuItem // - referencesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { componentsToolStripMenuItem, pastriesToolStripMenuItem, clientsToolStripMenuItem }); + referencesToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { componentsToolStripMenuItem, pastriesToolStripMenuItem, clientsToolStripMenuItem, implementersToolStripMenuItem }); referencesToolStripMenuItem.Name = "referencesToolStripMenuItem"; referencesToolStripMenuItem.Size = new Size(139, 29); referencesToolStripMenuItem.Text = "Справочники"; @@ -152,12 +154,12 @@ clientsToolStripMenuItem.Text = "Клиенты"; clientsToolStripMenuItem.Click += ClientsToolStripMenuItem_Click; // - // отчётыToolStripMenuItem + // reportsToolStripMenuItem // - отчётыToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { listOfComponentsToolStripMenuItem, ComponentsByPastryToolStripMenuItem, listOfOrdersToolStripMenuItem }); - отчётыToolStripMenuItem.Name = "отчётыToolStripMenuItem"; - отчётыToolStripMenuItem.Size = new Size(88, 29); - отчётыToolStripMenuItem.Text = "Отчёты"; + reportsToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { listOfComponentsToolStripMenuItem, ComponentsByPastryToolStripMenuItem, listOfOrdersToolStripMenuItem }); + reportsToolStripMenuItem.Name = "reportsToolStripMenuItem"; + reportsToolStripMenuItem.Size = new Size(88, 29); + reportsToolStripMenuItem.Text = "Отчёты"; // // listOfComponentsToolStripMenuItem // @@ -180,6 +182,20 @@ listOfOrdersToolStripMenuItem.Text = "Список заказов"; listOfOrdersToolStripMenuItem.Click += ListOfOrdersToolStripMenuItem_Click; // + // startWorkToolStripMenuItem + // + startWorkToolStripMenuItem.Name = "startWorkToolStripMenuItem"; + startWorkToolStripMenuItem.Size = new Size(136, 29); + startWorkToolStripMenuItem.Text = "Запуск работ"; + startWorkToolStripMenuItem.Click += StartWorkToolStripMenuItem_Click; + // + // implementersToolStripMenuItem + // + implementersToolStripMenuItem.Name = "implementersToolStripMenuItem"; + implementersToolStripMenuItem.Size = new Size(298, 34); + implementersToolStripMenuItem.Text = "Исполнители"; + implementersToolStripMenuItem.Click += ImplementersToolStripMenuItem_Click; + // // FormMain // AutoScaleDimensions = new SizeF(10F, 25F); @@ -215,10 +231,12 @@ private ToolStripMenuItem referencesToolStripMenuItem; private ToolStripMenuItem pastriesToolStripMenuItem; private ToolStripMenuItem componentsToolStripMenuItem; - private ToolStripMenuItem отчётыToolStripMenuItem; + private ToolStripMenuItem reportsToolStripMenuItem; private ToolStripMenuItem listOfComponentsToolStripMenuItem; private ToolStripMenuItem ComponentsByPastryToolStripMenuItem; private ToolStripMenuItem listOfOrdersToolStripMenuItem; private ToolStripMenuItem clientsToolStripMenuItem; + private ToolStripMenuItem startWorkToolStripMenuItem; + private ToolStripMenuItem implementersToolStripMenuItem; } } \ No newline at end of file diff --git a/Confectionery/Confectionery/FormMain.cs b/Confectionery/Confectionery/FormMain.cs index 188a0bc..ed8fe16 100644 --- a/Confectionery/Confectionery/FormMain.cs +++ b/Confectionery/Confectionery/FormMain.cs @@ -10,13 +10,15 @@ namespace ConfectioneryView private readonly ILogger _logger; private readonly IOrderLogic _orderLogic; private readonly IReportLogic _reportLogic; + private readonly IWorkProcess _workProcess; public FormMain(ILogger logger, IOrderLogic orderLogic, - IReportLogic reportLogic) + IReportLogic reportLogic, IWorkProcess workProcess) { InitializeComponent(); _logger = logger; _orderLogic = orderLogic; _reportLogic = reportLogic; + _workProcess = workProcess; } private void FormMain_Load(object sender, EventArgs e) { @@ -108,6 +110,25 @@ namespace ConfectioneryView form.ShowDialog(); } } + private void ImplementersToolStripMenuItem_Click( + object sender, EventArgs e) + { + var service = Program.ServiceProvider? + .GetService(typeof(FormImplementers)); + if (service is FormImplementers form) + { + form.ShowDialog(); + } + } + private void StartWorkToolStripMenuItem_Click( + object sender, EventArgs e) + { + _workProcess.DoWork((Program.ServiceProvider?.GetService( + typeof(IImplementerLogic)) as IImplementerLogic)!, + _orderLogic); + MessageBox.Show("Процесс обработки запущен", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + } private void ButtonCreateOrder_Click(object sender, EventArgs e) { var service = Program.ServiceProvider?.GetService( diff --git a/Confectionery/Confectionery/Program.cs b/Confectionery/Confectionery/Program.cs index 53b5074..4738629 100644 --- a/Confectionery/Confectionery/Program.cs +++ b/Confectionery/Confectionery/Program.cs @@ -39,11 +39,14 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); @@ -54,6 +57,8 @@ namespace ConfectioneryView services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddTransient(); services.AddTransient(); diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs index fa287f5..af60a8c 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ClientLogic.cs @@ -10,11 +10,11 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IClientStorage _clientStorage; - public ClientLogic(ILogger logger, IClientStorage - componentStorage) + public ClientLogic(ILogger logger, IClientStorage + clientStorage) { _logger = logger; - _clientStorage = componentStorage; + _clientStorage = clientStorage; } public List? ReadList(ClientSearchModel? model) { diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ImplementerLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ImplementerLogic.cs new file mode 100644 index 0000000..5ab75f6 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/ImplementerLogic.cs @@ -0,0 +1,130 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using Microsoft.Extensions.Logging; +namespace ConfectioneryBusinessLogic.BusinessLogics +{ + public class ImplementerLogic : IImplementerLogic + { + private readonly ILogger _logger; + private readonly IImplementerStorage _implementerStorage; + public ImplementerLogic(ILogger logger, + IImplementerStorage implementerStorage) + { + _logger = logger; + _implementerStorage = implementerStorage; + } + public List? ReadList( + ImplementerSearchModel? model) + { + _logger.LogInformation("ReadList. ImplementerFIO:{ClientFIO}. " + + "Id:{ Id}", model?.ImplementerFIO, model?.Id); + var list = model == null ? _implementerStorage.GetFullList() : + _implementerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ImplementerViewModel? ReadElement(ImplementerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ImplementerFIO:" + + "{ImplementerFIO}.Id:{ Id}", model.ImplementerFIO, model.Id); + var element = _implementerStorage.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(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ImplementerBindingModel model) + { + CheckModel(model); + if (_implementerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ImplementerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_implementerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ImplementerBindingModel model, + bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ImplementerFIO)) + { + throw new ArgumentNullException("Нет ФИО исполнителя", + nameof(model.ImplementerFIO)); + } + if (string.IsNullOrEmpty(model.Password)) + { + throw new ArgumentNullException("Нет пароля клиента", + nameof(model.Password)); + } + if (model.WorkExperience < 0) + { + throw new ArgumentNullException("Стаж меньше 0", + nameof(model.WorkExperience)); + } + if (model.Qualification < 0) + { + throw new ArgumentException("Квалификация меньше 0", + nameof(model.Qualification)); + } + _logger.LogInformation("Implementer. ImplementerFIO: " + + "{ImplementerFIO}. Password: {Password}. WorkExperience: " + + "{WorkExperience}. Qualification:{ Qualification}. Id: { Id} ", + model.ImplementerFIO, model.Password, model.WorkExperience, + model.Qualification, model.Id); + var element = _implementerStorage.GetElement( + new ImplementerSearchModel + { + ImplementerFIO = model.ImplementerFIO, + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException( + "Исполнитель с таким ФИО уже есть"); + } + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs index 15852c6..9e801a6 100644 --- a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/OrderLogic.cs @@ -11,12 +11,29 @@ namespace ConfectioneryBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; + static readonly object locker = new object(); public OrderLogic(ILogger logger, IOrderStorage orderStorage) { _logger = logger; _orderStorage = orderStorage; } + public OrderViewModel? ReadElement(OrderSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. Id: {Id}", model.Id); + var element = _orderStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id: {Id}", element.Id); + return element; + } public List? ReadList(OrderSearchModel? model) { _logger.LogInformation("ReadList. Id:{ Id}", model?.Id); @@ -44,7 +61,7 @@ namespace ConfectioneryBusinessLogic.BusinessLogics } public bool ChangeStatus(OrderBindingModel model, OrderStatus status) { - CheckModel(model); + CheckModel(model, false); var element = _orderStorage.GetElement( new OrderSearchModel { Id = model.Id }); if (element == null) @@ -61,12 +78,17 @@ namespace ConfectioneryBusinessLogic.BusinessLogics model.Status = status; if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + if (element.ImplementerId.HasValue) model.ImplementerId + = element.ImplementerId; _orderStorage.Update(model); return true; } public bool TakeOrderInWork(OrderBindingModel model) { - return ChangeStatus(model, OrderStatus.Выполняется); + lock (locker) + { + return ChangeStatus(model, OrderStatus.Выполняется); + } } public bool FinishOrder(OrderBindingModel model) { diff --git a/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/WorkModeling.cs b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/WorkModeling.cs new file mode 100644 index 0000000..d501289 --- /dev/null +++ b/Confectionery/ConfectioneryBusinessLogic/BusinessLogics/WorkModeling.cs @@ -0,0 +1,141 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using Microsoft.Extensions.Logging; +namespace ConfectioneryBusinessLogic.BusinessLogics +{ + public class WorkModeling : IWorkProcess + { + private readonly ILogger _logger; + private readonly Random _rnd; + private IOrderLogic? _orderLogic; + public WorkModeling(ILogger logger) + { + _logger = logger; + _rnd = new Random(1000); + } + public void DoWork(IImplementerLogic implementerLogic, + IOrderLogic orderLogic) + { + _orderLogic = orderLogic; + var implementers = implementerLogic.ReadList(null); + if (implementers == null) + { + _logger.LogWarning("DoWork. Implementers is null"); + return; + } + var orders = _orderLogic.ReadList(new OrderSearchModel + { + Status = OrderStatus.Принят + }); + if (orders == null || orders.Count == 0) + { + _logger.LogWarning("DoWork. Orders is null or empty"); + return; + } + _logger.LogDebug("DoWork for {Count} orders", orders.Count); + foreach (var implementer in implementers) + { + Task.Run(() => WorkerWorkAsync(implementer, orders)); + } + } + /// + /// Иммитация работы исполнителя + /// + /// + /// + private async Task WorkerWorkAsync(ImplementerViewModel implementer, + List orders) + { + if (_orderLogic == null || implementer == null) + { + return; + } + await RunOrderInWork(implementer); + await Task.Run(() => + { + foreach (var order in orders) + { + try + { + _logger.LogDebug( + "DoWork. Worker {Id} try get order { Order}", + implementer.Id, order.Id); + _orderLogic.TakeOrderInWork(new OrderBindingModel + { + Id = order.Id, + ImplementerId = implementer.Id + }); + Thread.Sleep(implementer.WorkExperience * + _rnd.Next(100, 1000) * order.Count); + _logger.LogDebug( + "DoWork. Worker {Id} finish order { Order}", + implementer.Id, order.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = order.Id + }); + Thread.Sleep(implementer.Qualification * + _rnd.Next(10, 100)); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + }); + } + /// + /// Ищем заказ, которые уже в работе (вдруг исполнителя прервали) + /// + /// + /// + private async Task RunOrderInWork(ImplementerViewModel implementer) + { + if (_orderLogic == null || implementer == null) + { + return; + } + try + { + var runOrder = await Task.Run(() => _orderLogic.ReadElement( + new OrderSearchModel + { + ImplementerId = implementer.Id, + Status = OrderStatus.Выполняется + })); + if (runOrder == null) + { + return; + } + _logger.LogDebug("DoWork. Worker {Id} back to order {Order}", + implementer.Id, runOrder.Id); + Thread.Sleep(implementer.WorkExperience * _rnd.Next(100, 300) + * runOrder.Count); + _logger.LogDebug("DoWork. Worker {Id} finish order {Order}", + implementer.Id, runOrder.Id); + _orderLogic.FinishOrder(new OrderBindingModel + { + Id = runOrder.Id + }); + Thread.Sleep(implementer.Qualification * _rnd.Next(10, 100)); + } + catch (InvalidOperationException ex) + { + _logger.LogWarning(ex, "Error try get work"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while do work"); + throw; + } + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/BindingModels/ImplementerBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/ImplementerBindingModel.cs new file mode 100644 index 0000000..1b3b1bd --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BindingModels/ImplementerBindingModel.cs @@ -0,0 +1,12 @@ +using ConfectioneryDataModels.Models; +namespace ConfectioneryContracts.BindingModels +{ + public class ImplementerBindingModel : IImplementerModel + { + public int Id { get; set; } + public string ImplementerFIO { get; set; } = string.Empty; + public int WorkExperience { get; set; } = 0; + public int Qualification { get; set; } = 0; + public string Password { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs index c4d89d0..56b67a0 100644 --- a/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs +++ b/Confectionery/ConfectioneryContracts/BindingModels/OrderBindingModel.cs @@ -6,6 +6,7 @@ namespace ConfectioneryContracts.BindingModels { public int Id { get; set; } public int PastryId { get; set; } + public int? ImplementerId { get; set; } = null; public int ClientId { get; set; } public int Count { get; set; } public double Sum { get; set; } diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IImplementerLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IImplementerLogic.cs new file mode 100644 index 0000000..252118e --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IImplementerLogic.cs @@ -0,0 +1,14 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IImplementerLogic + { + List? ReadList(ImplementerSearchModel? model); + ImplementerViewModel? ReadElement(ImplementerSearchModel model); + bool Create(ImplementerBindingModel model); + bool Update(ImplementerBindingModel model); + bool Delete(ImplementerBindingModel model); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IOrderLogic.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IOrderLogic.cs index 7493eb8..91e4c38 100644 --- a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IOrderLogic.cs +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IOrderLogic.cs @@ -6,6 +6,7 @@ namespace ConfectioneryContracts.BusinessLogicsContracts public interface IOrderLogic { List? ReadList(OrderSearchModel? model); + OrderViewModel? ReadElement(OrderSearchModel model); bool CreateOrder(OrderBindingModel model); bool TakeOrderInWork(OrderBindingModel model); bool FinishOrder(OrderBindingModel model); diff --git a/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IWorkProcess.cs b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IWorkProcess.cs new file mode 100644 index 0000000..2add168 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/BusinessLogicsContracts/IWorkProcess.cs @@ -0,0 +1,8 @@ +namespace ConfectioneryContracts.BusinessLogicsContracts +{ + public interface IWorkProcess + { + void DoWork(IImplementerLogic implementerLogic, + IOrderLogic orderLogic); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/SearchModels/ImplementerSearchModel.cs b/Confectionery/ConfectioneryContracts/SearchModels/ImplementerSearchModel.cs new file mode 100644 index 0000000..3cd12c8 --- /dev/null +++ b/Confectionery/ConfectioneryContracts/SearchModels/ImplementerSearchModel.cs @@ -0,0 +1,9 @@ +namespace ConfectioneryContracts.SearchModels +{ + public class ImplementerSearchModel + { + public int? Id { get; set; } + public string? ImplementerFIO { get; set; } = string.Empty; + public string? Password { get; set; } = string.Empty; + } +} diff --git a/Confectionery/ConfectioneryContracts/SearchModels/OrderSearchModel.cs b/Confectionery/ConfectioneryContracts/SearchModels/OrderSearchModel.cs index d5d6cf8..21259d8 100644 --- a/Confectionery/ConfectioneryContracts/SearchModels/OrderSearchModel.cs +++ b/Confectionery/ConfectioneryContracts/SearchModels/OrderSearchModel.cs @@ -1,9 +1,12 @@ -namespace ConfectioneryContracts.SearchModels +using ConfectioneryDataModels.Enums; +namespace ConfectioneryContracts.SearchModels { public class OrderSearchModel { public int? Id { get; set; } public int? ClientId { get; set; } + public int? ImplementerId { get; set; } + public OrderStatus? Status { get; set; } public DateTime? DateFrom { get; set; } public DateTime? DateTo { get; set; } } diff --git a/Confectionery/ConfectioneryContracts/StoragesContracts/IImplementerStorage.cs b/Confectionery/ConfectioneryContracts/StoragesContracts/IImplementerStorage.cs new file mode 100644 index 0000000..5c01f7e --- /dev/null +++ b/Confectionery/ConfectioneryContracts/StoragesContracts/IImplementerStorage.cs @@ -0,0 +1,16 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +namespace ConfectioneryContracts.StoragesContracts +{ + public interface IImplementerStorage + { + List GetFullList(); + List GetFilteredList( + ImplementerSearchModel model); + ImplementerViewModel? GetElement(ImplementerSearchModel model); + ImplementerViewModel? Insert(ImplementerBindingModel model); + ImplementerViewModel? Update(ImplementerBindingModel model); + ImplementerViewModel? Delete(ImplementerBindingModel model); + } +} diff --git a/Confectionery/ConfectioneryContracts/ViewModels/ImplementerViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/ImplementerViewModel.cs new file mode 100644 index 0000000..dbf646a --- /dev/null +++ b/Confectionery/ConfectioneryContracts/ViewModels/ImplementerViewModel.cs @@ -0,0 +1,17 @@ +using ConfectioneryDataModels.Models; +using System.ComponentModel; +namespace ConfectioneryContracts.ViewModels +{ + public class ImplementerViewModel : IImplementerModel + { + public int Id { get; set; } + [DisplayName("ФИО исполнителя")] + public string ImplementerFIO { get; set; } = string.Empty; + [DisplayName("Стаж работы")] + public int WorkExperience { get; set; } = 0; + [DisplayName("Квалификация")] + public int Qualification { get; set; } = 0; + [DisplayName("Пароль")] + public string Password { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs b/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs index 98c8812..a03c237 100644 --- a/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs +++ b/Confectionery/ConfectioneryContracts/ViewModels/OrderViewModel.cs @@ -13,6 +13,9 @@ namespace ConfectioneryContracts.ViewModels public int ClientId { get; set; } [DisplayName("Клиент")] public string ClientFIO { get; set; } = string.Empty; + public int? ImplementerId { get; set; } = null; + [DisplayName("Исполнитель")] + public string ImplementerFIO { get; set; } = string.Empty; [DisplayName("Количество")] public int Count { get; set; } [DisplayName("Сумма")] diff --git a/Confectionery/ConfectioneryDataModels/Models/IImplementerModel.cs b/Confectionery/ConfectioneryDataModels/Models/IImplementerModel.cs new file mode 100644 index 0000000..476b0ca --- /dev/null +++ b/Confectionery/ConfectioneryDataModels/Models/IImplementerModel.cs @@ -0,0 +1,10 @@ +namespace ConfectioneryDataModels.Models +{ + public interface IImplementerModel : IId + { + string ImplementerFIO { get; } + string Password { get; } + int WorkExperience { get; } + int Qualification { get; } + } +} diff --git a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs index 1c0b0cf..630c265 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/ConfectioneryDatabase.cs @@ -18,5 +18,6 @@ namespace ConfectioneryDatabaseImplement public virtual DbSet PastryComponents { set; get; } public virtual DbSet Orders { set; get; } public virtual DbSet Clients { set; get; } + public virtual DbSet Implementers { set; get; } } } diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/ImplementerStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..d0c5bc7 --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,83 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDatabaseImplement.Models; +namespace ConfectioneryDatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var res = context.Implementers + .FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + context.Implementers.Remove(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && + string.IsNullOrEmpty(model.Password) && + !model.Id.HasValue) + { + return null; + } + using var context = new ConfectioneryDatabase(); + return context.Implementers + .FirstOrDefault(x => + (string.IsNullOrEmpty(model.ImplementerFIO) || + x.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Password) || + x.Password == model.Password)) + ?.GetViewModel; + } + public List GetFilteredList( + ImplementerSearchModel model) + { + if (model == null) + { + return new(); + } + if (model.Id.HasValue) + { + var res = GetElement(model); + return res != null ? new() { res } : new(); + } + return new(); + } + public List GetFullList() + { + using var context = new ConfectioneryDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var res = Implementer.Create(model); + if (res != null) + { + context.Implementers.Add(res); + context.SaveChanges(); + } + return res?.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new ConfectioneryDatabase(); + var res = context.Implementers + .FirstOrDefault(x => x.Id == model.Id); + if (res != null) + { + res.Update(model); + context.SaveChanges(); + } + return res?.GetViewModel; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs index 10a439a..3929708 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Implements/OrderStorage.cs @@ -13,6 +13,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Orders.Include(x => x.Pastries) .Include(x => x.Client) + .Include(x => x.Implementer) .Select(x => x.GetViewModel) .ToList(); } @@ -21,6 +22,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Orders.Include(x => x.Pastries) .Include(x => x.Client) + .Include(x => x.Implementer) .Where(x => ( (!model.Id.HasValue || x.Id == model.Id) && (!model.DateFrom.HasValue || x.DateCreate >= @@ -41,7 +43,14 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); return context.Orders.Include(x => x.Pastries) .Include(x => x.Client) - .FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + .Include(x => x.Implementer) + .FirstOrDefault(x => ( + (model.Id.HasValue && x.Id == model.Id) || + (model.ImplementerId.HasValue && model.Status.HasValue && + x.ImplementerId == model.ImplementerId && + x.Status == model.Status) + ) + )?.GetViewModel; } public OrderViewModel? Insert(OrderBindingModel model) { @@ -60,6 +69,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); var order = context.Orders.Include(x => x.Pastries) .Include(x => x.Client) + .Include(x => x.Implementer) .FirstOrDefault(x => x.Id == model.Id); if (order == null) { @@ -74,6 +84,7 @@ namespace ConfectioneryDatabaseImplement.Implements using var context = new ConfectioneryDatabase(); var element = context.Orders.Include(x => x.Pastries) .Include(x => x.Client) + .Include(x => x.Implementer) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.Designer.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.Designer.cs similarity index 82% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.Designer.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.Designer.cs index aa974bd..97d84bc 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.Designer.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace ConfectioneryDatabaseImplement.Migrations { [DbContext(typeof(ConfectioneryDatabase))] - [Migration("20240508053013_InitialCreate")] + [Migration("20240508100134_InitialCreate")] partial class InitialCreate { /// @@ -70,6 +70,33 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("Components"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -90,6 +117,9 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Property("DateImplement") .HasColumnType("datetime2"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("PastryId") .HasColumnType("int"); @@ -103,6 +133,8 @@ namespace ConfectioneryDatabaseImplement.Migrations b.HasIndex("ClientId"); + b.HasIndex("ImplementerId"); + b.HasIndex("PastryId"); b.ToTable("Orders"); @@ -162,6 +194,10 @@ namespace ConfectioneryDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("ConfectioneryDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastries") .WithMany("Orders") .HasForeignKey("PastryId") @@ -170,6 +206,8 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Client"); + b.Navigation("Implementer"); + b.Navigation("Pastries"); }); @@ -202,6 +240,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => { b.Navigation("Components"); diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.cs similarity index 81% rename from Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.cs rename to Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.cs index f97083f..d89f6d0 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508053013_InitialCreate.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/20240508100134_InitialCreate.cs @@ -40,6 +40,22 @@ namespace ConfectioneryDatabaseImplement.Migrations table.PrimaryKey("PK_Components", x => x.Id); }); + migrationBuilder.CreateTable( + name: "Implementers", + columns: table => new + { + Id = table.Column(type: "int", nullable: false) + .Annotation("SqlServer:Identity", "1, 1"), + ImplementerFIO = table.Column(type: "nvarchar(max)", nullable: false), + Password = table.Column(type: "nvarchar(max)", nullable: false), + Qualification = table.Column(type: "int", nullable: false), + WorkExperience = table.Column(type: "int", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Implementers", x => x.Id); + }); + migrationBuilder.CreateTable( name: "Pastries", columns: table => new @@ -66,7 +82,8 @@ namespace ConfectioneryDatabaseImplement.Migrations DateCreate = table.Column(type: "datetime2", nullable: false), DateImplement = table.Column(type: "datetime2", nullable: true), PastryId = table.Column(type: "int", nullable: false), - ClientId = table.Column(type: "int", nullable: false) + ClientId = table.Column(type: "int", nullable: false), + ImplementerId = table.Column(type: "int", nullable: true) }, constraints: table => { @@ -77,6 +94,11 @@ namespace ConfectioneryDatabaseImplement.Migrations principalTable: "Clients", principalColumn: "Id", onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_Orders_Implementers_ImplementerId", + column: x => x.ImplementerId, + principalTable: "Implementers", + principalColumn: "Id"); table.ForeignKey( name: "FK_Orders_Pastries_PastryId", column: x => x.PastryId, @@ -117,6 +139,11 @@ namespace ConfectioneryDatabaseImplement.Migrations table: "Orders", column: "ClientId"); + migrationBuilder.CreateIndex( + name: "IX_Orders_ImplementerId", + table: "Orders", + column: "ImplementerId"); + migrationBuilder.CreateIndex( name: "IX_Orders_PastryId", table: "Orders", @@ -145,6 +172,9 @@ namespace ConfectioneryDatabaseImplement.Migrations migrationBuilder.DropTable( name: "Clients"); + migrationBuilder.DropTable( + name: "Implementers"); + migrationBuilder.DropTable( name: "Components"); diff --git a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs index a7354da..862cb28 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Migrations/ConfectioneryDatabaseModelSnapshot.cs @@ -67,6 +67,33 @@ namespace ConfectioneryDatabaseImplement.Migrations b.ToTable("Components"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("ImplementerFIO") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Password") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Qualification") + .HasColumnType("int"); + + b.Property("WorkExperience") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("Implementers"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Order", b => { b.Property("Id") @@ -87,6 +114,9 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Property("DateImplement") .HasColumnType("datetime2"); + b.Property("ImplementerId") + .HasColumnType("int"); + b.Property("PastryId") .HasColumnType("int"); @@ -100,6 +130,8 @@ namespace ConfectioneryDatabaseImplement.Migrations b.HasIndex("ClientId"); + b.HasIndex("ImplementerId"); + b.HasIndex("PastryId"); b.ToTable("Orders"); @@ -159,6 +191,10 @@ namespace ConfectioneryDatabaseImplement.Migrations .OnDelete(DeleteBehavior.Cascade) .IsRequired(); + b.HasOne("ConfectioneryDatabaseImplement.Models.Implementer", "Implementer") + .WithMany("Orders") + .HasForeignKey("ImplementerId"); + b.HasOne("ConfectioneryDatabaseImplement.Models.Pastry", "Pastries") .WithMany("Orders") .HasForeignKey("PastryId") @@ -167,6 +203,8 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("Client"); + b.Navigation("Implementer"); + b.Navigation("Pastries"); }); @@ -199,6 +237,11 @@ namespace ConfectioneryDatabaseImplement.Migrations b.Navigation("PastryComponents"); }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Implementer", b => + { + b.Navigation("Orders"); + }); + modelBuilder.Entity("ConfectioneryDatabaseImplement.Models.Pastry", b => { b.Navigation("Components"); diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Implementer.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Implementer.cs new file mode 100644 index 0000000..8ab6dcb --- /dev/null +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Implementer.cs @@ -0,0 +1,56 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +namespace ConfectioneryDatabaseImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + [Required] + public string ImplementerFIO { get; private set; } = string.Empty; + [Required] + public string Password { get; set; } = string.Empty; + [Required] + public int Qualification { get; set; } = 0; + [Required] + public int WorkExperience { get; set; } = 0; + [ForeignKey("ImplementerId")] + public virtual List Orders { get; set; } = new(); + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Password = model.Password, + Qualification = model.Qualification, + ImplementerFIO = model.ImplementerFIO, + WorkExperience = model.WorkExperience, + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + Password = model.Password; + Qualification = model.Qualification; + ImplementerFIO = model.ImplementerFIO; + WorkExperience = model.WorkExperience; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Password = Password, + Qualification = Qualification, + ImplementerFIO = ImplementerFIO, + WorkExperience = WorkExperience, + }; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs index 68c692f..2770685 100644 --- a/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryDatabaseImplement/Models/Order.cs @@ -22,6 +22,8 @@ namespace ConfectioneryDatabaseImplement.Models [Required] public int ClientId { get; private set; } public virtual Client? Client { get; private set; } + public int? ImplementerId { get; private set; } = null; + public virtual Implementer? Implementer { get; private set; } public static Order? Create(OrderBindingModel model) { if (model == null) @@ -37,7 +39,8 @@ namespace ConfectioneryDatabaseImplement.Models DateCreate = model.DateCreate, DateImplement = model.DateImplement, PastryId = model.PastryId, - ClientId = model.ClientId + ClientId = model.ClientId, + ImplementerId = model.ImplementerId }; } public void Update(OrderBindingModel? model) @@ -48,11 +51,13 @@ namespace ConfectioneryDatabaseImplement.Models } Status = model.Status; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() { PastryId = PastryId, ClientId = ClientId, + ImplementerId = ImplementerId, Count = Count, Sum = Sum, Status = Status, @@ -60,7 +65,8 @@ namespace ConfectioneryDatabaseImplement.Models DateImplement = DateImplement, Id = Id, PastryName = Pastries?.PastryName ?? string.Empty, - ClientFIO = Client?.ClientFIO ?? string.Empty + ClientFIO = Client?.ClientFIO ?? string.Empty, + ImplementerFIO = Implementer?.ImplementerFIO ?? string.Empty }; } } diff --git a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs index b3cf7a7..3fd51ac 100644 --- a/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs +++ b/Confectionery/ConfectioneryFileImplement/DataFileSingleton.cs @@ -9,10 +9,12 @@ namespace ConfectioneryFileImplement private readonly string OrderFileName = "Order.xml"; private readonly string PastryFileName = "Pastry.xml"; private readonly string ClientFileName = "Client.xml"; + private readonly string ImplementerFileName = "Implementer.xml"; public List Components { get; private set; } public List Orders { get; private set; } public List Pastries { get; private set; } public List Clients { get; private set; } + public List Implementers { get; private set; } public static DataFileSingleton GetInstance() { if (instance == null) @@ -29,6 +31,8 @@ namespace ConfectioneryFileImplement "Orders", x => x.GetXElement); public void SaveClients() => SaveData(Clients, ClientFileName, "Clients", x => x.GetXElement); + public void SaveImplementers() => SaveData(Implementers, + ImplementerFileName, "Implementers", x => x.GetXElement); private DataFileSingleton() { Components = LoadData(ComponentFileName, "Component", @@ -39,6 +43,8 @@ namespace ConfectioneryFileImplement x => Order.Create(x)!)!; Clients = LoadData(ClientFileName, "Client", x => Client.Create(x)!)!; + Implementers = LoadData(ImplementerFileName, "Implementer", + x => Implementer.Create(x)!)!; } private static List? LoadData(string filename, string xmlNodeName, Func selectFunction) diff --git a/Confectionery/ConfectioneryFileImplement/Implements/ImplementerStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..0af0024 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,86 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryFileImplement.Models; +namespace ConfectioneryFileImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataFileSingleton source; + public ImplementerStorage() + { + source = DataFileSingleton.GetInstance(); + } + public List GetFullList() + { + return source.Implementers + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList( + ImplementerSearchModel model) + { + if (string.IsNullOrEmpty(model.ImplementerFIO) && + string.IsNullOrEmpty(model.Password)) + { + return new(); + } + return source.Implementers + .Where(x => (string.IsNullOrEmpty(model.ImplementerFIO) || + x.ImplementerFIO.Contains(model.ImplementerFIO)) && + (string.IsNullOrEmpty(model.Password) || + x.Password.Contains(model.Password))) + .Select(x => x.GetViewModel) + .ToList(); + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + return source.Implementers + .FirstOrDefault(x => + (string.IsNullOrEmpty(model.ImplementerFIO) || + x.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || x.Id == model.Id) && + (string.IsNullOrEmpty(model.Password) || + x.Password == model.Password)) + ?.GetViewModel; + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = source.Implementers.Count > 0 ? + source.Implementers.Max(x => x.Id) + 1 : 1; + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + source.Implementers.Add(newImplementer); + source.SaveImplementers(); + return newImplementer.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + var implementer = source.Implementers + .FirstOrDefault(x => x.Id == model.Id); + if (implementer == null) + { + return null; + } + implementer.Update(model); + source.SaveImplementers(); + return implementer.GetViewModel; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + var element = source.Implementers + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + source.Implementers.Remove(element); + source.SaveImplementers(); + return element.GetViewModel; + } + return null; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs index ea0a229..070e9b3 100644 --- a/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryFileImplement/Implements/OrderStorage.cs @@ -15,30 +15,36 @@ namespace ConfectioneryFileImplement.Implements public List GetFullList() { return source.Orders - .Select(x => AccessPastryStorage(x.GetViewModel)) + .Select(x => AccessStorage(x.GetViewModel)) .ToList(); } public List GetFilteredList(OrderSearchModel model) { return source.Orders - .Where(x => ( - (!model.Id.HasValue || x.Id == model.Id) && - (!model.DateFrom.HasValue || x.DateCreate >= model.DateFrom) && - (!model.DateTo.HasValue || x.DateCreate <= model.DateTo) && - (!model.ClientId.HasValue || x.ClientId == model.ClientId) + .Where(x => ( + (!model.Id.HasValue || x.Id == model.Id) && + (!model.DateFrom.HasValue || x.DateCreate >= + model.DateFrom) && (!model.DateTo.HasValue || + x.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || + x.ClientId == model.ClientId) && + (!model.Status.HasValue || x.Status == model.Status) ) - ) - .Select(x => AccessPastryStorage(x.GetViewModel)) - .ToList(); - } + ) + .Select(x => AccessStorage(x.GetViewModel)) + .ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } - return AccessPastryStorage(source.Orders.FirstOrDefault( - x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel); + return AccessStorage(source.Orders.FirstOrDefault( + x => (model.Id.HasValue && x.Id == model.Id) || + (model.ImplementerId.HasValue && model.Status.HasValue && + x.ImplementerId == model.ImplementerId && + x.Status == model.Status))?.GetViewModel ?? new()); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -51,7 +57,7 @@ namespace ConfectioneryFileImplement.Implements } source.Orders.Add(newOrder); source.SaveOrders(); - return AccessPastryStorage(newOrder.GetViewModel); + return AccessStorage(newOrder.GetViewModel); } public OrderViewModel? Update(OrderBindingModel model) { @@ -62,7 +68,7 @@ namespace ConfectioneryFileImplement.Implements } order.Update(model); source.SaveOrders(); - return AccessPastryStorage(order.GetViewModel); + return AccessStorage(order.GetViewModel); } public OrderViewModel? Delete(OrderBindingModel model) { @@ -71,31 +77,24 @@ namespace ConfectioneryFileImplement.Implements { source.Orders.Remove(element); source.SaveOrders(); - return AccessPastryStorage(element.GetViewModel); + return AccessStorage(element.GetViewModel); } return null; } - public OrderViewModel AccessPastryStorage(OrderViewModel model) - { - if (model == null) - return null; - foreach (var Pastry in source.Pastries) - { - if (Pastry.Id == model.PastryId) - { - model.PastryName = Pastry.PastryName; - break; - } - } - return model; - } - public OrderViewModel AccessClientStorage(OrderViewModel model) + public OrderViewModel AccessStorage(OrderViewModel model) { if (model == null) return null; + var pastry = source.Pastries.FirstOrDefault(x => x.Id == model.Id); var client = source.Clients.FirstOrDefault(x => x.Id == model.Id); + var implementer = source.Implementers.FirstOrDefault( + x => x.Id == model.ImplementerId); + if (pastry != null) + model.PastryName = pastry.PastryName; if (client != null) model.ClientFIO = client.ClientFIO; + if (implementer != null) + model.ImplementerFIO = implementer.ImplementerFIO; return model; } } diff --git a/Confectionery/ConfectioneryFileImplement/Models/Implementer.cs b/Confectionery/ConfectioneryFileImplement/Models/Implementer.cs new file mode 100644 index 0000000..22d44b4 --- /dev/null +++ b/Confectionery/ConfectioneryFileImplement/Models/Implementer.cs @@ -0,0 +1,73 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +using System.Xml.Linq; +namespace ConfectioneryFileImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + public string ImplementerFIO { get; private set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int Qualification { get; set; } = 0; + public int WorkExperience { get; set; } = 0; + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + WorkExperience = model.WorkExperience, + Qualification = model.Qualification + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + WorkExperience = model.WorkExperience; + Qualification = model.Qualification; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + WorkExperience = WorkExperience, + Qualification = Qualification + }; + public static Implementer? Create(XElement element) + { + if (element == null) + { + return null; + } + return new Implementer() + { + Id = Convert.ToInt32(element.Attribute("Id")!.Value), + ImplementerFIO = element.Element("ImplementerFIO")!.Value, + Password = element.Element("Password")!.Value, + Qualification = Convert.ToInt32( + element.Element("Qualification")!.Value), + WorkExperience = Convert.ToInt32( + element.Element("WorkExperience")!.Value) + }; + } + public XElement GetXElement => new("Implementer", + new XAttribute("Id", Id), + new XElement("ImplementerFIO", ImplementerFIO), + new XElement("Password", Password), + new XElement("Qualification", Qualification.ToString()), + new XElement("WorkExperience", WorkExperience.ToString()) + ); + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryFileImplement/Models/Order.cs b/Confectionery/ConfectioneryFileImplement/Models/Order.cs index e989919..add5ee2 100644 --- a/Confectionery/ConfectioneryFileImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryFileImplement/Models/Order.cs @@ -15,6 +15,7 @@ namespace ConfectioneryFileImplement.Models public OrderStatus Status { get; private set; } public DateTime DateCreate { get; private set; } public DateTime? DateImplement { get; private set; } + public int? ImplementerId { get; private set; } = null; public static Order? Create(OrderBindingModel model) { if (model == null) @@ -31,6 +32,7 @@ namespace ConfectioneryFileImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId }; } public static Order? Create(XElement element) @@ -44,6 +46,8 @@ namespace ConfectioneryFileImplement.Models Id = Convert.ToInt32(element.Attribute("Id")!.Value), PastryId = Convert.ToInt32(element.Element("PastryId")!.Value), ClientId = Convert.ToInt32(element.Element("ClientId")!.Value), + ImplementerId = Convert.ToInt32( + element.Element("ImplementerId")!.Value), Count = Convert.ToInt32(element.Element("Count")!.Value), Sum = Convert.ToDouble(element.Element("Sum")!.Value), Status = (OrderStatus)Enum.Parse(typeof(OrderStatus), @@ -62,6 +66,7 @@ namespace ConfectioneryFileImplement.Models } Status = model.Status; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() { @@ -73,6 +78,7 @@ namespace ConfectioneryFileImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + ImplementerId = ImplementerId }; public XElement GetXElement => new("Order", new XAttribute("Id", Id), @@ -82,7 +88,8 @@ namespace ConfectioneryFileImplement.Models new XElement("Count", Count), new XElement("Status", Status.ToString()), new XElement("DateCreate", DateCreate.ToString()), - new XElement("DateImplement", DateImplement.ToString()) + new XElement("DateImplement", DateImplement.ToString()), + new XElement("ImplementerId", ImplementerId) ); } } \ No newline at end of file diff --git a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs index beb4b45..49ba186 100644 --- a/Confectionery/ConfectioneryListImplement/DataListSingleton.cs +++ b/Confectionery/ConfectioneryListImplement/DataListSingleton.cs @@ -8,12 +8,14 @@ namespace ConfectioneryListImplement public List Orders { get; set; } public List Pastries { get; set; } public List Clients { get; set; } + public List Implementers { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Pastries = new List(); Clients = new List(); + Implementers = new List(); } public static DataListSingleton GetInstance() { diff --git a/Confectionery/ConfectioneryListImplement/Implements/ImplementerStorage.cs b/Confectionery/ConfectioneryListImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..0ecd867 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,101 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.StoragesContracts; +using ConfectioneryContracts.ViewModels; +using ConfectioneryListImplement.Models; +namespace ConfectioneryListImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + private readonly DataListSingleton _source; + public ImplementerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var implementer in _source.Implementers) + { + result.Add(implementer.GetViewModel); + } + return result; + } + public List GetFilteredList( + ImplementerSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ImplementerFIO) && + string.IsNullOrEmpty(model.Password)) + { + return result; + } + foreach (var implementer in _source.Implementers) + { + if (implementer.ImplementerFIO.Contains(model.ImplementerFIO)) + { + result.Add(implementer.GetViewModel); + } + } + return result; + } + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + foreach (var implementer in _source.Implementers) + { + if ((string.IsNullOrEmpty(model.ImplementerFIO) || + implementer.ImplementerFIO == model.ImplementerFIO) && + (!model.Id.HasValue || implementer.Id == model.Id) && + (string.IsNullOrEmpty(model.Password) || + implementer.Password == model.Password)) + { + return implementer.GetViewModel; + } + } + return null; + } + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + model.Id = 1; + foreach (var implementer in _source.Implementers) + { + if (model.Id <= implementer.Id) + { + model.Id = implementer.Id + 1; + } + } + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + { + return null; + } + _source.Implementers.Add(newImplementer); + return newImplementer.GetViewModel; + } + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + foreach (var implementer in _source.Implementers) + { + if (model.Id == implementer.Id) + { + implementer.Update(model); + return implementer.GetViewModel; + } + } + return null; + } + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + for (int i = 0; i < _source.Implementers.Count; ++i) + { + if (_source.Implementers[i].Id == model.Id) + { + var element = _source.Implementers[i]; + _source.Implementers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs b/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs index 110252f..27f84d6 100644 --- a/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs +++ b/Confectionery/ConfectioneryListImplement/Implements/OrderStorage.cs @@ -17,7 +17,7 @@ namespace ConfectioneryListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - result.Add(AccessPastryStorage(order.GetViewModel)); + result.Add(AccessStorage(order.GetViewModel)); } return result; } @@ -26,13 +26,16 @@ namespace ConfectioneryListImplement.Implements var result = new List(); foreach (var order in _source.Orders) { - if ((!model.Id.HasValue || order.Id == model.Id) && - (!model.DateFrom.HasValue || order.DateCreate >= model.DateFrom) && - (!model.DateTo.HasValue || order.DateCreate <= model.DateTo) && - (!model.ClientId.HasValue || order.ClientId == model.ClientId)) - { - result.Add(AccessPastryStorage(order.GetViewModel)); - } + if ((!model.Id.HasValue || order.Id == model.Id) && + (!model.DateFrom.HasValue || order.DateCreate >= + model.DateFrom) && (!model.DateTo.HasValue || + order.DateCreate <= model.DateTo) && + (!model.ClientId.HasValue || + order.ClientId == model.ClientId) && + (!model.Status.HasValue || order.Status == model.Status)) + { + result.Add(AccessStorage(order.GetViewModel)); + } } return result; } @@ -44,9 +47,12 @@ namespace ConfectioneryListImplement.Implements } foreach (var order in _source.Orders) { - if (model.Id.HasValue && order.Id == model.Id) + if ((model.Id.HasValue && order.Id == model.Id) || + (model.ImplementerId.HasValue && model.Status.HasValue && + order.ImplementerId == model.ImplementerId && + order.Status == model.Status)) { - return order.GetViewModel; + return AccessStorage(order.GetViewModel); } } return null; @@ -94,24 +100,19 @@ namespace ConfectioneryListImplement.Implements } return null; } - public OrderViewModel AccessPastryStorage(OrderViewModel model) - { - foreach (var Pastry in _source.Pastries) - { - if (Pastry.Id == model.PastryId) - { - model.PastryName = Pastry.PastryName; - break; - } - } - return model; - } - public OrderViewModel AccessClientStorage(OrderViewModel model) + public OrderViewModel AccessStorage(OrderViewModel model) { + if (model == null) return null; + var pastry = _source.Pastries + .FirstOrDefault(x => x.Id == model.Id); var client = _source.Clients - .FirstOrDefault(x => x.Id == model.ClientId); - if (client != null) - model.ClientFIO = client.ClientFIO; + .FirstOrDefault(x => x.Id == model.Id); + var implementer = _source.Implementers + .FirstOrDefault(x => x.Id == model.ImplementerId); + if (pastry != null) model.PastryName = pastry.PastryName; + if (client != null) model.ClientFIO = client.ClientFIO; + if (implementer != null) + model.ImplementerFIO = implementer.ImplementerFIO; return model; } } diff --git a/Confectionery/ConfectioneryListImplement/Models/Implementer.cs b/Confectionery/ConfectioneryListImplement/Models/Implementer.cs new file mode 100644 index 0000000..71ada95 --- /dev/null +++ b/Confectionery/ConfectioneryListImplement/Models/Implementer.cs @@ -0,0 +1,48 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Models; +namespace ConfectioneryListImplement.Models +{ + public class Implementer : IImplementerModel + { + public int Id { get; private set; } + public string ImplementerFIO { get; private set; } = string.Empty; + public string Password { get; set; } = string.Empty; + public int WorkExperience { get; set; } = 0; + public int Qualification { get; set; } = 0; + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer() + { + Id = model.Id, + ImplementerFIO = model.ImplementerFIO, + Password = model.Password, + WorkExperience = model.WorkExperience, + Qualification = model.Qualification + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + { + return; + } + ImplementerFIO = model.ImplementerFIO; + Password = model.Password; + WorkExperience = model.WorkExperience; + Qualification = model.Qualification; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + ImplementerFIO = ImplementerFIO, + Password = Password, + WorkExperience = WorkExperience, + Qualification = Qualification + }; + } +} \ No newline at end of file diff --git a/Confectionery/ConfectioneryListImplement/Models/Order.cs b/Confectionery/ConfectioneryListImplement/Models/Order.cs index bbd8705..af96006 100644 --- a/Confectionery/ConfectioneryListImplement/Models/Order.cs +++ b/Confectionery/ConfectioneryListImplement/Models/Order.cs @@ -14,6 +14,7 @@ namespace ConfectioneryListImplement.Models public OrderStatus Status { get; private set; } public DateTime DateCreate { get; private set; } public DateTime? DateImplement { get; private set; } + public int? ImplementerId { get; private set; } public static Order? Create(OrderBindingModel? model) { if (model == null) @@ -30,6 +31,7 @@ namespace ConfectioneryListImplement.Models Status = model.Status, DateCreate = model.DateCreate, DateImplement = model.DateImplement, + ImplementerId = model.ImplementerId }; } public void Update(OrderBindingModel? model) @@ -40,6 +42,7 @@ namespace ConfectioneryListImplement.Models } Status = model.Status; DateImplement = model.DateImplement; + ImplementerId = model.ImplementerId; } public OrderViewModel GetViewModel => new() { @@ -51,6 +54,7 @@ namespace ConfectioneryListImplement.Models Status = Status, DateCreate = DateCreate, DateImplement = DateImplement, + ImplementerId = ImplementerId, }; } } diff --git a/Confectionery/ConfectioneryRestApi/Controllers/ImplementerController.cs b/Confectionery/ConfectioneryRestApi/Controllers/ImplementerController.cs new file mode 100644 index 0000000..626afd4 --- /dev/null +++ b/Confectionery/ConfectioneryRestApi/Controllers/ImplementerController.cs @@ -0,0 +1,105 @@ +using ConfectioneryContracts.BindingModels; +using ConfectioneryContracts.BusinessLogicsContracts; +using ConfectioneryContracts.SearchModels; +using ConfectioneryContracts.ViewModels; +using ConfectioneryDataModels.Enums; +using Microsoft.AspNetCore.Mvc; +namespace ConfectioneryRestApi.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class ImplementerController : ControllerBase + { + private readonly ILogger _logger; + private readonly IOrderLogic _order; + private readonly IImplementerLogic _logic; + public ImplementerController(IOrderLogic order, + IImplementerLogic logic, ILogger logger) + { + _logger = logger; + _order = order; + _logic = logic; + } + [HttpGet] + public ImplementerViewModel? Login(string login, string password) + { + try + { + return _logic.ReadElement(new ImplementerSearchModel + { + ImplementerFIO = login, + Password = password + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка авторизации сотрудника"); + throw; + } + } + [HttpGet] + public List? GetNewOrders() + { + try + { + return _order.ReadList(new OrderSearchModel + { + Status = OrderStatus.Принят + }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения новых заказов"); + throw; + } + } + [HttpGet] + public OrderViewModel? GetImplementerOrder(int implementerId) + { + try + { + return _order.ReadElement(new OrderSearchModel + { + ImplementerId = implementerId + }); + } + catch (Exception ex) + { + _logger.LogError( + ex, "Ошибка получения текущего заказа исполнителя"); + + throw; + } + } + + [HttpPost] + public void TakeOrderInWork(OrderBindingModel model) + { + try + { + _order.TakeOrderInWork(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка перевода заказа с №{Id} в работу", + model.Id); + throw; + } + } + [HttpPost] + public void FinishOrder(OrderBindingModel model) + { + try + { + _order.FinishOrder(model); + } + catch (Exception ex) + { + _logger.LogError( + ex, "Ошибка отметки о готовности заказа с №{ Id}", + model.Id); + throw; + } + } + } +} \ No newline at end of file