From b4487e7ae03e7ddd294c237d1f881c97de9e25fc Mon Sep 17 00:00:00 2001 From: Lazypr0ger Date: Thu, 28 Nov 2024 10:57:27 +0300 Subject: [PATCH] labs complited +- --- ProductionInCehOTP/Entities/Material.cs | 4 +- ProductionInCehOTP/Entities/Worker.cs | 10 +- .../FormArrivalMaterials.cs | 6 +- .../Material_Forms/FormMaterial.Designer.cs | 45 ++++--- .../Forms/Material_Forms/FormMaterial.cs | 9 +- .../Forms/Material_Forms/FormMaterials.cs | 11 -- .../Product_Forms/FormProduct.Designer.cs | 4 +- .../Forms/Product_Forms/FormProduct.cs | 11 +- .../Forms/Product_Forms/FormProducts.cs | 1 - .../Forms/Worker_Forms/FormWorker.Designer.cs | 4 +- .../Forms/Worker_Forms/FormWorker.cs | 70 ++++++----- ProductionInCehOTP/ProductionInCehOTP.csproj | 12 ++ ProductionInCehOTP/Program.cs | 28 +++-- .../Repositories/IWorkerRepository.cs | 4 +- .../ArrivalMaterialsRepository.cs | 116 +++++++++++++++-- .../Implementations/ConnectionString.cs | 9 +- .../Implementations/MaterialRepository.cs | 66 +++++++++- .../Implementations/PlanWorkRepository.cs | 89 ++++++++++++- .../Implementations/ProductRepository.cs | 117 ++++++++++++++++-- .../Implementations/WorkerRepository.cs | 115 +++++++++++++++-- 20 files changed, 590 insertions(+), 141 deletions(-) diff --git a/ProductionInCehOTP/Entities/Material.cs b/ProductionInCehOTP/Entities/Material.cs index 93b6e0b..a65b75f 100644 --- a/ProductionInCehOTP/Entities/Material.cs +++ b/ProductionInCehOTP/Entities/Material.cs @@ -7,7 +7,7 @@ public class Material { public int Id { get; private set; } public NameOfMaterials Name { get; private set; } - public int ArrivalMaterialsID { get; private set; } + public int ArrivalMaterialID { get; private set; } public IEnumerable MaterialForProducts { get; private set; } public static Material TransferMaterial(int id, NameOfMaterials name, int arrivalMaterialsID, IEnumerable materialForProducts ) { @@ -15,7 +15,7 @@ public static Material TransferMaterial(int id, NameOfMaterials name, int arriva { Id = id, Name = name, - ArrivalMaterialsID = arrivalMaterialsID, + ArrivalMaterialID = arrivalMaterialsID, MaterialForProducts = materialForProducts }; } diff --git a/ProductionInCehOTP/Entities/Worker.cs b/ProductionInCehOTP/Entities/Worker.cs index 3edec59..ef438e0 100644 --- a/ProductionInCehOTP/Entities/Worker.cs +++ b/ProductionInCehOTP/Entities/Worker.cs @@ -6,18 +6,18 @@ public class Worker { public int Id { get; private set; } public string Name { get; private set; } = string.Empty; - public DateTime Experiebce { get; private set; } - public int ClassOfWorker { get; private set; } + public int Experience { get; private set; } + public int Class { get; private set; } public int PlanWork { get; private set; } - public static Worker CreateWorker(int id, string name, DateTime experience, int classOfWorker, int planWorkID) + public static Worker CreateWorker(int id, string name, int experience, int classOfWorker, int planWorkID) { return new Worker { Id = id, Name = name, - Experiebce = experience, - ClassOfWorker = classOfWorker, + Experience = experience, + Class = classOfWorker, PlanWork = planWorkID }; } diff --git a/ProductionInCehOTP/Forms/ArrivalsMaterials_Forms/FormArrivalMaterials.cs b/ProductionInCehOTP/Forms/ArrivalsMaterials_Forms/FormArrivalMaterials.cs index 18fa41d..840f614 100644 --- a/ProductionInCehOTP/Forms/ArrivalsMaterials_Forms/FormArrivalMaterials.cs +++ b/ProductionInCehOTP/Forms/ArrivalsMaterials_Forms/FormArrivalMaterials.cs @@ -1,4 +1,5 @@ -using ProductionInCehOTP.Entities; +using Microsoft.Extensions.Logging; +using ProductionInCehOTP.Entities; using ProductionInCehOTP.Entities.Enums; using ProductionInCehOTP.Repositories; using ProductionInCehOTP.Repositories.Implementations; @@ -18,6 +19,7 @@ namespace ProductionInCehOTP.Forms { private readonly IArrivalMaterialsRepository _ArrivalMaterialsrepository; private int? _arrivalId; + public FormArrivalMaterials(IArrivalMaterialsRepository arrivalMaterialsRepository) { InitializeComponent(); @@ -27,8 +29,6 @@ namespace ProductionInCehOTP.Forms { checkedListBoxMaterials.Items.Add(elem); } - - } public int Id { diff --git a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.Designer.cs b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.Designer.cs index 927b566..29fc4bd 100644 --- a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.Designer.cs +++ b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.Designer.cs @@ -33,12 +33,11 @@ label1 = new Label(); label2 = new Label(); comboBoxNameOfMaterial = new ComboBox(); - numericUpDownArrivalNomber = new NumericUpDown(); dataGridViewMaterialToProduct = new DataGridView(); - groupBoxMaterialToProduct = new GroupBox(); ColumnProduct = new DataGridViewComboBoxColumn(); ColumnCountMaterial = new DataGridViewTextBoxColumn(); - ((System.ComponentModel.ISupportInitialize)numericUpDownArrivalNomber).BeginInit(); + groupBoxMaterialToProduct = new GroupBox(); + comboBoxNomberArrials = new ComboBox(); ((System.ComponentModel.ISupportInitialize)dataGridViewMaterialToProduct).BeginInit(); groupBoxMaterialToProduct.SuspendLayout(); SuspendLayout(); @@ -89,13 +88,6 @@ comboBoxNameOfMaterial.Size = new Size(220, 28); comboBoxNameOfMaterial.TabIndex = 5; // - // numericUpDownArrivalNomber - // - numericUpDownArrivalNomber.Location = new Point(220, 65); - numericUpDownArrivalNomber.Name = "numericUpDownArrivalNomber"; - numericUpDownArrivalNomber.Size = new Size(220, 27); - numericUpDownArrivalNomber.TabIndex = 6; - // // dataGridViewMaterialToProduct // dataGridViewMaterialToProduct.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; @@ -106,16 +98,6 @@ dataGridViewMaterialToProduct.Size = new Size(422, 282); dataGridViewMaterialToProduct.TabIndex = 7; // - // groupBoxMaterialToProduct - // - groupBoxMaterialToProduct.Controls.Add(dataGridViewMaterialToProduct); - groupBoxMaterialToProduct.Location = new Point(12, 98); - groupBoxMaterialToProduct.Name = "groupBoxMaterialToProduct"; - groupBoxMaterialToProduct.Size = new Size(434, 314); - groupBoxMaterialToProduct.TabIndex = 8; - groupBoxMaterialToProduct.TabStop = false; - groupBoxMaterialToProduct.Text = "Материалы для продукции"; - // // ColumnProduct // ColumnProduct.HeaderText = "Продукт"; @@ -132,13 +114,31 @@ ColumnCountMaterial.SortMode = DataGridViewColumnSortMode.NotSortable; ColumnCountMaterial.Width = 125; // + // groupBoxMaterialToProduct + // + groupBoxMaterialToProduct.Controls.Add(dataGridViewMaterialToProduct); + groupBoxMaterialToProduct.Location = new Point(12, 98); + groupBoxMaterialToProduct.Name = "groupBoxMaterialToProduct"; + groupBoxMaterialToProduct.Size = new Size(434, 314); + groupBoxMaterialToProduct.TabIndex = 8; + groupBoxMaterialToProduct.TabStop = false; + groupBoxMaterialToProduct.Text = "Материалы для продукции"; + // + // comboBoxNomberArrials + // + comboBoxNomberArrials.FormattingEnabled = true; + comboBoxNomberArrials.Location = new Point(219, 61); + comboBoxNomberArrials.Name = "comboBoxNomberArrials"; + comboBoxNomberArrials.Size = new Size(221, 28); + comboBoxNomberArrials.TabIndex = 9; + // // FormMaterial // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(458, 482); + Controls.Add(comboBoxNomberArrials); Controls.Add(groupBoxMaterialToProduct); - Controls.Add(numericUpDownArrivalNomber); Controls.Add(comboBoxNameOfMaterial); Controls.Add(label2); Controls.Add(label1); @@ -146,7 +146,6 @@ Controls.Add(buttonSave); Name = "FormMaterial"; Text = "Материал"; - ((System.ComponentModel.ISupportInitialize)numericUpDownArrivalNomber).EndInit(); ((System.ComponentModel.ISupportInitialize)dataGridViewMaterialToProduct).EndInit(); groupBoxMaterialToProduct.ResumeLayout(false); ResumeLayout(false); @@ -160,10 +159,10 @@ private Label label1; private Label label2; private ComboBox comboBoxNameOfMaterial; - private NumericUpDown numericUpDownArrivalNomber; private DataGridView dataGridViewMaterialToProduct; private GroupBox groupBoxMaterialToProduct; private DataGridViewComboBoxColumn ColumnProduct; private DataGridViewTextBoxColumn ColumnCountMaterial; + private ComboBox comboBoxNomberArrials; } } \ No newline at end of file diff --git a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs index 14be118..f830c5e 100644 --- a/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs +++ b/ProductionInCehOTP/Forms/Material_Forms/FormMaterial.cs @@ -19,7 +19,7 @@ namespace ProductionInCehOTP.Forms.Material_Forms private readonly IMaterialReposirory _materialRepository; private int? _materialID; - public FormMaterial(IMaterialReposirory materialRepository, IProductRepository productRepository) + public FormMaterial(IMaterialReposirory materialRepository, IProductRepository productRepository, IArrivalMaterialsRepository arrivalMaterialsRepository) { InitializeComponent(); _materialRepository = materialRepository ?? @@ -28,6 +28,11 @@ namespace ProductionInCehOTP.Forms.Material_Forms ColumnProduct.DataSource = productRepository.GetProducts(); ColumnProduct.DisplayMember = "Name"; ColumnProduct.ValueMember = "Id"; + + comboBoxNomberArrials.DataSource = arrivalMaterialsRepository.GetArrivalMaterials(); + comboBoxNomberArrials.DisplayMember = "Id"; + comboBoxNomberArrials.ValueMember = "Id"; + } private void ButtonSave_Click(object sender, EventArgs e) { @@ -39,7 +44,7 @@ namespace ProductionInCehOTP.Forms.Material_Forms throw new Exception("Имеются незаполненные поля"); } _materialRepository.TransferMaterial(Material.TransferMaterial(0, (NameOfMaterials)comboBoxNameOfMaterial.SelectedItem!, - Convert.ToInt32(numericUpDownArrivalNomber.Value), CreateListMateriaToProductForGrid())); + (int)comboBoxNomberArrials.SelectedValue!, CreateListMateriaToProductForGrid())); Close(); } catch (Exception ex) diff --git a/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs b/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs index af42bcf..d8c2ce6 100644 --- a/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs +++ b/ProductionInCehOTP/Forms/Material_Forms/FormMaterials.cs @@ -1,15 +1,4 @@ using ProductionInCehOTP.Repositories; -using ProductionInCehOTP.Repositories.Implementations; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using Unity; namespace ProductionInCehOTP.Forms.Material_Forms diff --git a/ProductionInCehOTP/Forms/Product_Forms/FormProduct.Designer.cs b/ProductionInCehOTP/Forms/Product_Forms/FormProduct.Designer.cs index ccd97a5..ad738ba 100644 --- a/ProductionInCehOTP/Forms/Product_Forms/FormProduct.Designer.cs +++ b/ProductionInCehOTP/Forms/Product_Forms/FormProduct.Designer.cs @@ -67,7 +67,7 @@ buttonSave.TabIndex = 10; buttonSave.Text = "Сохранить"; buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + buttonSave.Click += ButtonSave_Click; // // buttonCensel // @@ -78,7 +78,7 @@ buttonCensel.TabIndex = 11; buttonCensel.Text = "Отмена"; buttonCensel.UseVisualStyleBackColor = true; - buttonCensel.Click += buttonCensel_Click; + buttonCensel.Click += ButtonCensel_Click; // // numericUpDownPrice // diff --git a/ProductionInCehOTP/Forms/Product_Forms/FormProduct.cs b/ProductionInCehOTP/Forms/Product_Forms/FormProduct.cs index 740655a..5931314 100644 --- a/ProductionInCehOTP/Forms/Product_Forms/FormProduct.cs +++ b/ProductionInCehOTP/Forms/Product_Forms/FormProduct.cs @@ -17,8 +17,6 @@ namespace ProductionInCehOTP.Forms public partial class FormProduct : Form { - Random rnd = new Random(); - int RND; private readonly IProductRepository _productRepository; private int? _productID; @@ -30,7 +28,7 @@ namespace ProductionInCehOTP.Forms try { var product = _productRepository.GetProductById(value); - if (product != null) + if (product == null) { throw new InvalidDataException(nameof(product)); } @@ -39,7 +37,6 @@ namespace ProductionInCehOTP.Forms comboBoxProductType.SelectedItem = product.ProductionType; _productID = value; - } catch (Exception ex) { @@ -57,7 +54,7 @@ namespace ProductionInCehOTP.Forms comboBoxProductType.DataSource = Enum.GetValues(typeof(NameOfProductTypes)); } - private void buttonSave_Click(object sender, EventArgs e) + private void ButtonSave_Click(object sender, EventArgs e) { try { @@ -84,9 +81,9 @@ namespace ProductionInCehOTP.Forms } } - private Product CreateProducts(int id) => Product.CreateProducts(id, textBoxNameOfProduct.Text,Convert.ToInt32(numericUpDownPrice.Value), rnd.Next(0, 100000), (NameOfProductTypes)comboBoxProductType.SelectedItem!); + private Product CreateProducts(int id) => Product.CreateProducts(id, textBoxNameOfProduct.Text,Convert.ToInt32(numericUpDownPrice.Value), int.Parse(textBoxOficialID.Text), (NameOfProductTypes)comboBoxProductType.SelectedItem!); - private void buttonCensel_Click(object sender, EventArgs e) => Close(); + private void ButtonCensel_Click(object sender, EventArgs e) => Close(); diff --git a/ProductionInCehOTP/Forms/Product_Forms/FormProducts.cs b/ProductionInCehOTP/Forms/Product_Forms/FormProducts.cs index 2e509a2..6190d40 100644 --- a/ProductionInCehOTP/Forms/Product_Forms/FormProducts.cs +++ b/ProductionInCehOTP/Forms/Product_Forms/FormProducts.cs @@ -97,7 +97,6 @@ namespace ProductionInCehOTP.Forms private bool TryGetIdentifierFromSelectedRow(out int id) { - id = 0; if (dataGridViewData.SelectedRows.Count < 0) { MessageBox.Show("Нет выбранной записи", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); diff --git a/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.Designer.cs b/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.Designer.cs index 7b4b225..9558a47 100644 --- a/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.Designer.cs +++ b/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.Designer.cs @@ -153,7 +153,7 @@ buttonSave.TabIndex = 13; buttonSave.Text = "Сохранить"; buttonSave.UseVisualStyleBackColor = true; - buttonSave.Click += buttonSave_Click; + buttonSave.Click += ButtonSave_Click; // // buttonCensel // @@ -163,7 +163,7 @@ buttonCensel.TabIndex = 14; buttonCensel.Text = "Отмена"; buttonCensel.UseVisualStyleBackColor = true; - buttonCensel.Click += buttonCensel_Click; + buttonCensel.Click += ButtonCensel_Click; // // FormWorker // diff --git a/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.cs b/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.cs index 1724508..d736337 100644 --- a/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.cs +++ b/ProductionInCehOTP/Forms/Worker_Forms/FormWorker.cs @@ -21,10 +21,40 @@ namespace ProductionInCehOTP.Forms { InitializeComponent(); _workerRepository = workerRepository ?? - throw new ArgumentNullException(nameof(workerRepository)); + throw new ArgumentNullException(nameof(workerRepository)); } - private void buttonSave_Click(object sender, EventArgs e) + public int WorkerID + { + set + { + try + { + var worker = _workerRepository.GetWorkerById(value); + if (worker == null) + { + throw new InvalidDataException(nameof(worker)); + } + string firstname = textBoxFirstName.Text; + string lastname = textBoxLastName.Text; + string Secondname = textBoxSecondName.Text; + string name = String.Concat(firstname, " ", Secondname, " ", lastname); + name = worker.Name; + int ExperienceYear = DateTime.Now.Year - monthCalendarExp.SelectionStart.Year; + ExperienceYear = worker.Experience; + numericUpDownClassOfWorker.Value = worker.Class; + numericUpDownPlan.Value = worker.PlanWork; + + _workerID = value; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + } + } + private void ButtonSave_Click(object sender, EventArgs e) { try { @@ -39,43 +69,19 @@ namespace ProductionInCehOTP.Forms else { _workerRepository.CreateWorker(CreateWorker(0)); - } + } + + Close(); } catch(Exception ex) { MessageBox.Show(ex.Message, "Ошибка сохранения", MessageBoxButtons.OK, MessageBoxIcon.Error); } + } - private void buttonCensel_Click(object sender, EventArgs e) => Close(); - public int WorkerID - { - set - { - try - { - var worker = _workerRepository.GetWorkerById(value); - if (worker != null) - { - throw new InvalidDataException(nameof(worker)); - } - string firstname = textBoxFirstName.Text; - string lastname = textBoxLastName.Text; - string Secondname = textBoxSecondName.Text; - string name = String.Concat(firstname," ",Secondname," ",lastname ); - name = worker.Name; - monthCalendarExp.DataContext = worker.Experiebce; - numericUpDownClassOfWorker.Value = worker.ClassOfWorker; - numericUpDownPlan.Value = worker.PlanWork; - } - catch (Exception ex) - { - MessageBox.Show(ex.Message, "Ошибка при получении данных", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; - } - } - } + private void ButtonCensel_Click(object sender, EventArgs e) => Close(); private Worker CreateWorker(int workerID) => - Worker.CreateWorker(workerID, Name, Convert.ToDateTime(monthCalendarExp.DataContext), Convert.ToInt32(numericUpDownClassOfWorker.Value), Convert.ToInt32(numericUpDownPlan.Value)); + Worker.CreateWorker(workerID, String.Concat(textBoxSecondName.Text, " ", textBoxFirstName.Text," ", textBoxLastName.Text), DateTime.Now.Year - monthCalendarExp.SelectionStart.Year, Convert.ToInt32(numericUpDownClassOfWorker.Value), Convert.ToInt32(numericUpDownPlan.Value)); } } diff --git a/ProductionInCehOTP/ProductionInCehOTP.csproj b/ProductionInCehOTP/ProductionInCehOTP.csproj index 5e3387e..9e0f15b 100644 --- a/ProductionInCehOTP/ProductionInCehOTP.csproj +++ b/ProductionInCehOTP/ProductionInCehOTP.csproj @@ -9,11 +9,17 @@ + + + + + + @@ -33,4 +39,10 @@ + + + PreserveNewest + + + \ No newline at end of file diff --git a/ProductionInCehOTP/Program.cs b/ProductionInCehOTP/Program.cs index dbcb3e4..adb7a0f 100644 --- a/ProductionInCehOTP/Program.cs +++ b/ProductionInCehOTP/Program.cs @@ -1,3 +1,4 @@ +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using ProductionInCehOTP.Repositories; using ProductionInCehOTP.Repositories.Implementations; @@ -24,27 +25,28 @@ namespace ProductionInCehOTP private static IUnityContainer CreateContainer() { var container = new UnityContainer(); - //container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.AddExtension(new LoggingExtension(CreateLoggerFactory())); + container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); container.RegisterType(); - container.RegisterType(); + return container; } - //private static LoggerFactory CreateLoggerFactory() - //{ - // var loggerFactory = new LoggerFactory(); - // loggerFactory.AddSerilog(new LoggerConfiguration()) - // .ReadForm.Configuration(new ConfigurationBuilder()) - // .SetBasePath(Directory.GetCurrentDirectory()) - // .AddJsonFile("appsettings.json") - // .Build()) - // .CreateLogger()); - // return loggerFactory; + private static LoggerFactory CreateLoggerFactory() + { + var loggerFactory = new LoggerFactory(); + loggerFactory.AddSerilog(new LoggerConfiguration() + .ReadFrom.Configuration(new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json") + .Build()) + .CreateLogger()); + return loggerFactory; + } - //} } } \ No newline at end of file diff --git a/ProductionInCehOTP/Repositories/IWorkerRepository.cs b/ProductionInCehOTP/Repositories/IWorkerRepository.cs index c64bceb..2b6fc2c 100644 --- a/ProductionInCehOTP/Repositories/IWorkerRepository.cs +++ b/ProductionInCehOTP/Repositories/IWorkerRepository.cs @@ -5,10 +5,10 @@ namespace ProductionInCehOTP.Repositories; public interface IWorkerRepository { IEnumerable GetWorkers(); - Worker GetWorkerById(int id); + Worker GetWorkerById(int WorkerID); void CreateWorker (Worker worker); void UpdateWorker (Worker worker); - void DeleteWorker (int id); + void DeleteWorker (int WorkerID); } diff --git a/ProductionInCehOTP/Repositories/Implementations/ArrivalMaterialsRepository.cs b/ProductionInCehOTP/Repositories/Implementations/ArrivalMaterialsRepository.cs index f28d943..dbd4fa3 100644 --- a/ProductionInCehOTP/Repositories/Implementations/ArrivalMaterialsRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/ArrivalMaterialsRepository.cs @@ -1,37 +1,139 @@ -using ProductionInCehOTP.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProductionInCehOTP.Entities; using ProductionInCehOTP.Entities.Enums; +using System.Data.SqlClient; +using System.Data.SqlTypes; +using System.Net.Http.Json; +using System.Text.Json.Serialization; namespace ProductionInCehOTP.Repositories.Implementations; public class ArrivalMaterialsRepository : IArrivalMaterialsRepository { private readonly IConnectionString _connectionString; - public ArrivalMaterialsRepository(IConnectionString connectionString) + + private readonly ILogger _logger; + public ArrivalMaterialsRepository(IConnectionString connectionString , ILogger logger) { _connectionString = connectionString; + _logger = logger; } + public void CreateArrivalMaterials(ArrivalMaterials material) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Оъект: {json}", JsonConvert.SerializeObject(material)); + try + { + using var connecntion = new NpgsqlConnection(_connectionString.ConnectionString); + connecntion.Open(); + var queryInsert = @" +INSERT INTO ARRIVAL_MATERIALS (DATE, NAME, COUNT) +VALUES (@DATE, @NAME, @COUNT)"; + connecntion.Execute(queryInsert, material); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeleteArrivalMaterials(int id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM ARRIVAL_MATERIALS +WHERE Id=@id"; + connection.Execute(queryDelete, new { id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } public IEnumerable GetArrivalMaterials() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM ARRIVAL_MATERIALS"; + var materials = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(materials)); + return materials; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } public ArrivalMaterials GetArrivalMaterialsById(int id) { - return ArrivalMaterials.CreateArrivalMaterials(0, DateTime.Now, NameOfMaterials.None, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM ARRIVAL_MATERIALS +WHERE Id=@id"; + var material = connection.QueryFirst(querySelect, new + { + id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(material)); + return material; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } + } public void UpdateArrivalMaterials(ArrivalMaterials material) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(material)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE ARRIVAL_MATERIALS +SET + ID=@ID, + DATE=@DATE, + NAME=@NAME, + COUNT=@COUNT +WHERE Id=@Id"; + connection.Execute(queryUpdate, material); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } + } } diff --git a/ProductionInCehOTP/Repositories/Implementations/ConnectionString.cs b/ProductionInCehOTP/Repositories/Implementations/ConnectionString.cs index 1c2ce75..46db2bc 100644 --- a/ProductionInCehOTP/Repositories/Implementations/ConnectionString.cs +++ b/ProductionInCehOTP/Repositories/Implementations/ConnectionString.cs @@ -4,10 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace ProductionInCehOTP.Repositories.Implementations +namespace ProductionInCehOTP.Repositories.Implementations; + +public class ConnectionString : IConnectionString { - public class ConnectionString : IConnectionString - { - string IConnectionString.ConnectionString => ""; - } + string IConnectionString.ConnectionString => "Host=127.0.0.1;Port=5432;Username=postgres;Password=daa200513dr;Database=labsto_otp"; } diff --git a/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs b/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs index d717056..284b51c 100644 --- a/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/MaterialRepository.cs @@ -1,11 +1,22 @@ -using ProductionInCehOTP.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProductionInCehOTP.Entities; using ProductionInCehOTP.Entities.Enums; namespace ProductionInCehOTP.Repositories.Implementations; public class MaterialRepository : IMaterialReposirory { + private readonly IConnectionString _connectionString; + private readonly ILogger _logger; + public MaterialRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public Material GetMaterialById(int id) { throw new NotImplementedException(); @@ -13,11 +24,60 @@ public class MaterialRepository : IMaterialReposirory public IEnumerable GetMaterials(NameOfMaterials? name, int? arrivalMaterialsid) { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM MATERIAL"; + var material = + connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(material)); + return material; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + } public void TransferMaterial(Material material) { - throw new NotImplementedException(); + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(material)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO MATERIAL (NAME,ARRIVALMATERIALID) +VALUES (@NAME, @ARRIVALMATERIALID); +SELECT MAX(Id) FROM MATERIAL"; + var materialID = + connection.QueryFirst(queryInsert, material, transaction); + var querySubInsert = @" +INSERT INTO MATERIAL_PRODUCT (PRODUCTID, MATERIALID, COUNT) +VALUES (@PRODUCTID,@MATERIALID, @COUNT)"; + foreach (var elem in material.MaterialForProducts) + { + connection.Execute(querySubInsert, new + { + material, + elem.ProductID, + elem.MaterialID, + elem.Count + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } } diff --git a/ProductionInCehOTP/Repositories/Implementations/PlanWorkRepository.cs b/ProductionInCehOTP/Repositories/Implementations/PlanWorkRepository.cs index 2138635..4214038 100644 --- a/ProductionInCehOTP/Repositories/Implementations/PlanWorkRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/PlanWorkRepository.cs @@ -1,17 +1,79 @@ -using ProductionInCehOTP.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProductionInCehOTP.Entities; +using System.Data.SqlClient; namespace ProductionInCehOTP.Repositories.Implementations; public class PlanWorkRepository : IPlanWorkRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + public PlanWorkRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreatePlanWork(PlanWork planWork) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(planWork)); + try + { + using var connection = new NpgsqlConnection(_connectionString.ConnectionString); + connection.Open(); + using var transaction = connection.BeginTransaction(); + var queryInsert = @" +INSERT INTO PLANWORK (Plan, Date,WorkerID) +VALUES (@Plan, @Date, @WorkerID); +SELECT MAX(Id) FROM PLANWORK"; + var PlanWorkID = + connection.QueryFirst(queryInsert, planWork, transaction); + var querySubInsert = @" +INSERT INTO PLANWORK_PRODUCT (PLANWORKID, PRODUCTID, COUNT) +VALUES (@PLANWORKID,@PRODUCTID, @COUNT)"; + foreach (var elem in planWork.PlanWorkForProducts) + { + connection.Execute(querySubInsert, new + { + planWork, + elem.PlanWorkId, + elem.ProductId, + elem.Count + }, transaction); + } + transaction.Commit(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } public void DeletePlanWork(int planId) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", planId); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM PlanWork +WHERE Id=@id"; + connection.Execute(queryDelete, new { planId }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } + } public PlanWork GetPlanWorkById(int planId) @@ -20,7 +82,24 @@ public class PlanWorkRepository : IPlanWorkRepository } public IEnumerable GetPlanWorks() { - return []; - + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @"SELECT * FROM PlanWork"; + var planwork = + connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(planwork)); + return planwork; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } + + } } diff --git a/ProductionInCehOTP/Repositories/Implementations/ProductRepository.cs b/ProductionInCehOTP/Repositories/Implementations/ProductRepository.cs index 58bd217..cf4a8c1 100644 --- a/ProductionInCehOTP/Repositories/Implementations/ProductRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/ProductRepository.cs @@ -1,31 +1,132 @@ -using ProductionInCehOTP.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProductionInCehOTP.Entities; namespace ProductionInCehOTP.Repositories.Implementations; public class ProductRepository : IProductRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public ProductRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateProduct(Product product) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Оъект: {json}", JsonConvert.SerializeObject(product)); + try + { + using var connecntion = new NpgsqlConnection(_connectionString.ConnectionString); + connecntion.Open(); + var queryInsert = @" +INSERT INTO PRODUCT (NAME,PRICE,PRODUCTIONID, PRODUCTIONTYPEID) +VALUES (@NAME, @PRICE, @PRODUCTIONID, @PRODUCTIONTYPE)"; + connecntion.Execute(queryInsert, product); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteProduct(int productId) + public void DeleteProduct(int Id) { - + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", Id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM PRODUCT +WHERE Id=@id"; + connection.Execute(queryDelete, new { Id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public Product GetProductById(int productId) + public Product GetProductById(int Id) { - return Product.CreateProducts(0, "", 0, 0, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", Id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM PRODUCT +WHERE Id=@id"; + var product = connection.QueryFirst(querySelect, new + { + Id + }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(product)); + return product; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable GetProducts() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM PRODUCT"; + var product = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(product)); + return product; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateProduct(Product product) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(product)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE PRODUCT +SET + ID=@Id, + NAME=@Name, + PRICE=@Price, + PRODUCTIONID=@ProductionId, + PRODUCTIONTYPEDID=@ProductionType +WHERE Id=@Id"; + connection.Execute(queryUpdate, product); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } diff --git a/ProductionInCehOTP/Repositories/Implementations/WorkerRepository.cs b/ProductionInCehOTP/Repositories/Implementations/WorkerRepository.cs index 948bd4c..8675578 100644 --- a/ProductionInCehOTP/Repositories/Implementations/WorkerRepository.cs +++ b/ProductionInCehOTP/Repositories/Implementations/WorkerRepository.cs @@ -1,31 +1,130 @@ -using ProductionInCehOTP.Entities; +using Dapper; +using Microsoft.Extensions.Logging; +using Newtonsoft.Json; +using Npgsql; +using ProductionInCehOTP.Entities; namespace ProductionInCehOTP.Repositories.Implementations; public class WorkerRepository : IWorkerRepository { + private readonly IConnectionString _connectionString; + + private readonly ILogger _logger; + + public WorkerRepository(IConnectionString connectionString, ILogger logger) + { + _connectionString = connectionString; + _logger = logger; + } public void CreateWorker(Worker worker) { - + _logger.LogInformation("Добавление объекта"); + _logger.LogDebug("Оъект: {json}", JsonConvert.SerializeObject(worker)); + try + { + using var connecntion = new NpgsqlConnection(_connectionString.ConnectionString); + connecntion.Open(); + var queryInsert = @" +INSERT INTO WORKER (NAME,EXPERIENCE, CLASS, PLANWORK) +VALUES (@NAME, @EXPERIENCE, @CLASS, @PLANWORK)"; + connecntion.Execute(queryInsert, worker); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при добавлении объекта"); + throw; + } } - public void DeleteWorker(int id) + public void DeleteWorker(int Id) { - throw new NotImplementedException(); + _logger.LogInformation("Удаление объекта"); + _logger.LogDebug("Объект: {id}", Id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryDelete = @" +DELETE FROM WORKER +WHERE ID=@Id"; + connection.Execute(queryDelete, new { Id }); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при удалении объекта"); + throw; + } } - public Worker GetWorkerById(int id) + public Worker GetWorkerById(int Id) { - return Worker.CreateWorker(0, string.Empty, DateTime.Now, 0, 0); + _logger.LogInformation("Получение объекта по идентификатору"); + _logger.LogDebug("Объект: {id}", Id); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = @" +SELECT * FROM WORKER +WHERE ID=@Id"; + var worker = connection.QueryFirstOrDefault(querySelect, new { id = Id }); + _logger.LogDebug("Найденный объект: {json}", + JsonConvert.SerializeObject(worker)); + return worker; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при поиске объекта"); + throw; + } } public IEnumerable GetWorkers() { - return []; + _logger.LogInformation("Получение всех объектов"); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var querySelect = "SELECT * FROM WORKER"; + var worker = connection.Query(querySelect); + _logger.LogDebug("Полученные объекты: {json}", + JsonConvert.SerializeObject(worker)); + return worker; + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при чтении объектов"); + throw; + } } public void UpdateWorker(Worker worker) { - + _logger.LogInformation("Редактирование объекта"); + _logger.LogDebug("Объект: {json}", + JsonConvert.SerializeObject(worker)); + try + { + using var connection = new + NpgsqlConnection(_connectionString.ConnectionString); + var queryUpdate = @" +UPDATE WORKER +SET + ID=@Id + NAME=@Name, + EXPERIENCE=@Experience, + CLASS=@Class, + PLANWORK=@PlanWork +WHERE ID=@Id"; + connection.Execute(queryUpdate, worker); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка при редактировании объекта"); + throw; + } } } +