using MedicalDatabaseContracts.Models; using Microsoft.Extensions.Logging; using Npgsql; using System.Diagnostics; namespace MedicalPostgresqlDatabase.Storages { public class DiagnosesStorage : AbstractPostgresqlStorage { private ILogger _logger; public DiagnosesStorage(ILogger logger) { _logger = logger; } public override void Delete(int id, out long elapsedMilliseconds) { using var connection = GetConnection(); connection.Open(); using var cmd = new NpgsqlCommand("DELETE FROM diagnoses WHERE diagnose_id = @id", connection); cmd.Parameters.AddWithValue("@id", id); Stopwatch stopwatch = new(); stopwatch.Start(); cmd.ExecuteNonQuery(); stopwatch.Stop(); elapsedMilliseconds = stopwatch.ElapsedMilliseconds; } public override Diagnose? Get(int id, out long elapsedMilliseconds) { using var connection = GetConnection(); connection.Open(); using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses WHERE diagnose_id = @id", connection); cmd.Parameters.AddWithValue("@id", id); Stopwatch stopwatch = new(); stopwatch.Start(); using var reader = cmd.ExecuteReader(); stopwatch.Stop(); elapsedMilliseconds = stopwatch.ElapsedMilliseconds; if (reader.Read()) { return new Diagnose { Id = reader.GetInt32(0), Name = reader.GetString(1), }; } return null; } public override List GetAll(out long elapsedMilliseconds) { var items = new List(); using var connection = GetConnection(); connection.Open(); using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses ORDER BY diagnose_id", connection); Stopwatch stopwatch = new(); stopwatch.Start(); using var reader = cmd.ExecuteReader(); stopwatch.Stop(); elapsedMilliseconds = stopwatch.ElapsedMilliseconds; while (reader.Read()) { items.Add(new Diagnose { Id = reader.GetInt32(0), Name = reader.GetString(1), }); } return items; } public override void Insert(Diagnose item, out long elapsedMilliseconds) { using var connection = GetConnection(); connection.Open(); using var cmd = new NpgsqlCommand("INSERT INTO diagnoses (diagnose_id, name) VALUES ((nextval('diagnoses_id_seq')), @name)", connection); cmd.Parameters.AddWithValue("@name", item.Name); Stopwatch stopwatch = new(); stopwatch.Start(); cmd.ExecuteNonQuery(); stopwatch.Stop(); elapsedMilliseconds = stopwatch.ElapsedMilliseconds; } public override void Update(Diagnose item, out long elapsedMilliseconds) { using var connection = GetConnection(); connection.Open(); using var cmd = new NpgsqlCommand("UPDATE diagnoses SET name = @name WHERE diagnose_id = @id", connection); cmd.Parameters.AddWithValue("@id", item.Id); cmd.Parameters.AddWithValue("@name", item.Name); Stopwatch stopwatch = new(); stopwatch.Start(); cmd.ExecuteNonQuery(); stopwatch.Stop(); elapsedMilliseconds = stopwatch.ElapsedMilliseconds; } } }