PIbd-22. Fedorenko G.Y. Lab Work 1 BASE #1

Closed
fedorenko wants to merge 4 commits from LabWork01 into main
8 changed files with 207 additions and 203 deletions
Showing only changes of commit e9a686fc1a - Show all commits

View File

@ -28,119 +28,127 @@
/// </summary>
private void InitializeComponent()
{
this.labelCount = new System.Windows.Forms.Label();
this.comboBoxWood = new System.Windows.Forms.ComboBox();
this.buttonCancel = new System.Windows.Forms.Button();
this.buttonSave = new System.Windows.Forms.Button();
this.textBoxCount = new System.Windows.Forms.TextBox();
this.labelProduct = new System.Windows.Forms.Label();
this.labelSum = new System.Windows.Forms.Label();
this.textBoxSum = new System.Windows.Forms.TextBox();
this.SuspendLayout();
labelCount = new Label();
comboBoxWood = new ComboBox();
buttonCancel = new Button();
buttonSave = new Button();
textBoxCount = new TextBox();
labelProduct = new Label();
labelSum = new Label();
textBoxSum = new TextBox();
SuspendLayout();
//
// labelCount
//
this.labelCount.AutoSize = true;
this.labelCount.Location = new System.Drawing.Point(12, 36);
this.labelCount.Name = "labelCount";
this.labelCount.Size = new System.Drawing.Size(69, 13);
this.labelCount.TabIndex = 2;
this.labelCount.Text = "Количество:";
labelCount.AutoSize = true;
labelCount.Location = new Point(14, 42);
labelCount.Margin = new Padding(4, 0, 4, 0);
labelCount.Name = "labelCount";
labelCount.Size = new Size(75, 15);
labelCount.TabIndex = 2;
labelCount.Text = "Количество:";
//
// comboBoxWood
//
this.comboBoxWood.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBoxWood.FormattingEnabled = true;
this.comboBoxWood.Location = new System.Drawing.Point(87, 6);
this.comboBoxWood.Name = "comboBoxWood";
this.comboBoxWood.Size = new System.Drawing.Size(217, 21);
this.comboBoxWood.TabIndex = 1;
this.comboBoxWood.SelectedIndexChanged += new System.EventHandler(this.comboBoxWood_SelectedIndexChanged);
comboBoxWood.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxWood.FormattingEnabled = true;
comboBoxWood.Location = new Point(102, 7);
comboBoxWood.Margin = new Padding(4, 3, 4, 3);
comboBoxWood.Name = "comboBoxWood";
comboBoxWood.Size = new Size(252, 23);
comboBoxWood.TabIndex = 1;
comboBoxWood.SelectedIndexChanged += comboBoxWood_SelectedIndexChanged;
//
// buttonCancel
//
this.buttonCancel.Location = new System.Drawing.Point(219, 85);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 7;
this.buttonCancel.Text = "Отмена";
this.buttonCancel.UseVisualStyleBackColor = true;
this.buttonCancel.Click += new System.EventHandler(this.ButtonCancel_Click);
buttonCancel.Location = new Point(255, 98);
buttonCancel.Margin = new Padding(4, 3, 4, 3);
buttonCancel.Name = "buttonCancel";
buttonCancel.Size = new Size(88, 27);
buttonCancel.TabIndex = 7;
buttonCancel.Text = "Отмена";
buttonCancel.UseVisualStyleBackColor = true;
buttonCancel.Click += ButtonCancel_Click;
//
// buttonSave
//
this.buttonSave.Location = new System.Drawing.Point(138, 85);
this.buttonSave.Name = "buttonSave";
this.buttonSave.Size = new System.Drawing.Size(75, 23);
this.buttonSave.TabIndex = 6;
this.buttonSave.Text = "Сохранить";
this.buttonSave.UseVisualStyleBackColor = true;
this.buttonSave.Click += new System.EventHandler(this.ButtonSave_Click);
buttonSave.Location = new Point(161, 98);
buttonSave.Margin = new Padding(4, 3, 4, 3);
buttonSave.Name = "buttonSave";
buttonSave.Size = new Size(88, 27);
buttonSave.TabIndex = 6;
buttonSave.Text = "Сохранить";
buttonSave.UseVisualStyleBackColor = true;
buttonSave.Click += ButtonSave_Click;
//
// textBoxCount
//
this.textBoxCount.Location = new System.Drawing.Point(87, 33);
this.textBoxCount.Name = "textBoxCount";
this.textBoxCount.Size = new System.Drawing.Size(217, 20);
this.textBoxCount.TabIndex = 3;
this.textBoxCount.TextChanged += new System.EventHandler(this.textBoxCount_TextChanged);
textBoxCount.Location = new Point(102, 38);
textBoxCount.Margin = new Padding(4, 3, 4, 3);
textBoxCount.Name = "textBoxCount";
textBoxCount.Size = new Size(252, 23);
textBoxCount.TabIndex = 3;
textBoxCount.TextChanged += textBoxCount_TextChanged;
//
// labelProduct
//
this.labelProduct.AutoSize = true;
this.labelProduct.Location = new System.Drawing.Point(12, 9);
this.labelProduct.Name = "labelProduct";
this.labelProduct.Size = new System.Drawing.Size(54, 13);
this.labelProduct.TabIndex = 0;
this.labelProduct.Text = "Изделие:";
labelProduct.AutoSize = true;
labelProduct.Location = new Point(14, 10);
labelProduct.Margin = new Padding(4, 0, 4, 0);
labelProduct.Name = "labelProduct";
labelProduct.Size = new Size(56, 15);
labelProduct.TabIndex = 0;
labelProduct.Text = "Изделие:";
//
// labelSum
//
this.labelSum.AutoSize = true;
this.labelSum.Location = new System.Drawing.Point(12, 62);
this.labelSum.Name = "labelSum";
this.labelSum.Size = new System.Drawing.Size(44, 13);
this.labelSum.TabIndex = 4;
this.labelSum.Text = "Сумма:";
labelSum.AutoSize = true;
labelSum.Location = new Point(14, 72);
labelSum.Margin = new Padding(4, 0, 4, 0);
labelSum.Name = "labelSum";
labelSum.Size = new Size(48, 15);
labelSum.TabIndex = 4;
labelSum.Text = "Сумма:";
//
// textBoxSum
//
this.textBoxSum.Location = new System.Drawing.Point(87, 59);
this.textBoxSum.Name = "textBoxSum";
this.textBoxSum.ReadOnly = true;
this.textBoxSum.Size = new System.Drawing.Size(217, 20);
this.textBoxSum.TabIndex = 5;
textBoxSum.Location = new Point(102, 68);
textBoxSum.Margin = new Padding(4, 3, 4, 3);
textBoxSum.Name = "textBoxSum";
textBoxSum.ReadOnly = true;
textBoxSum.Size = new Size(252, 23);
textBoxSum.TabIndex = 5;
//
// FormCreateOrder
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(318, 118);
this.Controls.Add(this.labelSum);
this.Controls.Add(this.textBoxSum);
this.Controls.Add(this.labelCount);
this.Controls.Add(this.comboBoxWood);
this.Controls.Add(this.buttonCancel);
this.Controls.Add(this.buttonSave);
this.Controls.Add(this.textBoxCount);
this.Controls.Add(this.labelProduct);
this.Name = "FormCreateOrder";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Заказ";
this.Load += new System.EventHandler(this.FormCreateOrder_Load);
this.ResumeLayout(false);
this.PerformLayout();
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(371, 136);
Controls.Add(labelSum);
Controls.Add(textBoxSum);
Controls.Add(labelCount);
Controls.Add(comboBoxWood);
Controls.Add(buttonCancel);
Controls.Add(buttonSave);
Controls.Add(textBoxCount);
Controls.Add(labelProduct);
Margin = new Padding(4, 3, 4, 3);
Name = "FormCreateOrder";
StartPosition = FormStartPosition.CenterScreen;
Text = "Заказ";
Load += FormCreateOrder_Load;
ResumeLayout(false);
PerformLayout();
}
#endregion
private System.Windows.Forms.Label labelCount;
private System.Windows.Forms.ComboBox comboBoxWood;
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Button buttonSave;
private System.Windows.Forms.TextBox textBoxCount;
private System.Windows.Forms.Label labelProduct;
private System.Windows.Forms.Label labelSum;
private System.Windows.Forms.TextBox textBoxSum;
private Label labelCount;
private ComboBox comboBoxWood;
private Button buttonCancel;
private Button buttonSave;
private TextBox textBoxCount;
private Label labelProduct;
private Label labelSum;
private TextBox textBoxSum;
}
}

