Лаба 1. Готова.

This commit is contained in:
Андрей Байгулов 2024-02-15 21:24:26 +04:00
parent 59b1467ac7
commit 6898b826c6
11 changed files with 89 additions and 83 deletions

View File

@ -11,7 +11,7 @@ namespace SushiBarContracts.ViewModels
public class SushiViewModel : ISushiModel
{
public int Id { get; set; }
[DisplayName("Название изделия")]
[DisplayName("Название суши")]
public string SushiName { get; set; } = string.Empty;
[DisplayName("Цена")]
public double Price { get; set; }

View File

@ -39,6 +39,7 @@
// dataGridView
//
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(2, 2);
dataGridView.Name = "dataGridView";

View File

@ -79,6 +79,7 @@
comboBoxSushi.Name = "comboBoxSushi";
comboBoxSushi.Size = new Size(276, 23);
comboBoxSushi.TabIndex = 4;
comboBoxSushi.SelectedIndexChanged += comboBoxSushi_SelectedIndexChanged;
//
// textBoxSum
//
@ -123,6 +124,7 @@
Controls.Add(labelSushi);
Name = "FormCreateOrder";
Text = "Заказ";
Load += FormCreateOrder_Load;
ResumeLayout(false);
PerformLayout();
}

View File

@ -2,7 +2,9 @@
using SushiBarContracts.BindingModels;
using SushiBarContracts.BusinessLogicsContracts;
using SushiBarContracts.SearchModels;
using SushiBarContracts.ViewModels;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
@ -12,13 +14,19 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SushiBarView
{
public partial class FormCreateOrder : Form
{
public FormCreateOrder()
{
InitializeComponent();
}
private readonly ILogger _logger;
private readonly ISushiLogic _logicP;
private readonly IOrderLogic _logicO;
private List<SushiViewModel>? _list;
public FormCreateOrder(ILogger<FormCreateOrder> logger, ISushiLogic
logicP, IOrderLogic logicO)
{
@ -29,8 +37,16 @@ namespace SushiBarView
}
private void FormCreateOrder_Load(object sender, EventArgs e)
{
_logger.LogInformation("Загрузка изделий для заказа");
// прописать логику
_logger.LogInformation("Загрузка суши для заказа");
_list = _logicP.ReadList(null);
if (_list != null)
{
comboBoxSushi.DisplayMember = "SushiName";
comboBoxSushi.ValueMember = "Id";
comboBoxSushi.DataSource = _list;
comboBoxSushi.SelectedItem = null;
_logger.LogInformation("Загрузка суши для заказа");
}
}
private void CalcSum()
{
@ -40,14 +56,9 @@ namespace SushiBarView
try
{
int id = Convert.ToInt32(comboBoxSushi.SelectedValue);
var product = _logicP.ReadElement(new SushiSearchModel
{
Id
= id
});
var sushi = _logicP.ReadElement(new SushiSearchModel { Id = id });
int count = Convert.ToInt32(textBoxCount.Text);
textBoxSum.Text = Math.Round(count * (product?.Price ?? 0),
2).ToString();
textBoxSum.Text = Math.Round(count * (sushi?.Price ?? 0), 2).ToString();
_logger.LogInformation("Расчет суммы заказа");
}
catch (Exception ex)
@ -62,8 +73,7 @@ namespace SushiBarView
{
CalcSum();
}
private void ComboBoxSushi_SelectedIndexChanged(object sender,
EventArgs e)
private void comboBoxSushi_SelectedIndexChanged(object sender, EventArgs e)
{
CalcSum();
}
@ -77,7 +87,7 @@ namespace SushiBarView
}
if (comboBoxSushi.SelectedValue == null)
{
MessageBox.Show("Выберите изделие", "Ошибка",
MessageBox.Show("Выберите суши", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}

View File

@ -44,6 +44,7 @@
//
// dataGridView
//
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(1, 29);
dataGridView.Name = "dataGridView";
@ -89,6 +90,7 @@
ButtonIssuedOrder.TabIndex = 4;
ButtonIssuedOrder.Text = "Заказ выдан";
ButtonIssuedOrder.UseVisualStyleBackColor = true;
ButtonIssuedOrder.Click += ButtonIssuedOrder_Click;
//
// ButtonRef
//
@ -119,14 +121,14 @@
// componentsToolStripMenuItem
//
componentsToolStripMenuItem.Name = "componentsToolStripMenuItem";
componentsToolStripMenuItem.Size = new Size(180, 22);
componentsToolStripMenuItem.Size = new Size(145, 22);
componentsToolStripMenuItem.Text = "Компоненты";
componentsToolStripMenuItem.Click += componentsToolStripMenuItem_Click;
//
// sushiToolStripMenuItem
//
sushiToolStripMenuItem.Name = "sushiToolStripMenuItem";
sushiToolStripMenuItem.Size = new Size(180, 22);
sushiToolStripMenuItem.Size = new Size(145, 22);
sushiToolStripMenuItem.Text = "Суши";
sushiToolStripMenuItem.Click += sushiToolStripMenuItem_Click;
//
@ -144,7 +146,7 @@
Controls.Add(menuStrip);
MainMenuStrip = menuStrip;
Name = "FormMain";
Text = "FormMain";
Text = "Суши-бар";
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
menuStrip.ResumeLayout(false);
menuStrip.PerformLayout();

View File

@ -40,9 +40,6 @@
dataGridView = new DataGridView();
ButtonSave = new Button();
ButtonCancel = new Button();
idColumn = new DataGridViewTextBoxColumn();
componentColumn = new DataGridViewTextBoxColumn();
countColumn = new DataGridViewTextBoxColumn();
groupBoxComponents.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
@ -138,8 +135,8 @@
//
dataGridView.Anchor = AnchorStyles.Top;
dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Columns.AddRange(new DataGridViewColumn[] { idColumn, componentColumn, countColumn });
dataGridView.Location = new Point(6, 22);
dataGridView.Name = "dataGridView";
dataGridView.RowTemplate.Height = 25;
@ -166,22 +163,6 @@
ButtonCancel.UseVisualStyleBackColor = true;
ButtonCancel.Click += ButtonCancel_Click;
//
// idColumn
//
idColumn.HeaderText = "id";
idColumn.Name = "idColumn";
idColumn.Visible = false;
//
// componentColumn
//
componentColumn.HeaderText = "Компонент";
componentColumn.Name = "componentColumn";
//
// countColumn
//
countColumn.HeaderText = "Количество";
countColumn.Name = "countColumn";
//
// FormSushi
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -195,7 +176,7 @@
Controls.Add(labelCost);
Controls.Add(labelName);
Name = "FormSushi";
Text = "FormSushi";
Text = "Суши";
groupBoxComponents.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
@ -216,8 +197,5 @@
private Button ButtonAdd;
private Button ButtonSave;
private Button ButtonCancel;
private DataGridViewTextBoxColumn idColumn;
private DataGridViewTextBoxColumn componentColumn;
private DataGridViewTextBoxColumn countColumn;
}
}

View File

@ -20,39 +20,60 @@ namespace SushiBarView
private readonly ILogger _logger;
private readonly ISushiLogic _logic;
private int? _id;
private Dictionary<int, (IComponentModel, int)> _productComponents;
private Dictionary<int, (IComponentModel, int)> _sushiComponents;
public int Id { set { _id = value; } }
public FormSushi()
{
InitializeComponent();
}
public FormSushi(ILogger<FormSushi> logger, ISushiLogic logic)
{
InitializeComponent();
_logger = logger;
_logic = logic;
_productComponents = new Dictionary<int, (IComponentModel, int)>();
_sushiComponents = new Dictionary<int, (IComponentModel, int)>();
}
private void FormSushi_Load(object sender, EventArgs e)
{
var list = _logic.ReadList(null);
if (list != null)
{
dataGridView.AllowUserToAddRows = false;
DataGridViewTextBoxColumn Id = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn Component = new DataGridViewTextBoxColumn();
DataGridViewTextBoxColumn Number = new DataGridViewTextBoxColumn();
Component.HeaderText = "Количество";
Number.HeaderText = "Компонент";
Number.Name = "Number";
Id.Name = "Id";
dataGridView.Columns.Add(Id);
dataGridView.Columns.Add(Number);
dataGridView.Columns.Add(Component);
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["Number"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
if (_id.HasValue)
{
_logger.LogInformation("Загрузка изделия");
_logger.LogInformation("Загрузка суши");
try
{
var view = _logic.ReadElement(new SushiSearchModel
{
Id =
_id.Value
Id = _id.Value
});
if (view != null)
{
textBoxName.Text = view.SushiName;
textBoxPrice.Text = view.Price.ToString();
_productComponents = view.SushiComponents ?? new
_sushiComponents = view.SushiComponents ?? new
Dictionary<int, (IComponentModel, int)>();
LoadData();
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки изделия");
_logger.LogError(ex, "Ошибка загрузки суши");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
@ -60,13 +81,13 @@ namespace SushiBarView
}
private void LoadData()
{
_logger.LogInformation("Загрузка компонент изделия");
_logger.LogInformation("Загрузка компонент суши");
try
{
if (_productComponents != null)
if (_sushiComponents != null)
{
dataGridView.Rows.Clear();
foreach (var pc in _productComponents)
foreach (var pc in _sushiComponents)
{
dataGridView.Rows.Add(new object[] { pc.Key, pc.Value.Item1.ComponentName, pc.Value.Item2 });
}
@ -75,7 +96,7 @@ namespace SushiBarView
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка загрузки компонент изделия");
_logger.LogError(ex, "Ошибка загрузки компонент суши");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
@ -93,14 +114,14 @@ namespace SushiBarView
return;
}
_logger.LogInformation("Добавление нового компонента: { ComponentName}- { Count}", form.ComponentModel.ComponentName, form.Count);
if (_productComponents.ContainsKey(form.Id))
if (_sushiComponents.ContainsKey(form.Id))
{
_productComponents[form.Id] = (form.ComponentModel,
_sushiComponents[form.Id] = (form.ComponentModel,
form.Count);
}
else
{
_productComponents.Add(form.Id, (form.ComponentModel,
_sushiComponents.Add(form.Id, (form.ComponentModel,
form.Count));
}
LoadData();
@ -118,7 +139,7 @@ namespace SushiBarView
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value);
form.Id = id;
form.Count = _productComponents[id].Item2;
form.Count = _sushiComponents[id].Item2;
if (form.ShowDialog() == DialogResult.OK)
{
if (form.ComponentModel == null)
@ -126,7 +147,7 @@ namespace SushiBarView
return;
}
_logger.LogInformation("Изменение компонента: { ComponentName} - { Count}", form.ComponentModel.ComponentName, form.Count);
_productComponents[form.Id] = (form.ComponentModel, form.Count);
_sushiComponents[form.Id] = (form.ComponentModel, form.Count);
LoadData();
}
}
@ -141,7 +162,7 @@ namespace SushiBarView
{
try
{
_logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); _productComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
_logger.LogInformation("Удаление компонента: { ComponentName} - { Count}", dataGridView.SelectedRows[0].Cells[1].Value); _sushiComponents?.Remove(Convert.ToInt32(dataGridView.SelectedRows[0].Cells[0].Value));
}
catch (Exception ex)
{
@ -170,13 +191,13 @@ namespace SushiBarView
MessageBoxIcon.Error);
return;
}
if (_productComponents == null || _productComponents.Count == 0)
if (_sushiComponents == null || _sushiComponents.Count == 0)
{
MessageBox.Show("Заполните компоненты", "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_logger.LogInformation("Сохранение изделия");
_logger.LogInformation("Сохранение суши");
try
{
var model = new SushiBindingModel
@ -184,7 +205,7 @@ namespace SushiBarView
Id = _id ?? 0,
SushiName = textBoxName.Text,
Price = Convert.ToDouble(textBoxPrice.Text),
SushiComponents = _productComponents
SushiComponents = _sushiComponents
};
var operationResult = _id.HasValue ? _logic.Update(model) :
_logic.Create(model);
@ -199,7 +220,7 @@ namespace SushiBarView
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка сохранения изделия");
_logger.LogError(ex, "Ошибка сохранения суши");
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
@ -211,7 +232,7 @@ namespace SushiBarView
private double CalcPrice()
{
double price = 0;
foreach (var elem in _productComponents)
foreach (var elem in _sushiComponents)
{
price += ((elem.Value.Item1?.Cost ?? 0) * elem.Value.Item2);
}

View File

@ -117,13 +117,4 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="idColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="componentColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="countColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root>

View File

@ -101,7 +101,7 @@
Controls.Add(labelCount);
Controls.Add(labelComponent);
Name = "FormSushiComponent";
Text = "Компонент изделия";
Text = "Компонент суши";
ResumeLayout(false);
PerformLayout();
}

View File

@ -38,6 +38,7 @@
//
// dataGridView
//
dataGridView.BackgroundColor = SystemColors.Window;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(12, 12);
dataGridView.Name = "dataGridView";
@ -96,7 +97,7 @@
Controls.Add(ButtonAdd);
Controls.Add(dataGridView);
Name = "FormSushis";
Text = "FormSushis";
Text = "Суши";
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
}

View File

@ -1,15 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" internalLogLevel="Info">
<nlog xmlns="http://www.nlog-project.org/schemas/NLog.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
autoReload="true" internalLogLevel="Info">
<targets>
<target xsi:type="File" name="tofile" fileName="log-${shortdate}.log" />
</targets>
<targets>
<target xsi:type="File" name="tofile" fileName="log-${shortdate}.log" />
</targets>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile" />
</rules>
</nlog>
<rules>
<logger name="*" minlevel="Debug" writeTo="tofile" />
</rules>
</nlog>
</configuration>