Добавил реализацию зранения для Postgresql

This commit is contained in:
Никита Потапов 2024-05-08 00:48:57 +04:00
parent c9666232a3
commit c9f40b690a
5 changed files with 115 additions and 1 deletions

View File

@ -7,6 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MedicalView", "MedicalView\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MedicalDatabaseContracts", "MedicalDatabaseContracts\MedicalDatabaseContracts.csproj", "{43A2B5DF-F305-412B-BE81-161E0B495AEB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MedicalPostgresqlDatabase", "MedicalPostgresqlDatabase\MedicalPostgresqlDatabase.csproj", "{2BAF4C2E-DC1E-443B-85CC-4F44A8C98E17}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -21,6 +23,10 @@ Global
{43A2B5DF-F305-412B-BE81-161E0B495AEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{43A2B5DF-F305-412B-BE81-161E0B495AEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{43A2B5DF-F305-412B-BE81-161E0B495AEB}.Release|Any CPU.Build.0 = Release|Any CPU
{2BAF4C2E-DC1E-443B-85CC-4F44A8C98E17}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BAF4C2E-DC1E-443B-85CC-4F44A8C98E17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BAF4C2E-DC1E-443B-85CC-4F44A8C98E17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BAF4C2E-DC1E-443B-85CC-4F44A8C98E17}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,19 @@
using MedicalDatabaseContracts;
using MedicalDatabaseContracts.Models;
using Npgsql;
namespace MedicalPostgresqlDatabase
{
public abstract class AbstractPostgresqlStorage<T> : IStorage<T> where T : AbstractModel
{
public abstract void Delete(int id);
public abstract T? Get(int id);
public abstract List<T> GetAll();
public abstract void Insert(T item);
public abstract void Update(T item);
protected NpgsqlConnection GetConnection()
{
return new NpgsqlConnection("Host=127.0.0.1;Port=5555;Username=elina;Database=beauty_salon;Password=elina");
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MedicalDatabaseContracts\MedicalDatabaseContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,72 @@
using MedicalDatabaseContracts.Models;
using Npgsql;
namespace MedicalPostgresqlDatabase.Storages
{
public class DiagnosesStorage : AbstractPostgresqlStorage<Diagnose>
{
public override void Delete(int id)
{
using var connection = GetConnection();
connection.Open();
using var cmd = new NpgsqlCommand("DELETE FROM diagnoses WHERE diagnose_id = @id", connection);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
public override Diagnose? Get(int id)
{
using var connection = GetConnection();
connection.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses WHERE diagnose_id = @id", connection);
cmd.Parameters.AddWithValue("@id", id);
using var reader = cmd.ExecuteReader();
if (reader.Read())
{
return new Diagnose
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
};
}
return null;
}
public override List<Diagnose> GetAll()
{
var items = new List<Diagnose>();
using var connection = GetConnection();
connection.Open();
using var cmd = new NpgsqlCommand("SELECT * FROM diagnoses ORDER BY diagnose_id", connection);
using var reader = cmd.ExecuteReader();
while (reader.Read())
{
items.Add(new Diagnose
{
Id = reader.GetInt32(0),
Name = reader.GetString(1),
});
}
return items;
}
public override void Insert(Diagnose item)
{
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);
cmd.ExecuteNonQuery();
}
public override void Update(Diagnose item)
{
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);
cmd.ExecuteNonQuery();
}
}
}

View File

@ -9,7 +9,7 @@
private void FormMain_Load(object sender, EventArgs e)
{
}
}
}