View File

@ -34,7 +34,7 @@ namespace CarpentryWorkshopView
var list = _logicW.ReadList(null);
if (list != null)
{
comboBoxWood.DisplayMember = "ManufactureName";
comboBoxWood.DisplayMember = "WoodName";
comboBoxWood.ValueMember = "Id";
comboBoxWood.DataSource = list;
comboBoxWood.SelectedItem = null;
@ -93,7 +93,6 @@ namespace CarpentryWorkshopView
var operationResult = _logicO.CreateOrder(new OrderBindingModel
{
WoodId = Convert.ToInt32(comboBoxWood.SelectedValue),
WoodName = comboBoxWood.Text,
Count = Convert.ToInt32(textBoxCount.Text),
Sum = Convert.ToDouble(textBoxSum.Text)
});

View File

@ -1,16 +1,8 @@
using CarpentryWorkshop;
using CarpentryWorkshopContracts.BindingModels;
using CarpentryWorkshopContracts.BusinessLogicsContracts;
using CarpentryWorkshopDataModels.Enums;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CarpentryWorkshopView
{
@ -38,7 +30,8 @@ namespace CarpentryWorkshopView
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns[1].Visible = false;
dataGridView.Columns["WoodId"].Visible = false;
dataGridView.Columns["WoodName"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
}
_logger.LogInformation("Загрузка продуктов");
@ -75,6 +68,20 @@ namespace CarpentryWorkshopView
LoadData();
}
}
Review

