поменяла класс реализацию для специализации на использование параметров в запросах. и формы для специализации донастроила вроде... только айдишники не перезаписываются (удалила 4 айдишник - следующим добавилась специализация с айдишником 5)

This commit is contained in:
Елена Бакальская 2024-05-07 21:17:20 +04:00
parent 735d10a4d6
commit c612091f47
4 changed files with 163 additions and 40 deletions

View File

@ -28,12 +28,57 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.components = new System.ComponentModel.Container(); buttonSave = new Button();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; textBoxNameSpecialisation = new TextBox();
this.ClientSize = new System.Drawing.Size(800, 450); buttonCancel = new Button();
this.Text = "FormSpecialisation"; 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 #endregion
private Button buttonSave;
private TextBox textBoxNameSpecialisation;
private Button buttonCancel;
} }
} }

View File

@ -1,4 +1,6 @@
using System; using BeautySalonDBModels;
using BeautySalonDBModels.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -12,9 +14,77 @@ namespace BeautySalon
{ {
public partial class FormSpecialisation : Form public partial class FormSpecialisation : Form
{ {
public FormSpecialisation()
private readonly AbstractWorkWithStorage<Specialisation> storage;
private int? _id;
public int Id
{ {
set { _id = value; }
}
public FormSpecialisation(AbstractWorkWithStorage<Specialisation> storage)
{
this.storage = storage;
InitializeComponent(); 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);
}
}
}
} }
} }

View File

@ -14,18 +14,20 @@ namespace BeautySalonDBModels.Implements
{ {
using var conn = GetConnection(); using var conn = GetConnection();
conn.Open(); conn.Open();
using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_name) " + using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_id, specialisation_name) VALUES (nextval('seq_specialisation'), @Name)", conn);
"VALUES (@Name)", conn);
cmd.Parameters.AddWithValue("@Name", specialisation.Name); cmd.Parameters.AddWithValue("@Name", specialisation.Name);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
public override Specialisation? GetObject(int id) public override Specialisation? GetObject(int id)
{ {
using var conn = GetConnection(); using var conn = GetConnection();
conn.Open(); 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(); using var reader = cmd.ExecuteReader();
if (reader.Read()) if (reader.Read())
{ {
return new Specialisation return new Specialisation
@ -58,10 +60,14 @@ namespace BeautySalonDBModels.Implements
public override Specialisation? Remove(int id) public override Specialisation? Remove(int id)
{ {
var specialisation = GetObject(id); var specialisation = GetObject(id);
using var conn = GetConnection(); if (specialisation != null)
conn.Open(); {
using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = {id}", conn); using var conn = GetConnection();
cmd.ExecuteNonQuery(); conn.Open();
using var cmd = new NpgsqlCommand("delete from specialisations where specialisation_id = @SpecialisationId", conn);
cmd.Parameters.AddWithValue("@SpecialisationId", id);
cmd.ExecuteNonQuery();
}
return specialisation; return specialisation;
} }
@ -69,9 +75,11 @@ namespace BeautySalonDBModels.Implements
{ {
using var conn = GetConnection(); using var conn = GetConnection();
conn.Open(); conn.Open();
using var cmd = new NpgsqlCommand("update specialisation set specialisation_name = {specialisation.Name}, " + using var cmd = new NpgsqlCommand("UPDATE specialisations SET specialisation_name = @Name WHERE specialisation_id = @SpecialisationId", conn);
"where specialisation_id = {specialisation.SpecialisationId}", conn); cmd.Parameters.AddWithValue("@Name", specialisation.Name);
cmd.Parameters.AddWithValue("@SpecialisationId", specialisation.SpecialisationId);
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
} }
} }