From d3bc885bc97ae483252b26865d3f7ac70baeca69 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Wed, 30 Oct 2024 13:34:23 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=BE=D0=B4=D1=83=D0=BF=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Laba3/MainForm.Designer.cs | 112 ++++++++++++++++ Laba3/MainForm.cs | 96 ++++++++++++++ Laba3/MainForm.resx | 123 ++++++++++++++++++ Laba3/ManufacturerForm.Designer.cs | 39 ++++++ Laba3/ManufacturerForm.cs | 67 ++++++++++ Laba3/ManufacturerForm.resx | 120 +++++++++++++++++ Laba3/ProductForm.Designer.cs | 69 ++++++++++ Laba3/ProductForm.cs | 96 ++++++++++++++ Laba3/ProductForm.resx | 120 +++++++++++++++++ Laba3/Program.cs | 17 +++ Laba3/View.csproj | 27 ++++ Laba3/appsettings.json | 5 + Library14Petrushin/Library14Petrushin.csproj | 1 + Library14Petrushin/Library14Petrushin.sln | 20 +-- Models/ApplicationDbContext.cs | 21 +++ Models/Data.csproj | 19 +++ .../20241029070533_Init.Designer.cs | 78 +++++++++++ Models/Migrations/20241029070533_Init.cs | 55 ++++++++ .../ApplicationDbContextModelSnapshot.cs | 75 +++++++++++ Models/Models/Manufacturer.cs | 19 +++ Models/Models/Product.cs | 30 +++++ .../Repositories/IManufacturerRepository.cs | 18 +++ Models/Repositories/IProductRepository.cs | 18 +++ Models/Repositories/ManufacturerRepository.cs | 51 ++++++++ Models/Repositories/ProductRepository.cs | 51 ++++++++ 25 files changed, 1337 insertions(+), 10 deletions(-) create mode 100644 Laba3/MainForm.Designer.cs create mode 100644 Laba3/MainForm.cs create mode 100644 Laba3/MainForm.resx create mode 100644 Laba3/ManufacturerForm.Designer.cs create mode 100644 Laba3/ManufacturerForm.cs create mode 100644 Laba3/ManufacturerForm.resx create mode 100644 Laba3/ProductForm.Designer.cs create mode 100644 Laba3/ProductForm.cs create mode 100644 Laba3/ProductForm.resx create mode 100644 Laba3/Program.cs create mode 100644 Laba3/View.csproj create mode 100644 Laba3/appsettings.json create mode 100644 Models/ApplicationDbContext.cs create mode 100644 Models/Data.csproj create mode 100644 Models/Migrations/20241029070533_Init.Designer.cs create mode 100644 Models/Migrations/20241029070533_Init.cs create mode 100644 Models/Migrations/ApplicationDbContextModelSnapshot.cs create mode 100644 Models/Models/Manufacturer.cs create mode 100644 Models/Models/Product.cs create mode 100644 Models/Repositories/IManufacturerRepository.cs create mode 100644 Models/Repositories/IProductRepository.cs create mode 100644 Models/Repositories/ManufacturerRepository.cs create mode 100644 Models/Repositories/ProductRepository.cs diff --git a/Laba3/MainForm.Designer.cs b/Laba3/MainForm.Designer.cs new file mode 100644 index 0000000..03c291d --- /dev/null +++ b/Laba3/MainForm.Designer.cs @@ -0,0 +1,112 @@ +namespace Laba3 +{ + partial class MainForm + { + /// + /// 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() + { + menuStrip1 = new MenuStrip(); + createToolStripMenuItem = new ToolStripMenuItem(); + updateToolStripMenuItem = new ToolStripMenuItem(); + outputTableResults = new Library15Gerimovich.OutputTableResults(); + deleteToolStripMenuItem = new ToolStripMenuItem(); + manufacturerToolStripMenuItem = new ToolStripMenuItem(); + menuStrip1.SuspendLayout(); + SuspendLayout(); + // + // menuStrip1 + // + menuStrip1.ImageScalingSize = new Size(20, 20); + menuStrip1.Items.AddRange(new ToolStripItem[] { createToolStripMenuItem, updateToolStripMenuItem, deleteToolStripMenuItem, manufacturerToolStripMenuItem }); + menuStrip1.Location = new Point(0, 0); + menuStrip1.Name = "menuStrip1"; + menuStrip1.Size = new Size(800, 28); + menuStrip1.TabIndex = 0; + menuStrip1.Text = "menuStrip1"; + // + // createToolStripMenuItem + // + createToolStripMenuItem.Name = "createToolStripMenuItem"; + createToolStripMenuItem.Size = new Size(66, 24); + createToolStripMenuItem.Text = "Create"; + createToolStripMenuItem.Click += addToolStripMenuItem_Click; + // + // updateToolStripMenuItem + // + updateToolStripMenuItem.Name = "updateToolStripMenuItem"; + updateToolStripMenuItem.Size = new Size(72, 24); + updateToolStripMenuItem.Text = "Update"; + updateToolStripMenuItem.Click += editToolStripMenuItem_Click; + // + // outputTableResults + // + outputTableResults.Location = new Point(12, 32); + outputTableResults.Margin = new Padding(3, 4, 3, 4); + outputTableResults.Name = "outputTableResults"; + outputTableResults.SelectedRow = -1; + outputTableResults.Size = new Size(462, 306); + outputTableResults.TabIndex = 1; + // + // deleteToolStripMenuItem + // + deleteToolStripMenuItem.Name = "deleteToolStripMenuItem"; + deleteToolStripMenuItem.Size = new Size(67, 24); + deleteToolStripMenuItem.Text = "Delete"; + deleteToolStripMenuItem.Click += deleteToolStripMenuItem_Click; + // + // manufacturerToolStripMenuItem + // + manufacturerToolStripMenuItem.Name = "manufacturerToolStripMenuItem"; + manufacturerToolStripMenuItem.Size = new Size(111, 24); + manufacturerToolStripMenuItem.Text = "Manufacturer"; + manufacturerToolStripMenuItem.Click += manufacturersToolStripMenuItem_Click; + // + // MainForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(outputTableResults); + Controls.Add(menuStrip1); + MainMenuStrip = menuStrip1; + Name = "MainForm"; + Text = "Form1"; + menuStrip1.ResumeLayout(false); + menuStrip1.PerformLayout(); + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private MenuStrip menuStrip1; + private ToolStripMenuItem createToolStripMenuItem; + private ToolStripMenuItem updateToolStripMenuItem; + private Library15Gerimovich.OutputTableResults outputTableResults; + private ToolStripMenuItem deleteToolStripMenuItem; + private ToolStripMenuItem manufacturerToolStripMenuItem; + } +} diff --git a/Laba3/MainForm.cs b/Laba3/MainForm.cs new file mode 100644 index 0000000..2fac4fa --- /dev/null +++ b/Laba3/MainForm.cs @@ -0,0 +1,96 @@ +using Data; +using Data.Repositories; +using View; +using System.Windows.Forms; +using Microsoft.Office.Interop.Excel; +using Data.Models; + +namespace Laba3 +{ + public partial class MainForm : Form + { + private readonly IProductRepository _productRepository; + private readonly IManufacturerRepository _manufacturerRepository; + + public MainForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository) + { + InitializeComponent(); + _productRepository = productRepository; + _manufacturerRepository = manufacturerRepository; + InitializeOutputTableResults(); + LoadProducts(); + } + + private void InitializeOutputTableResults() + { + outputTableResults.ConfigureColumns(new List + { + new Library15Gerimovich.ColumnInfo("", 0, false, "Id"), + new Library15Gerimovich.ColumnInfo("Name", 150, true, "Name"), + new Library15Gerimovich.ColumnInfo("ManufacturerNameManufacturerName", 150, true, "ManufacturerName"), + new Library15Gerimovich.ColumnInfo("DeliveryDate", 50, true, "DeliveryDate"), + }); + /*TestObject TestOB = new TestObject(1, "Ôàìèëèÿ", "èìÿ", 10); + TestObject TestOB2 = new TestObject(1, "Èâàíîâ", "Èâàí", 29); + + outputTableResults.InsertValue(TestOB); + outputTableResults.InsertValue(TestOB2);*/ + + } + + private void LoadProducts() + { + var products = _productRepository.GetAllProducts(); + outputTableResults.ClearGrid(); + foreach (var product in products) + { + outputTableResults.InsertValue(product); + } + } + + /*private void MainForm_Load(object sender, EventArgs e) + { + outputTableResults.ContextMenuStrip = contextMenuStrip; + }*/ + + private void addToolStripMenuItem_Click(object sender, EventArgs e) + { + var productForm = new ProductForm(_productRepository, _manufacturerRepository); + if (productForm.ShowDialog() == DialogResult.OK) + { + LoadProducts(); + } + } + + private void editToolStripMenuItem_Click(object sender, EventArgs e) + { + /*if (outputTableResults.SelectedItems.Count > 0) + {*/ + + var selectedProductId = outputTableResults.GetSelectedObject().Id; + var selectedProduct = _productRepository.GetProductById(selectedProductId); + var productForm = new ProductForm(_productRepository, _manufacturerRepository, selectedProduct); + if (productForm.ShowDialog() == DialogResult.OK) + { + LoadProducts(); + } + //} + } + + private void deleteToolStripMenuItem_Click(object sender, EventArgs e) + { + var selectedProductId = outputTableResults.GetSelectedObject().Id; + if (MessageBox.Show("Are you sure you want to delete this product?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) + { + _productRepository.DeleteProduct(selectedProductId); + LoadProducts(); + } + } + + private void manufacturersToolStripMenuItem_Click(object sender, EventArgs e) + { + var manufacturerForm = new ManufacturerForm(_manufacturerRepository); + manufacturerForm.ShowDialog(); + } + } +} diff --git a/Laba3/MainForm.resx b/Laba3/MainForm.resx new file mode 100644 index 0000000..a0623c8 --- /dev/null +++ b/Laba3/MainForm.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 17, 17 + + \ No newline at end of file diff --git a/Laba3/ManufacturerForm.Designer.cs b/Laba3/ManufacturerForm.Designer.cs new file mode 100644 index 0000000..8d7540d --- /dev/null +++ b/Laba3/ManufacturerForm.Designer.cs @@ -0,0 +1,39 @@ +namespace View +{ + partial class ManufacturerForm + { + /// + /// 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() + { + this.components = new System.ComponentModel.Container(); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Text = "ManufacturerForm"; + } + + #endregion + } +} \ No newline at end of file diff --git a/Laba3/ManufacturerForm.cs b/Laba3/ManufacturerForm.cs new file mode 100644 index 0000000..d3161f9 --- /dev/null +++ b/Laba3/ManufacturerForm.cs @@ -0,0 +1,67 @@ +using Data.Models; +using Data.Repositories; +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 View +{ + public partial class ManufacturerForm : Form + { + private readonly IManufacturerRepository _manufacturerRepository; + + public ManufacturerForm(IManufacturerRepository manufacturerRepository) + { + InitializeComponent(); + _manufacturerRepository = manufacturerRepository; + LoadManufacturers(); + } + + private void LoadManufacturers() + { + var manufacturers = _manufacturerRepository.GetAllManufacturers().ToList(); + dataGridView.DataSource = manufacturers; + } + + private void dataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e) + { + if (e.RowIndex >= 0) + { + var manufacturer = dataGridView.Rows[e.RowIndex].DataBoundItem as Manufacturer; + if (manufacturer != null) + { + _manufacturerRepository.UpdateManufacturer(manufacturer); + } + } + } + + private void dataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e) + { + var manufacturer = e.Row.DataBoundItem as Manufacturer; + if (manufacturer != null) + { + if (MessageBox.Show("Are you sure you want to delete this manufacturer?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) + { + _manufacturerRepository.DeleteManufacturer(manufacturer.Id); + } + else + { + e.Cancel = true; + } + } + } + + private void dataGridView_UserAddedRow(object sender, DataGridViewRowEventArgs e) + { + var manufacturer = new Manufacturer { Name = "" }; + _manufacturerRepository.AddManufacturer(manufacturer); + LoadManufacturers(); + } + } +} diff --git a/Laba3/ManufacturerForm.resx b/Laba3/ManufacturerForm.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/Laba3/ManufacturerForm.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/Laba3/ProductForm.Designer.cs b/Laba3/ProductForm.Designer.cs new file mode 100644 index 0000000..3e8c4cb --- /dev/null +++ b/Laba3/ProductForm.Designer.cs @@ -0,0 +1,69 @@ +namespace Laba3 +{ + partial class ProductForm + { + /// + /// 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() + { + dtpDeliveryDate = new WinFormsLibrary1.DateInputControl(); + comboBoxUserControl1 = new WinFormsLibrary1.ComboBoxUserControl(); + SuspendLayout(); + // + // dtpDeliveryDate + // + dtpDeliveryDate.Location = new Point(13, 65); + dtpDeliveryDate.Margin = new Padding(4, 5, 4, 5); + dtpDeliveryDate.Name = "dtpDeliveryDate"; + dtpDeliveryDate.Size = new Size(186, 110); + dtpDeliveryDate.TabIndex = 0; + // + // comboBoxUserControl1 + // + comboBoxUserControl1.Location = new Point(13, 115); + comboBoxUserControl1.Margin = new Padding(4, 5, 4, 5); + comboBoxUserControl1.Name = "comboBoxUserControl1"; + comboBoxUserControl1.SelectedValue = ""; + comboBoxUserControl1.Size = new Size(201, 40); + comboBoxUserControl1.TabIndex = 1; + // + // ProductForm + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 450); + Controls.Add(comboBoxUserControl1); + Controls.Add(dtpDeliveryDate); + Name = "ProductForm"; + Text = "ProductForm"; + ResumeLayout(false); + } + + #endregion + + private WinFormsLibrary1.DateInputControl dtpDeliveryDate; + private WinFormsLibrary1.ComboBoxUserControl comboBoxUserControl1; + } +} \ No newline at end of file diff --git a/Laba3/ProductForm.cs b/Laba3/ProductForm.cs new file mode 100644 index 0000000..8cfe96c --- /dev/null +++ b/Laba3/ProductForm.cs @@ -0,0 +1,96 @@ +using Data; +using Data.Models; +using Data.Repositories; +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 Laba3 +{ + public partial class ProductForm : Form + { + private readonly IProductRepository _productRepository; + private readonly IManufacturerRepository _manufacturerRepository; + private Product _product; + private bool _isNewProduct; + + public ProductForm(IProductRepository productRepository, IManufacturerRepository manufacturerRepository, Product product = null) + { + InitializeComponent(); + _productRepository = productRepository; + _manufacturerRepository = manufacturerRepository; + _product = product; + _isNewProduct = product == null; + + if (!_isNewProduct) + { + txtName.Text = _product.Name; + cmbManufacturer.SelectedItem = _product.ManufacturerName; + dtpDeliveryDate.Value = _product.DeliveryDate; + if (_product.Image != null) + { + using (var ms = new MemoryStream(_product.Image)) + { + pbImage.Image = Image.FromStream(ms); + } + } + } + } + + private void btnBrowse_Click(object sender, EventArgs e) + { + using (var openFileDialog = new OpenFileDialog()) + { + openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp"; + if (openFileDialog.ShowDialog() == DialogResult.OK) + { + pbImage.Image = Image.FromFile(openFileDialog.FileName); + } + } + } + + private void btnSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(txtName.Text)) + { + MessageBox.Show("Name is required."); + return; + } + + if (_isNewProduct) + { + _product = new Product(); + _productRepository.AddProduct(_product); + } + + _product.Name = txtName.Text; + _product.ManufacturerName = cmbManufacturer.SelectedItem.ToString(); + _product.DeliveryDate = dtpDeliveryDate.Value; + + if (pbImage.Image != null) + { + using (var ms = new MemoryStream()) + { + pbImage.Image.Save(ms, pbImage.Image.RawFormat); + _product.Image = ms.ToArray(); + } + } + + _productRepository.UpdateProduct(_product); + DialogResult = DialogResult.OK; + Close(); + } + + private void ProductForm_Load(object sender, EventArgs e) + { + var manufacturers = _manufacturerRepository.GetAllManufacturers().Select(m => m.Name).ToList(); + cmbManufacturer.DataSource = manufacturers; + } + } +} diff --git a/Laba3/ProductForm.resx b/Laba3/ProductForm.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/Laba3/ProductForm.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/Laba3/Program.cs b/Laba3/Program.cs new file mode 100644 index 0000000..f19d060 --- /dev/null +++ b/Laba3/Program.cs @@ -0,0 +1,17 @@ +namespace Laba3 +{ + internal static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + // To customize application configuration such as set high DPI settings or default font, + // see https://aka.ms/applicationconfiguration. + ApplicationConfiguration.Initialize(); + Application.Run(new MainForm()); + } + } +} \ No newline at end of file diff --git a/Laba3/View.csproj b/Laba3/View.csproj new file mode 100644 index 0000000..373f68d --- /dev/null +++ b/Laba3/View.csproj @@ -0,0 +1,27 @@ + + + + WinExe + net8.0-windows + enable + true + enable + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + \ No newline at end of file diff --git a/Laba3/appsettings.json b/Laba3/appsettings.json new file mode 100644 index 0000000..f809773 --- /dev/null +++ b/Laba3/appsettings.json @@ -0,0 +1,5 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Host=localhost;Port=5432;Database=kop;Username=postgres;Password=postgres" + } +} \ No newline at end of file diff --git a/Library14Petrushin/Library14Petrushin.csproj b/Library14Petrushin/Library14Petrushin.csproj index 7d73251..3d9f1e5 100644 --- a/Library14Petrushin/Library14Petrushin.csproj +++ b/Library14Petrushin/Library14Petrushin.csproj @@ -5,6 +5,7 @@ enable true enable + True diff --git a/Library14Petrushin/Library14Petrushin.sln b/Library14Petrushin/Library14Petrushin.sln index 4d67935..ad6a1c3 100644 --- a/Library14Petrushin/Library14Petrushin.sln +++ b/Library14Petrushin/Library14Petrushin.sln @@ -5,9 +5,9 @@ VisualStudioVersion = 17.10.35013.160 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Library14Petrushin", "Library14Petrushin.csproj", "{64A09BF4-7D97-49DE-8AB8-E9C23B75A62D}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ExecForm1", "..\ExecForm\ExecForm1.csproj", "{6C3A0E39-ED7B-41D8-9253-023B1D35C935}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "View", "..\Laba3\View.csproj", "{F342448F-F458-45A7-B335-EAD594D6B7A4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExexForm2", "..\ExexForm2\ExexForm2.csproj", "{80D961BA-24BC-4B3E-A578-BAA90C6EED01}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Data", "..\Models\Data.csproj", "{DC70937E-903F-4BAA-A774-80D01EC5B75A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -19,14 +19,14 @@ Global {64A09BF4-7D97-49DE-8AB8-E9C23B75A62D}.Debug|Any CPU.Build.0 = Debug|Any CPU {64A09BF4-7D97-49DE-8AB8-E9C23B75A62D}.Release|Any CPU.ActiveCfg = Release|Any CPU {64A09BF4-7D97-49DE-8AB8-E9C23B75A62D}.Release|Any CPU.Build.0 = Release|Any CPU - {6C3A0E39-ED7B-41D8-9253-023B1D35C935}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {6C3A0E39-ED7B-41D8-9253-023B1D35C935}.Debug|Any CPU.Build.0 = Debug|Any CPU - {6C3A0E39-ED7B-41D8-9253-023B1D35C935}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6C3A0E39-ED7B-41D8-9253-023B1D35C935}.Release|Any CPU.Build.0 = Release|Any CPU - {80D961BA-24BC-4B3E-A578-BAA90C6EED01}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {80D961BA-24BC-4B3E-A578-BAA90C6EED01}.Debug|Any CPU.Build.0 = Debug|Any CPU - {80D961BA-24BC-4B3E-A578-BAA90C6EED01}.Release|Any CPU.ActiveCfg = Release|Any CPU - {80D961BA-24BC-4B3E-A578-BAA90C6EED01}.Release|Any CPU.Build.0 = Release|Any CPU + {F342448F-F458-45A7-B335-EAD594D6B7A4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F342448F-F458-45A7-B335-EAD594D6B7A4}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F342448F-F458-45A7-B335-EAD594D6B7A4}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F342448F-F458-45A7-B335-EAD594D6B7A4}.Release|Any CPU.Build.0 = Release|Any CPU + {DC70937E-903F-4BAA-A774-80D01EC5B75A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC70937E-903F-4BAA-A774-80D01EC5B75A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC70937E-903F-4BAA-A774-80D01EC5B75A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC70937E-903F-4BAA-A774-80D01EC5B75A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Models/ApplicationDbContext.cs b/Models/ApplicationDbContext.cs new file mode 100644 index 0000000..5d004c4 --- /dev/null +++ b/Models/ApplicationDbContext.cs @@ -0,0 +1,21 @@ +using Data.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data +{ + public class ApplicationDbContext : DbContext + { + public DbSet Products { get; set; } + public DbSet Manufacturers { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=cop;Username=postgres;Password=postgres"); + } + } +} diff --git a/Models/Data.csproj b/Models/Data.csproj new file mode 100644 index 0000000..871c72b --- /dev/null +++ b/Models/Data.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + diff --git a/Models/Migrations/20241029070533_Init.Designer.cs b/Models/Migrations/20241029070533_Init.Designer.cs new file mode 100644 index 0000000..db25449 --- /dev/null +++ b/Models/Migrations/20241029070533_Init.Designer.cs @@ -0,0 +1,78 @@ +// +using System; +using Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20241029070533_Init")] + partial class Init + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Models.Manufacturer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("Manufacturers"); + }); + + modelBuilder.Entity("Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DeliveryDate") + .HasColumnType("date"); + + b.Property("Image") + .IsRequired() + .HasColumnType("bytea"); + + b.Property("ManufacturerName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Models/Migrations/20241029070533_Init.cs b/Models/Migrations/20241029070533_Init.cs new file mode 100644 index 0000000..b89dae2 --- /dev/null +++ b/Models/Migrations/20241029070533_Init.cs @@ -0,0 +1,55 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Data.Migrations +{ + /// + public partial class Init : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Manufacturers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Manufacturers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Products", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + Name = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), + Image = table.Column(type: "bytea", nullable: false), + ManufacturerName = table.Column(type: "character varying(100)", maxLength: 100, nullable: false), + DeliveryDate = table.Column(type: "date", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Products", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Manufacturers"); + + migrationBuilder.DropTable( + name: "Products"); + } + } +} diff --git a/Models/Migrations/ApplicationDbContextModelSnapshot.cs b/Models/Migrations/ApplicationDbContextModelSnapshot.cs new file mode 100644 index 0000000..c5aabce --- /dev/null +++ b/Models/Migrations/ApplicationDbContextModelSnapshot.cs @@ -0,0 +1,75 @@ +// +using System; +using Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace Data.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + partial class ApplicationDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Models.Manufacturer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("Manufacturers"); + }); + + modelBuilder.Entity("Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("DeliveryDate") + .HasColumnType("date"); + + b.Property("Image") + .IsRequired() + .HasColumnType("bytea"); + + b.Property("ManufacturerName") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(100) + .HasColumnType("character varying(100)"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Models/Models/Manufacturer.cs b/Models/Models/Manufacturer.cs new file mode 100644 index 0000000..7cb4827 --- /dev/null +++ b/Models/Models/Manufacturer.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Models +{ + public class Manufacturer + { + [Key] + public int Id { get; set; } + + [Required] + [MaxLength(100)] + public string Name { get; set; } + } +} diff --git a/Models/Models/Product.cs b/Models/Models/Product.cs new file mode 100644 index 0000000..ba06d1d --- /dev/null +++ b/Models/Models/Product.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Models +{ + public class Product + { + [Key] + public int Id { get; set; } + + [Required] + [MaxLength(100)] + public string Name { get; set; } + + public byte[] Image { get; set; } + + [Required] + [MaxLength(100)] + public string ManufacturerName { get; set; } + + [Required] + [Column(TypeName = "date")] + public DateTime DeliveryDate { get; set; } + } +} diff --git a/Models/Repositories/IManufacturerRepository.cs b/Models/Repositories/IManufacturerRepository.cs new file mode 100644 index 0000000..0dd4389 --- /dev/null +++ b/Models/Repositories/IManufacturerRepository.cs @@ -0,0 +1,18 @@ +using Data.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Repositories +{ + public interface IManufacturerRepository + { + IEnumerable GetAllManufacturers(); + Manufacturer GetManufacturerById(int id); + void AddManufacturer(Manufacturer manufacturer); + void UpdateManufacturer(Manufacturer manufacturer); + void DeleteManufacturer(int id); + } +} diff --git a/Models/Repositories/IProductRepository.cs b/Models/Repositories/IProductRepository.cs new file mode 100644 index 0000000..1fa4b8b --- /dev/null +++ b/Models/Repositories/IProductRepository.cs @@ -0,0 +1,18 @@ +using Data.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Repositories +{ + public interface IProductRepository + { + IEnumerable GetAllProducts(); + Product GetProductById(int id); + void AddProduct(Product product); + void UpdateProduct(Product product); + void DeleteProduct(int id); + } +} diff --git a/Models/Repositories/ManufacturerRepository.cs b/Models/Repositories/ManufacturerRepository.cs new file mode 100644 index 0000000..1271844 --- /dev/null +++ b/Models/Repositories/ManufacturerRepository.cs @@ -0,0 +1,51 @@ +using Data.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Repositories +{ + public class ManufacturerRepository : IManufacturerRepository + { + private readonly ApplicationDbContext _context; + + public ManufacturerRepository(ApplicationDbContext context) + { + _context = context; + } + + public IEnumerable GetAllManufacturers() + { + return _context.Manufacturers.ToList(); + } + + public Manufacturer GetManufacturerById(int id) + { + return _context.Manufacturers.Find(id); + } + + public void AddManufacturer(Manufacturer manufacturer) + { + _context.Manufacturers.Add(manufacturer); + _context.SaveChanges(); + } + + public void UpdateManufacturer(Manufacturer manufacturer) + { + _context.Manufacturers.Update(manufacturer); + _context.SaveChanges(); + } + + public void DeleteManufacturer(int id) + { + var manufacturer = _context.Manufacturers.Find(id); + if (manufacturer != null) + { + _context.Manufacturers.Remove(manufacturer); + _context.SaveChanges(); + } + } + } +} diff --git a/Models/Repositories/ProductRepository.cs b/Models/Repositories/ProductRepository.cs new file mode 100644 index 0000000..dc29903 --- /dev/null +++ b/Models/Repositories/ProductRepository.cs @@ -0,0 +1,51 @@ +using Data.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Data.Repositories +{ + public class ProductRepository : IProductRepository + { + private readonly ApplicationDbContext _context; + + public ProductRepository(ApplicationDbContext context) + { + _context = context; + } + + public IEnumerable GetAllProducts() + { + return _context.Products.ToList(); + } + + public Product GetProductById(int id) + { + return _context.Products.Find(id); + } + + public void AddProduct(Product product) + { + _context.Products.Add(product); + _context.SaveChanges(); + } + + public void UpdateProduct(Product product) + { + _context.Products.Update(product); + _context.SaveChanges(); + } + + public void DeleteProduct(int id) + { + var product = _context.Products.Find(id); + if (product != null) + { + _context.Products.Remove(product); + _context.SaveChanges(); + } + } + } +}