2024-05-02 20:33:46 +04:00
|
|
|
|
using BeautySalonDBModels.Models;
|
|
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
|
|
namespace BeautySalonDBModels.Implements
|
|
|
|
|
{
|
2024-05-08 01:25:07 +04:00
|
|
|
|
public class SpecialisationDatabase : AbstractWorkWithStorage<Specialisation>
|
2024-05-02 20:33:46 +04:00
|
|
|
|
{
|
|
|
|
|
public override void Add(Specialisation specialisation)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:17:20 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("INSERT INTO specialisations (specialisation_id, specialisation_name) VALUES (nextval('seq_specialisation'), @Name)", conn);
|
2024-05-02 20:33:46 +04:00
|
|
|
|
cmd.Parameters.AddWithValue("@Name", specialisation.Name);
|
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-07 21:17:20 +04:00
|
|
|
|
|
2024-05-02 20:33:46 +04:00
|
|
|
|
public override Specialisation? GetObject(int id)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:17:20 +04:00
|
|
|
|
using var cmd = new NpgsqlCommand("SELECT * FROM specialisations WHERE specialisation_id = @id", conn);
|
|
|
|
|
cmd.Parameters.AddWithValue("@id", id);
|
2024-05-02 20:33:46 +04:00
|
|
|
|
using var reader = cmd.ExecuteReader();
|
2024-05-07 21:17:20 +04:00
|
|
|
|
|
2024-05-02 20:33:46 +04:00
|
|
|
|
if (reader.Read())
|
|
|
|
|
{
|
|
|
|
|
return new Specialisation
|
|
|
|
|
{
|
|
|
|
|
SpecialisationId = reader.GetInt32(0),
|
|
|
|
|
Name = reader.GetString(1)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override List<Specialisation> GetObjects()
|
|
|
|
|
{
|
|
|
|
|
var specialisations = new List<Specialisation>();
|
|
|
|
|
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);
|
2024-05-07 21:17:20 +04:00
|
|
|
|
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();
|
|
|
|
|
}
|
2024-05-02 20:33:46 +04:00
|
|
|
|
return specialisation;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(Specialisation specialisation)
|
|
|
|
|
{
|
|
|
|
|
using var conn = GetConnection();
|
|
|
|
|
conn.Open();
|
2024-05-07 21:17:20 +04:00
|
|
|
|
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);
|
2024-05-02 20:33:46 +04:00
|
|
|
|
cmd.ExecuteNonQuery();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|