From e2035800e3a081f66ce3312717e69302533c69ea 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 12:54:17 +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=81=D0=B5=D1=82=D1=8B=20=D0=B8=20=D0=BF=D0=BE=D1=87?= =?UTF-8?q?=D1=82=D0=B8=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BB=D0=B0=20=D1=85=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8?= =?UTF-8?q?=D1=89=D0=B5=20=D0=BA=D0=BB=D0=B8=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AbstractWorkWithStorage.cs | 20 ++++-- .../Implements/ClientDB.cs | 69 +++++++++++++++++++ .../BeautySalonDBModels/Models/Cheque.cs | 6 +- .../BeautySalonDBModels/Models/Client.cs | 6 +- .../BeautySalonDBModels/Models/Master.cs | 6 +- .../BeautySalonDBModels/Models/Reception.cs | 8 +-- .../BeautySalonDBModels/Models/Service.cs | 8 +-- .../Models/Specialisation.cs | 4 +- 8 files changed, 101 insertions(+), 26 deletions(-) create mode 100644 BeautySalon/BeautySalonDBModels/Implements/ClientDB.cs diff --git a/BeautySalon/BeautySalonDBModels/AbstractWorkWithStorage.cs b/BeautySalon/BeautySalonDBModels/AbstractWorkWithStorage.cs index d3856d7..21e9a2b 100644 --- a/BeautySalon/BeautySalonDBModels/AbstractWorkWithStorage.cs +++ b/BeautySalon/BeautySalonDBModels/AbstractWorkWithStorage.cs @@ -1,13 +1,19 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using BeautySalonDBModels.Models; +using Npgsql; namespace BeautySalonDBModels { - public abstract class AbstractWorkWithStorage + public abstract class AbstractWorkWithStorage { - public abstract void AddClient(Clien); + public abstract void Add(T obj); + public abstract T? Remove(int id); + public abstract void Update(T obj); + public abstract List GetObjects(); + public abstract T? GetObject(int id); + + public NpgsqlConnection GetConnection() + { + return new NpgsqlConnection("Host=localhost;Port=5555;Username=elina;Database=beauty_salon;Password=elina"); + } } } diff --git a/BeautySalon/BeautySalonDBModels/Implements/ClientDB.cs b/BeautySalon/BeautySalonDBModels/Implements/ClientDB.cs new file mode 100644 index 0000000..666e3f3 --- /dev/null +++ b/BeautySalon/BeautySalonDBModels/Implements/ClientDB.cs @@ -0,0 +1,69 @@ +using BeautySalonDBModels.Models; +using Npgsql; + +namespace BeautySalonDBModels.Implements +{ + public class ClientDB : AbstractWorkWithStorage + { + public override void Add(Client client) + { + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("INSERT INTO clients (fio, age) VALUES (@FIO, @Age)", conn); + cmd.Parameters.AddWithValue("@FIO", client.FIO); + cmd.Parameters.AddWithValue("@Age", client.Age); + cmd.ExecuteNonQuery(); + } + + public override Client? GetObject(int id) + { + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM clients WHERE client_id = {id}", conn); + using var reader = cmd.ExecuteReader(); + if (reader.Read()) + { + return new Client + { + ClientId = reader.GetInt32(0), + FIO = reader.GetString(1), + Age = reader.GetInt32(2) + }; + } + return null; + } + + public override List GetObjects() + { + var clients = new List(); + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("SELECT * FROM clients order by id", conn); + using var reader = cmd.ExecuteReader(); + while (reader.Read()) + { + clients.Add(new Client + { + ClientId = reader.GetInt32(0), + FIO = reader.GetString(1), + Age = reader.GetInt32(2) + }); + } + return clients; + } + + public override Client? Remove(int id) + { + var client = GetObject(id); + using var conn = GetConnection(); + conn.Open(); + using var cmd = new NpgsqlCommand("delete from clients where client_id = {id}", conn); + return client; + } + + public override void Update(Client client) + { + + } + } +} diff --git a/BeautySalon/BeautySalonDBModels/Models/Cheque.cs b/BeautySalon/BeautySalonDBModels/Models/Cheque.cs index 7c76c02..052dc98 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Cheque.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Cheque.cs @@ -2,8 +2,8 @@ { public class Cheque { - public int ChequeId { get; } - public int ClientId { get; } - public int ReceptionId { get; } + public int ChequeId { get; set; } + public int ClientId { get; set; } + public int ReceptionId { get; set; } } } diff --git a/BeautySalon/BeautySalonDBModels/Models/Client.cs b/BeautySalon/BeautySalonDBModels/Models/Client.cs index c7ec72a..9a9ef95 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Client.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Client.cs @@ -2,8 +2,8 @@ { public class Client { - public int ClientId { get; } - public string FIO { get; } = string.Empty; - public int Age { get; } + public int ClientId { get; set; } + public string FIO { get; set; } = string.Empty; + public int Age { get; set; } } } diff --git a/BeautySalon/BeautySalonDBModels/Models/Master.cs b/BeautySalon/BeautySalonDBModels/Models/Master.cs index c37652e..60b2087 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Master.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Master.cs @@ -2,8 +2,8 @@ { public class Master { - public int MasterId { get; } - public int SpecialisationId { get; } - public string FIO { get; } = string.Empty; + public int MasterId { get; set; } + public int SpecialisationId { get; set; } + public string FIO { get; set; } = string.Empty; } } diff --git a/BeautySalon/BeautySalonDBModels/Models/Reception.cs b/BeautySalon/BeautySalonDBModels/Models/Reception.cs index b1c5712..714c296 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Reception.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Reception.cs @@ -2,9 +2,9 @@ { public class Reception { - public int ReceptionId { get; } - public int MasterId { get; } - public int ServiceId { get; } - public DateTime DateReception { get; } + public int ReceptionId { get; set; } + public int MasterId { get; set; } + public int ServiceId { get; set; } + public DateTime DateReception { get; set; } } } diff --git a/BeautySalon/BeautySalonDBModels/Models/Service.cs b/BeautySalon/BeautySalonDBModels/Models/Service.cs index e324b69..81e8c3f 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Service.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Service.cs @@ -2,9 +2,9 @@ { public class Service { - public int ServiceId { get; } - public string ServiceName { get; } = string.Empty; - public double Price { get; } - public int SpecialisationId { get; } + public int ServiceId { get; set; } + public string ServiceName { get; set; } = string.Empty; + public double Price { get; set; } + public int SpecialisationId { get; set; } } } diff --git a/BeautySalon/BeautySalonDBModels/Models/Specialisation.cs b/BeautySalon/BeautySalonDBModels/Models/Specialisation.cs index 100f457..c8884fe 100644 --- a/BeautySalon/BeautySalonDBModels/Models/Specialisation.cs +++ b/BeautySalon/BeautySalonDBModels/Models/Specialisation.cs @@ -2,7 +2,7 @@ { public class Specialisation { - public int SpecialisationId { get; } - public string Name { get; } = string.Empty; + public int SpecialisationId { get; set; } + public string Name { get; set; } = string.Empty; } }