Не требуется каждый раз передавать все данные заказа

Не требуется каждый раз передавать все данные заказа
private OrderBindingModel CreateBindingModel(int id, bool isDone = false)
{
return new OrderBindingModel
{
Id = id,
WoodId = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["WoodId"].Value),
Status = Enum.Parse<OrderStatus>(dataGridView.SelectedRows[0].Cells["Status"].Value.ToString()),
Count = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Count"].Value),
Sum = double.Parse(dataGridView.SelectedRows[0].Cells["Sum"].Value.ToString()),
DateCreate = DateTime.Parse(dataGridView.SelectedRows[0].Cells["DateCreate"].Value.ToString()),
};
}
private void ButtonTakeOrderInWork_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
@ -83,7 +90,7 @@ namespace CarpentryWorkshopView
_logger.LogInformation("Заказ №{id}. Меняется статус на 'В работе'", id);
try
{
var operationResult = _orderLogic.TakeOrderInWork(new OrderBindingModel { Id = id });
var operationResult = _orderLogic.TakeOrderInWork(CreateBindingModel(id));
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
@ -105,7 +112,7 @@ namespace CarpentryWorkshopView
_logger.LogInformation("Заказ №{id}. Меняется статус на 'Готов'", id);
try
{
var operationResult = _orderLogic.FinishOrder(new OrderBindingModel { Id = id });
var operationResult = _orderLogic.FinishOrder(CreateBindingModel(id));
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");
@ -126,7 +133,7 @@ namespace CarpentryWorkshopView
_logger.LogInformation("Заказ №{id}. Меняется статус на 'Выдан'", id);
try
{
var operationResult = _orderLogic.DeliveryOrder(new OrderBindingModel { Id = id });
var operationResult = _orderLogic.DeliveryOrder(CreateBindingModel(id));
if (!operationResult)
{
throw new Exception("Ошибка при сохранении. Дополнительная информация в логах.");

View File

@ -34,15 +34,15 @@
textBoxPrice = new TextBox();
groupBox1 = new GroupBox();
dataGridView = new DataGridView();
ID = new DataGridViewTextBoxColumn();
ComponentName = new DataGridViewTextBoxColumn();
Count = new DataGridViewTextBoxColumn();
ButtonRef = new Button();
ButtonDel = new Button();
ButtonUpd = new Button();
ButtonAdd = new Button();
ButtonSave = new Button();
ButtonCancel = new Button();
ID = new DataGridViewTextBoxColumn();
ComponentName = new DataGridViewTextBoxColumn();
Count = new DataGridViewTextBoxColumn();
groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
@ -76,6 +76,7 @@
//
textBoxPrice.Location = new Point(94, 38);
textBoxPrice.Name = "textBoxPrice";
textBoxPrice.ReadOnly = true;
textBoxPrice.Size = new Size(100, 23);
textBoxPrice.TabIndex = 3;
//
@ -104,6 +105,24 @@
dataGridView.Size = new Size(394, 311);
dataGridView.TabIndex = 5;
//
// ID
//
ID.HeaderText = "Id";
ID.Name = "ID";
ID.Visible = false;
ID.Width = 250;
//
// ComponentName
//
ComponentName.HeaderText = "Компонент";
ComponentName.Name = "ComponentName";
ComponentName.Width = 250;
//
// Count
//
Count.HeaderText = "Количество";
Count.Name = "Count";
//
// ButtonRef
//
ButtonRef.Location = new Point(400, 169);
@ -164,24 +183,6 @@
ButtonCancel.UseVisualStyleBackColor = true;
ButtonCancel.Click += ButtonCancel_Click;
//
// ID
//
ID.HeaderText = "Id";
ID.Name = "ID";
ID.Visible = false;
ID.Width = 250;
//
// ComponentName
//
ComponentName.HeaderText = "Компонент";
ComponentName.Name = "ComponentName";
ComponentName.Width = 250;
//
// Count
//
Count.HeaderText = "Количество";
Count.Name = "Count";
//
// FormWood
//
AutoScaleDimensions = new SizeF(7F, 15F);

View File

@ -63,6 +63,7 @@
//
// comboBoxComponent
//
comboBoxComponent.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxComponent.BackColor = SystemColors.Window;
comboBoxComponent.FormattingEnabled = true;
comboBoxComponent.Location = new Point(90, 6);

View File

@ -12,15 +12,18 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics
{
private readonly ILogger _logger;
private readonly IOrderStorage _orderStorage;
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
{
_logger = logger;
_orderStorage = orderStorage;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model);
var list = model == null ? _orderStorage.GetFullList() :
_orderStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
@ -29,102 +32,75 @@ namespace CarpentryWorkshopBusinessLogic.BusinessLogics
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool CreateOrder(OrderBindingModel model)
{
_orderStorage.Insert(new OrderBindingModel
CheckModel(model);
if (model.Status != OrderStatus.Неизвестен) return false;
model.Status = OrderStatus.Принят;
if (_orderStorage.Insert(model) == null)
{
WoodId = model.WoodId,
WoodName = model.WoodName,
Count = model.Count,
Sum = model.Sum,
Status = OrderStatus.Принят,
DateCreate = DateTime.Now
}); ;
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool ChangeStatus(OrderBindingModel model, OrderStatus status)
{
CheckModel(model);
var element = _orderStorage.GetElement(new OrderSearchModel { Id = model.Id });
if (element == null)
{
_logger.LogWarning("Read operation failed");
return false;
}
if (element.Status != status - 1)
{
_logger.LogWarning("Status change operation failed");
throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный");
}
model.Status = status;
if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now;
_orderStorage.Update(model);
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
var order = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (order == null)
{
throw new Exception("Элемент не найден");
}
if (order.Status.ToString() != "Принят")
{
throw new Exception("Заказ не в статусе \"Принят\"");
}
_orderStorage.Update(new OrderBindingModel
{
Id = order.Id,
WoodId = order.WoodId,
WoodName = order.WoodName,
Count = order.Count,
Sum = order.Sum,
DateCreate = order.DateCreate,
Status = OrderStatus.Выполняется
});
return true;
return ChangeStatus(model, OrderStatus.Выполняется);
}
public bool FinishOrder(OrderBindingModel model)
{
var order = _orderStorage.GetElement(new OrderSearchModel
{
Id = model.Id
});
if (order == null)
{
throw new Exception("Не найден заказ");
}
if (order.Status.ToString() != "Выполняется")
{
throw new Exception("Заказ не в статусе \"Выполняется\"");
}
_orderStorage.Update(new OrderBindingModel
{
Id = order.Id,
WoodId = order.WoodId,
WoodName = order.WoodName,
Count = order.Count,
Sum = order.Sum,
DateCreate = order.DateCreate,
DateImplement = DateTime.Now,
Status = OrderStatus.Готов
});
return true;
return ChangeStatus(model, OrderStatus.Готов);
}
public bool DeliveryOrder(OrderBindingModel model)
{
var order = _orderStorage.GetElement(new OrderSearchModel
return ChangeStatus(model, OrderStatus.Выдан);
}
private void CheckModel(OrderBindingModel model, bool withParams =
true)
{
if (model == null)
{
Id = model.Id
});
if (order == null)
{
throw new Exception("Не найден заказ");
throw new ArgumentNullException(nameof(model));
}
if (order.Status.ToString() != "Готов")
if (!withParams)
{
throw new Exception("Заказ не в статусе \"Готов\"");
return;
}
_orderStorage.Update(new OrderBindingModel
if (model.Sum <= 0)
{
Id = order.Id,
WoodId = order.WoodId,
WoodName = order.WoodName,
Count = order.Count,
Sum = order.Sum,
DateCreate = order.DateCreate,
DateImplement = order.DateImplement,
Status = OrderStatus.Выдан
});
return true;
throw new ArgumentNullException("Цена заказа должна быть больше 0", nameof(model.Sum));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество элементов в заказе должно быть больше 0", nameof(model.Count));
}
_logger.LogInformation("Order. Sum:{ Cost}. Id: { Id}", model.Sum, model.Id);
}
}
}
}

View File

@ -7,7 +7,6 @@ namespace CarpentryWorkshopContracts.BindingModels
{
public int Id { get; set; }
public int WoodId { get; set; }
public string WoodName { get; set; } = string.Empty;
public int Count { get; set; }
public double Sum { get; set; }
public OrderStatus Status { get; set; } = OrderStatus.Неизвестен;

View File

@ -18,7 +18,7 @@ namespace CarpentryWorkshopListImplement.Implements
var result = new List<OrderViewModel>();
foreach (var order in _source.Orders)
{
result.Add(order.GetViewModel);
result.Add(AccessWoodStorage(order.GetViewModel));
}
return result;
}
@ -33,7 +33,7 @@ namespace CarpentryWorkshopListImplement.Implements
{
if (order.Id.Equals(model.Id))
{
result.Add(order.GetViewModel);
result.Add(AccessWoodStorage(order.GetViewModel));
}
}
return result;
@ -97,5 +97,18 @@ namespace CarpentryWorkshopListImplement.Implements
}
return null;
}
public OrderViewModel AccessWoodStorage(OrderViewModel model)
{
foreach (var wood in _source.Woods)
{
if (wood.Id == model.WoodId)
{
model.WoodName = wood.WoodName;
break;
}
}
return model;
}
}
}