From 1eef200c11db381a46683f4dacc3b3e1530297b8 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: Thu, 2 May 2024 20:33:46 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8E=20=D1=81=D0=BF=D0=B5=D1=86=D0=B8=D0=B0=D0=BB=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/SpecialisationDB.cs | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs diff --git a/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs b/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs new file mode 100644 index 0000000..6b7a82f --- /dev/null +++ b/BeautySalon/BeautySalonDBModels/Implements/SpecialisationDB.cs @@ -0,0 +1,77 @@ +using BeautySalonDBModels.Models; +using Npgsql; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BeautySalonDBModels.Implements +{ + public class SpecialisationDB : AbstractWorkWithStorage + { + public override void Add(Specialisation specialisation) + { + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_name) " + + "VALUES (@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 reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Specialisation + { + SpecialisationId = reader.GetInt32(0), + Name = reader.GetString(1) + }; + } + return null; + } + + public override List GetObjects() + { + var specialisations = new List(); + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM specialisations order by specialisation_id", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + specialisations.Add(new Specialisation + { + SpecialisationId = reader.GetInt32(0), + Name = reader.GetString(1) + }); + } + return specialisations; + } + + 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(); + return specialisation; + } + + public override void Update(Specialisation specialisation) + { + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("update specialisation set specialisation_name = {specialisation.Name}, " + + "where specialisation_id = {specialisation.SpecialisationId}", conn); + cmd.ExecuteNonQuery(); + } + } +}