LabWork№1 #6

Merged
MorozovDanil merged 4 commits from LabWork№1 into LabWork№2 2024-12-15 19:15:08 +04:00
9 changed files with 152 additions and 177 deletions

View File

@ -14,17 +14,17 @@ public class Order
public DateTime DateIssue { get; private set; }
public int FullPrice { get; private set; }
public int MasterId { get; private set; }
public IEnumerable<OrderDetail> OrderDetails { get; private set; } = [];
public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion,
public IEnumerable<OrderDetail> OrderDetails { get; private set; } = [];
public static Order CreateOperation(int id, int fullPrice, int masterId, DateTime dateCompletion,
DateTime dateIssue, IEnumerable<OrderDetail> orderDetails)
{
return new Order
{
Id = id,
OrderDate = DateTime.Now,
OrderDate = DateTime.Now,
DateCompletion = dateCompletion,
DateIssue = dateIssue,
FullPrice = fullPrice,

View File

@ -10,7 +10,7 @@ public class StorageDetail
{
public int StorageId { get; private set; }
public int DetailId { get; private set; }
public int DetailCount { get; private set; }
public int DetailCount { get; private set; }
public DateTime SupplyDate { get; private set; } // Дата поступления деталей
public static StorageDetail CreateOperation(int storageId, int detailId, int detailCount, DateTime supplyDate)

View File

@ -32,7 +32,7 @@
ButtonCancel = new Button();
label1 = new Label();
label2 = new Label();
dateTimePicker1 = new DateTimePicker();
dateTimePickerOrderDate = new DateTimePicker();
dateTimeCompletion = new DateTimePicker();
dateTimeIssue = new DateTimePicker();
label3 = new Label();
@ -40,12 +40,12 @@
numericUpDownFullPrice = new NumericUpDown();
label5 = new Label();
comboBoxMaster = new ComboBox();
groupBox1 = new GroupBox();
groupBoxDetail = new GroupBox();
dataGridView = new DataGridView();
Detail = new DataGridViewComboBoxColumn();
DetailCount = new DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)numericUpDownFullPrice).BeginInit();
groupBox1.SuspendLayout();
groupBoxDetail.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
SuspendLayout();
//
@ -89,13 +89,13 @@
label2.TabIndex = 3;
label2.Text = "Дата начала работы:";
//
// dateTimePicker1
// dateTimePickerOrderDate
//
dateTimePicker1.Enabled = false;
dateTimePicker1.Location = new Point(190, 12);
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new Size(188, 25);
dateTimePicker1.TabIndex = 4;
dateTimePickerOrderDate.Enabled = false;
dateTimePickerOrderDate.Location = new Point(190, 12);
dateTimePickerOrderDate.Name = "dateTimePickerOrderDate";
dateTimePickerOrderDate.Size = new Size(188, 25);
dateTimePickerOrderDate.TabIndex = 4;
//
// dateTimeCompletion
//
@ -156,16 +156,17 @@
comboBoxMaster.Size = new Size(182, 25);
comboBoxMaster.TabIndex = 11;
//
// groupBox1
// groupBoxDetail
//
groupBox1.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
groupBox1.Controls.Add(dataGridView);
groupBox1.Location = new Point(25, 234);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(401, 224);
groupBox1.TabIndex = 12;
groupBox1.TabStop = false;
groupBox1.Text = "Деталь";
groupBoxDetail.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
groupBoxDetail.Controls.Add(dataGridView);
groupBoxDetail.Location = new Point(30, 234);
groupBoxDetail.Name = "groupBoxDetail";
groupBoxDetail.RightToLeft = RightToLeft.No;
groupBoxDetail.Size = new Size(401, 224);
groupBoxDetail.TabIndex = 12;
groupBoxDetail.TabStop = false;
groupBoxDetail.Text = "Детали";
//
// dataGridView
//
@ -179,6 +180,7 @@
dataGridView.MultiSelect = false;
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersVisible = false;
dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
dataGridView.Size = new Size(395, 200);
dataGridView.TabIndex = 0;
//
@ -197,7 +199,7 @@
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(461, 532);
Controls.Add(groupBox1);
Controls.Add(groupBoxDetail);
Controls.Add(comboBoxMaster);
Controls.Add(label5);
Controls.Add(numericUpDownFullPrice);
@ -205,7 +207,7 @@
Controls.Add(label3);
Controls.Add(dateTimeIssue);
Controls.Add(dateTimeCompletion);
Controls.Add(dateTimePicker1);
Controls.Add(dateTimePickerOrderDate);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(ButtonCancel);
@ -214,7 +216,7 @@
StartPosition = FormStartPosition.CenterParent;
Text = "Заказ";
((System.ComponentModel.ISupportInitialize)numericUpDownFullPrice).EndInit();
groupBox1.ResumeLayout(false);
groupBoxDetail.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
ResumeLayout(false);
PerformLayout();
@ -226,7 +228,7 @@
private Button ButtonCancel;
private Label label1;
private Label label2;
private DateTimePicker dateTimePicker1;
private DateTimePicker dateTimePickerOrderDate;
private DateTimePicker dateTimeCompletion;
private DateTimePicker dateTimeIssue;
private Label label3;
@ -234,7 +236,7 @@
private NumericUpDown numericUpDownFullPrice;
private Label label5;
private ComboBox comboBoxMaster;
private GroupBox groupBox1;
private GroupBox groupBoxDetail;
private DataGridView dataGridView;
private DataGridViewComboBoxColumn Detail;
private DataGridViewTextBoxColumn DetailCount;

View File

@ -10,56 +10,54 @@ using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ProjectRepairCompany.Forms;
public partial class FormOrder : Form
namespace ProjectRepairCompany.Forms
{
private readonly IOrderRepository _orderRepository;
private int? _orderId;
public int Id
public partial class FormOrder : Form
{
set
private readonly IOrderRepository _orderRepository;
private int? _orderId;
public int Id
{
try
set
{
var order = _orderRepository.ReadOrderById(value);
if (order == null)
try
{
throw new InvalidOperationException(nameof(order));
var order = _orderRepository.ReadOrderById(value);
if (order == null)
{
throw new InvalidOperationException(nameof(order));
}
comboBoxMaster.SelectedValue = order.MasterId;
numericUpDownFullPrice.Value = (decimal)order.FullPrice;
dateTimeCompletion.Value = order.DateCompletion;
dateTimeIssue.Value = order.DateIssue;
foreach (var detail in order.OrderDetails)
{
dataGridView.Rows.Add(detail.DetailId, detail.DetailCount);
}
_orderId = value;
}
comboBoxMaster.SelectedValue = order.MasterId;
numericUpDownFullPrice.Value = (decimal)order.FullPrice;
dateTimeCompletion.Value = order.DateCompletion;
dateTimeIssue.Value = order.DateIssue;
dataGridView.Rows.Clear();
foreach (var detail in order.OrderDetails)
catch (Exception ex)
{
dataGridView.Rows.Add(detail.DetailId, detail.DetailCount);
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
_orderId = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
public FormOrder(IOrderRepository orderRepository, IMasterRepository masterRepository, IDetailRepository detailRepository)
{
InitializeComponent();
public FormOrder(IOrderRepository orderRepository, IMasterRepository masterRepository, IDetailRepository detailRepository)
{
InitializeComponent();
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
_orderRepository = orderRepository ?? throw new ArgumentNullException(nameof(orderRepository));
comboBoxMaster.DataSource = masterRepository.ReadMasters();
comboBoxMaster.DisplayMember = "MasterName";
comboBoxMaster.ValueMember = "Id";
comboBoxMaster.DataSource = masterRepository.ReadMasters();
comboBoxMaster.DisplayMember = "MasterName";
comboBoxMaster.ValueMember = "Id";
Detail.DataSource = detailRepository.ReadDetails();
Detail.DisplayMember = "NameDetail";
@ -75,54 +73,56 @@ public partial class FormOrder : Form
throw new Exception("Имеются незаполненные поля.");
}
var order = CreateOrder();
if (_orderId.HasValue)
{
_orderRepository.UpdateOrder(order);
var order = CreateOrder();
if (_orderId.HasValue)
{
_orderRepository.UpdateOrder(order);
}
else
{
_orderRepository.CreateOrder(order);
}
DialogResult = DialogResult.OK;
Close();
}
else
catch (Exception ex)
{
_orderRepository.CreateOrder(order);
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
DialogResult = DialogResult.OK;
Close();
}
catch (Exception ex)
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<OrderDetail> CreateListOrderDetailsFromDataGrid()
{
MessageBox.Show(ex.Message, "Ошибка при сохранении", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ButtonCancel_Click(object sender, EventArgs e) => Close();
private List<OrderDetail> CreateListOrderDetailsFromDataGrid()
{
var list = new List<OrderDetail>();
foreach (DataGridViewRow row in dataGridView.Rows)
{
if (row.Cells["Detail"].Value == null ||
row.Cells["DetailCount"].Value == null)
var list = new List<OrderDetail>();
foreach (DataGridViewRow row in dataGridView.Rows)
{
continue;
if (row.Cells["Detail"].Value == null ||
row.Cells["DetailCount"].Value == null)
{
continue;
}
list.Add(OrderDetail.CreateOperation(0,
Convert.ToInt32(row.Cells["Detail"].Value),
Convert.ToInt32(row.Cells["DetailCount"].Value)));
}
list.Add(OrderDetail.CreateOperation(0,
Convert.ToInt32(row.Cells["Detail"].Value),
Convert.ToInt32(row.Cells["DetailCount"].Value)));
return list;
}
private Order CreateOrder()
{
var orderDetails = CreateListOrderDetailsFromDataGrid();
return Order.CreateOperation(
_orderId ?? 0,
Convert.ToInt32(numericUpDownFullPrice.Value),
(int)comboBoxMaster.SelectedValue,
dateTimeCompletion.Value,
dateTimeIssue.Value,
orderDetails
);
}
return list;
}
private Order CreateOrder()
{
var orderDetails = CreateListOrderDetailsFromDataGrid();
return Order.CreateOperation(
_orderId ?? 0,
Convert.ToInt32(numericUpDownFullPrice.Value),
(int)comboBoxMaster.SelectedValue,
dateTimeCompletion.Value,
dateTimeIssue.Value,
orderDetails
);
}

View File

@ -1,6 +1,6 @@
using ProjectRepairCompany.Entities;
using ProjectRepairCompany.Repositories;
using ProjectRepairCompany.Repositories.Implementations;
using ProjectRepairCompany.Entities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
@ -62,12 +62,10 @@ namespace ProjectRepairCompany.Forms
try
{
var form = _container.Resolve<FormOrder>();
form.Id = findId;
if (form.ShowDialog() == DialogResult.OK)
{
LoadList();
LoadList();
dataGridView.Refresh();
}
}
@ -99,8 +97,7 @@ namespace ProjectRepairCompany.Forms
}
}
private void LoadList() => dataGridView.DataSource =_orderRepository.ReadOrders();
private void LoadList() => dataGridView.DataSource = _orderRepository.ReadOrders();
private bool TryGetIdentifierFromSelectRow(out int id)
{

View File

@ -33,12 +33,12 @@
label1 = new Label();
label2 = new Label();
label3 = new Label();
numericUpDown1 = new NumericUpDown();
dateTimePicker1 = new DateTimePicker();
comboBoxDetail = new ComboBox();
numericUpDownCount = new NumericUpDown();
dateTimePickerDate = new DateTimePicker();
comboBoxAddress = new ComboBox();
label4 = new Label();
((System.ComponentModel.ISupportInitialize)numericUpDown1).BeginInit();
comboBoxDetail = new ComboBox();
((System.ComponentModel.ISupportInitialize)numericUpDownCount).BeginInit();
SuspendLayout();
//
// ButtonSave
@ -88,65 +88,65 @@
label3.TabIndex = 4;
label3.Text = "Дата";
//
// numericUpDown1
// numericUpDownCount
//
numericUpDown1.Location = new Point(128, 62);
numericUpDown1.Name = "numericUpDown1";
numericUpDown1.Size = new Size(154, 25);
numericUpDown1.TabIndex = 5;
numericUpDownCount.Location = new Point(128, 62);
numericUpDownCount.Name = "numericUpDownCount";
numericUpDownCount.Size = new Size(154, 25);
numericUpDownCount.TabIndex = 5;
//
// dateTimePicker1
// dateTimePickerDate
//
dateTimePicker1.Location = new Point(119, 104);
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new Size(187, 25);
dateTimePicker1.TabIndex = 6;
//
// comboBoxDetail
//
comboBoxDetail.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxDetail.FormattingEnabled = true;
comboBoxDetail.Location = new Point(128, 23);
comboBoxDetail.Name = "comboBoxDetail";
comboBoxDetail.Size = new Size(162, 25);
comboBoxDetail.TabIndex = 7;
dateTimePickerDate.Location = new Point(119, 104);
dateTimePickerDate.Name = "dateTimePickerDate";
dateTimePickerDate.Size = new Size(187, 25);
dateTimePickerDate.TabIndex = 6;
//
// comboBoxAddress
//
comboBoxAddress.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxAddress.FormattingEnabled = true;
comboBoxAddress.Location = new Point(119, 135);
comboBoxAddress.Location = new Point(119, 148);
comboBoxAddress.Name = "comboBoxAddress";
comboBoxAddress.Size = new Size(187, 25);
comboBoxAddress.Size = new Size(183, 25);
comboBoxAddress.TabIndex = 8;
//
// label4
//
label4.AutoSize = true;
label4.Location = new Point(12, 143);
label4.Location = new Point(15, 151);
label4.Name = "label4";
label4.Size = new Size(91, 17);
label4.TabIndex = 9;
label4.Text = "Адрес склада:";
//
// comboBoxDetail
//
comboBoxDetail.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxDetail.FormattingEnabled = true;
comboBoxDetail.Location = new Point(119, 23);
comboBoxDetail.Name = "comboBoxDetail";
comboBoxDetail.Size = new Size(183, 25);
comboBoxDetail.TabIndex = 10;
//
// FormStorageDetail
//
AutoScaleDimensions = new SizeF(7F, 17F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(393, 295);
ClientSize = new Size(393, 283);
Controls.Add(comboBoxDetail);
Controls.Add(label4);
Controls.Add(comboBoxAddress);
Controls.Add(comboBoxDetail);
Controls.Add(dateTimePicker1);
Controls.Add(numericUpDown1);
Controls.Add(dateTimePickerDate);
Controls.Add(numericUpDownCount);
Controls.Add(label3);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(ButtonCancel);
Controls.Add(ButtonSave);
Name = "FormStorageDetail";
Text = "Пополнение склада";
((System.ComponentModel.ISupportInitialize)numericUpDown1).EndInit();
Text = "FormStorageDetail";
((System.ComponentModel.ISupportInitialize)numericUpDownCount).EndInit();
ResumeLayout(false);
PerformLayout();
}
@ -158,10 +158,10 @@
private Label label1;
private Label label2;
private Label label3;
private NumericUpDown numericUpDown1;
private DateTimePicker dateTimePicker1;
private ComboBox comboBoxDetail;
private NumericUpDown numericUpDownCount;
private DateTimePicker dateTimePickerDate;
private ComboBox comboBoxAddress;
private Label label4;
private ComboBox comboBoxDetail;
}
}

View File

@ -10,30 +10,24 @@ namespace ProjectRepairCompany.Forms
public partial class FormStorageDetail : Form
{
private readonly IStorageDetailRepository _storageDetailRepository;
private readonly IDetailRepository _detailRepository;
public FormStorageDetail(IStorageDetailRepository storageDetailRepository, IDetailRepository detailRepository, IStorageRepository storageRepository)
public FormStorageDetail(IStorageDetailRepository storageDetailRepository, IDetailRepository detailRepository)
{
InitializeComponent();
_storageDetailRepository = storageDetailRepository ?? throw new ArgumentNullException(nameof(storageDetailRepository));
comboBoxDetail.DataSource = detailRepository.ReadDetails();
comboBoxDetail.DisplayMember = "NameDetail";
comboBoxDetail.ValueMember = "Id";
comboBoxAddress.DataSource = storageRepository.ReadStorages();
comboBoxAddress.DisplayMember = "StorageAddress";
comboBoxAddress.ValueMember = "Id";
_detailRepository = detailRepository ?? throw new ArgumentNullException(nameof(detailRepository));
}
private void ButtonSave_Click(object sender, EventArgs e)
{
try
{
if (comboBoxDetail.SelectedIndex < 0 || comboBoxAddress.SelectedIndex < 0 || Convert.ToInt32(numericUpDown1.Value) == 0)
if (comboBoxDetail.SelectedIndex < 0 || comboBoxAddress.SelectedIndex < 0 || Convert.ToInt32(numericUpDownCount.Value) == 0)
{
throw new Exception("Заполни все поля");
}
_storageDetailRepository.CreateStorageDetail(StorageDetail.CreateOperation((int)comboBoxAddress.SelectedValue!, (int)comboBoxDetail.SelectedValue!, Convert.ToInt32(numericUpDown1.Value), dateTimePicker1.Value));
_storageDetailRepository.CreateStorageDetail(StorageDetail.CreateOperation((int)comboBoxAddress.SelectedValue!, (int)comboBoxDetail.SelectedValue!, Convert.ToInt32(numericUpDownCount.Value), dateTimePickerDate.Value));
Close();
}
catch (Exception ex)

View File

@ -34,9 +34,7 @@ namespace ProjectRepairCompany.Forms
{
try
{
var formStorageDetail = _container.Resolve<FormStorageDetail>();
formStorageDetail.ShowDialog();
_container.Resolve<FormStorageDetail>().ShowDialog();
LoadList();
}
catch (Exception ex)

View File

@ -151,23 +151,7 @@ public class OrderRepository : IOrderRepository
public Order ReadOrderById(int id)
{
_logger.LogInformation("Получение по id");
_logger.LogDebug("Объект: {id}", id);
try
{
using var connection = new NpgsqlConnection(_connectionString.ConnectionString);
var querySelect = @"
SELECT * FROM ""Order""
WHERE Id=@id";
var order = connection.QueryFirst<Order>(querySelect, new { id });
_logger.LogDebug("Найденный объект: {json}", JsonConvert.SerializeObject(order));
return order;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка поиска");
throw;
}
return Order.CreateOperation(0, 0, 0, DateTime.MinValue, DateTime.MinValue, Enumerable.Empty<OrderDetail>());
}