From c612091f470afff5036152917cc35552fa0ae266 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BB=D0=B5=D0=BD=D0=B0=20=D0=91=D0=B0=D0=BA=D0=B0?= =?UTF-8?q?=D0=BB=D1=8C=D1=81=D0=BA=D0=B0=D1=8F?= Date: Tue, 7 May 2024 21:17:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB?= =?UTF-8?q?=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=20=D1=80=D0=B5=D0=B0?= =?UTF-8?q?=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8=D1=8E=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=81=D0=BF=D0=B5=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D0=B8=20=D0=BD=D0=B0=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=BF?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=BE=D0=B2=20=D0=B2?= =?UTF-8?q?=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0=D1=85.=20=D0=B8?= =?UTF-8?q?=20=D1=84=D0=BE=D1=80=D0=BC=D1=8B=20=D0=B4=D0=BB=D1=8F=20=D1=81?= =?UTF-8?q?=D0=BF=D0=B5=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86?= =?UTF-8?q?=D0=B8=D0=B8=20=D0=B4=D0=BE=D0=BD=D0=B0=D1=81=D1=82=D1=80=D0=BE?= =?UTF-8?q?=D0=B8=D0=BB=D0=B0=20=D0=B2=D1=80=D0=BE=D0=B4=D0=B5...=20=D1=82?= =?UTF-8?q?=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=B0=D0=B9=D0=B4=D0=B8=D1=88?= =?UTF-8?q?=D0=BD=D0=B8=D0=BA=D0=B8=20=D0=BD=D0=B5=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D1=8B=D0=B2=D0=B0=D1=8E?= =?UTF-8?q?=D1=82=D1=81=D1=8F=20(=D1=83=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=204=20=D0=B0=D0=B9=D0=B4=D0=B8=D1=88=D0=BD=D0=B8=D0=BA?= =?UTF-8?q?=20-=20=D1=81=D0=BB=D0=B5=D0=B4=D1=83=D1=8E=D1=89=D0=B8=D0=BC?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=D0=B0=D1=81=D1=8C?= =?UTF-8?q?=20=D1=81=D0=BF=D0=B5=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D1=81=20=D0=B0=D0=B9=D0=B4=D0=B8=D1=88?= =?UTF-8?q?=D0=BD=D0=B8=D0=BA=D0=BE=D0=BC=205)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../FormSpecialisation.Designer.cs | 53 ++++++++++++- BeautySalon/BeautySalon/FormSpecialisation.cs | 74 ++++++++++++++++++- .../BeautySalon/FormSpecialisation.resx | 50 ++++++------- .../Implements/SpecialisationDB.cs | 26 ++++--- 4 files changed, 163 insertions(+), 40 deletions(-) diff --git a/BeautySalon/BeautySalon/FormSpecialisation.Designer.cs b/BeautySalon/BeautySalon/FormSpecialisation.Designer.cs index 7df883b..427304a 100644 --- a/BeautySalon/BeautySalon/FormSpecialisation.Designer.cs +++ b/BeautySalon/BeautySalon/FormSpecialisation.Designer.cs @@ -28,12 +28,57 @@ /// 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 = "FormSpecialisation"; + buttonSave = new Button(); + textBoxNameSpecialisation = new TextBox(); + buttonCancel = new Button(); + SuspendLayout(); + // + // buttonSave + // + buttonSave.Location = new Point(37, 86); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(133, 44); + buttonSave.TabIndex = 0; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // textBoxNameSpecialisation + // + textBoxNameSpecialisation.Location = new Point(37, 33); + textBoxNameSpecialisation.Name = "textBoxNameSpecialisation"; + textBoxNameSpecialisation.Size = new Size(274, 27); + textBoxNameSpecialisation.TabIndex = 1; + // + // buttonCancel + // + buttonCancel.Location = new Point(178, 86); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(133, 44); + buttonCancel.TabIndex = 2; + buttonCancel.Text = "Отмена"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // FormSpecialisation + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(345, 170); + Controls.Add(buttonCancel); + Controls.Add(textBoxNameSpecialisation); + Controls.Add(buttonSave); + Name = "FormSpecialisation"; + Text = "Специализация"; + Load += FormSpecialisation_Load; + ResumeLayout(false); + PerformLayout(); } #endregion + + private Button buttonSave; + private TextBox textBoxNameSpecialisation; + private Button buttonCancel; } } \ No newline at end of file diff --git a/BeautySalon/BeautySalon/FormSpecialisation.cs b/BeautySalon/BeautySalon/FormSpecialisation.cs index c83ea6c..44d0e8e 100644 --- a/BeautySalon/BeautySalon/FormSpecialisation.cs +++ b/BeautySalon/BeautySalon/FormSpecialisation.cs @@ -1,4 +1,6 @@ -using System; +using BeautySalonDBModels; +using BeautySalonDBModels.Models; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +14,77 @@ namespace BeautySalon { public partial class FormSpecialisation : Form { - public FormSpecialisation() + + private readonly AbstractWorkWithStorage storage; + + private int? _id; + public int Id { + set { _id = value; } + } + + public FormSpecialisation(AbstractWorkWithStorage storage) + { + this.storage = storage; InitializeComponent(); } + + private void buttonSave_Click(object sender, EventArgs e) + { + try + { + if (_id != null) + { + var newSpecialisation = new Specialisation + { + SpecialisationId = _id ?? 0, + Name = textBoxNameSpecialisation.Text, + }; + + storage.Update(newSpecialisation); + } + else + { + var specialisation = new Specialisation + { + Name = textBoxNameSpecialisation.Text, + }; + storage.Add(specialisation); + } + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + + } + + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; Close(); + } + + private void FormSpecialisation_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var specialisationObject = storage.GetObject(_id.Value); + + if (specialisationObject != null) + { + textBoxNameSpecialisation.Text = specialisationObject.Name; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } } } diff --git a/BeautySalon/BeautySalon/FormSpecialisation.resx b/BeautySalon/BeautySalon/FormSpecialisation.resx index 1af7de1..af32865 100644 --- a/BeautySalon/BeautySalon/FormSpecialisation.resx +++ b/BeautySalon/BeautySalon/FormSpecialisation.resx @@ -1,17 +1,17 @@  - diff --git a/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs b/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs index 6b7a82f..7e56d4f 100644 --- a/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs +++ b/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs @@ -14,18 +14,20 @@ namespace BeautySalonDBModels.Implements { using var conn = GetConnection(); conn.Open(); - using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_name) " + - "VALUES (@Name)", conn); + using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_id, specialisation_name) VALUES (nextval('seq_specialisation'), @Name)", conn); cmd.Parameters.AddWithValue("@Name", specialisation.Name); cmd.ExecuteNonQuery(); } + public override Specialisation? GetObject(int id) { using var conn = GetConnection(); conn.Open(); - using var cmd = new NpgsqlCommand("SELECT * FROM specialisations WHERE specialisation_id = {id}", conn); + using var cmd = new NpgsqlCommand("SELECT * FROM specialisations WHERE specialisation_id = @id", conn); + cmd.Parameters.AddWithValue("@id", id); using var reader = cmd.ExecuteReader(); + if (reader.Read()) { return new Specialisation @@ -58,10 +60,14 @@ namespace BeautySalonDBModels.Implements public override Specialisation? Remove(int id) { var specialisation = GetObject(id); - using var conn = GetConnection(); - conn.Open(); - using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = {id}", conn); - cmd.ExecuteNonQuery(); + if (specialisation != null) + { + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = @SpecialisationId", conn); + cmd.Parameters.AddWithValue("@SpecialisationId", id); + cmd.ExecuteNonQuery(); + } return specialisation; } @@ -69,9 +75,11 @@ namespace BeautySalonDBModels.Implements { using var conn = GetConnection(); conn.Open(); - using var cmd = new NpgsqlCommand("update specialisation set specialisation_name = {specialisation.Name}, " + - "where specialisation_id = {specialisation.SpecialisationId}", conn); + using var cmd = new NpgsqlCommand("UPDATE specialisations SET specialisation_name = @Name WHERE specialisation_id = @SpecialisationId", conn); + cmd.Parameters.AddWithValue("@Name", specialisation.Name); + cmd.Parameters.AddWithValue("@SpecialisationId", specialisation.SpecialisationId); cmd.ExecuteNonQuery(); } + } }