Grundlegene Funktionen geschrieben

This commit is contained in:
2023-06-09 15:53:11 +02:00
parent 2c4e8fb4cb
commit fd84775aa4
26 changed files with 721 additions and 4 deletions

View File

@@ -0,0 +1,39 @@
using dcnsanplanung.DAL.Services.PostgresqlData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dcnsanplanung.DAL.Helper
{
public class WriteToDatabase
{
List<shared.Model.Haltung> haltungen = new List<shared.Model.Haltung>();
List<shared.Model.LeistungsverzeichnisPosition> LVPositionen = new List<shared.Model.LeistungsverzeichnisPosition>();
public WriteToDatabase(string XMLFile, string connectionstring = "Host = localhost; Database = sanplaner; Username = dcnsanplaner; Password = sanplaner")
{
haltungen = new shared.Helper.ImportToSoftware(XMLFile).haltungen;
LVPositionen = new shared.Helper.ImportLVToSoftware(@"C:\Users\damia\source\repos\dcnsanplanung\BE-BS-AWN DW_0001_OOWV_Stamm_LV_Kanalsanierung_xml33.X81").LVPositionen;
}
public async Task<bool> WriteInHaltung()
{
string connectionstring = "Host = localhost; Database = sanplaner; Username = dcnsanplaner; Password = sanplaner";
HaltungDataService haltungDataService = new HaltungDataService(connectionstring);
await haltungDataService.InsertHaltungBulk(haltungen);
return true;
}
public async Task<bool> WriteInLV()
{
string connectionstring = "Host = localhost; Database = sanplaner; Username = dcnsanplaner; Password = sanplaner";
LVDataService lVDataService = new LVDataService(connectionstring);
await lVDataService.InsertLeistungsverzeichnisPositionBulk(LVPositionen);
return true;
}
}
}

View File

@@ -0,0 +1,87 @@
using dcnsanplanung.shared.Model;
using Npgsql;
using Schnittstelle.Import.XML.v2013.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dcnsanplanung.DAL.Services.PostgresqlData
{
public class HaltungDataService : PostgresqlDataService
{
SchadenDataService schadenDataService;
public HaltungDataService(string connectionstring) : base(connectionstring, "haltung")
{
schadenDataService = new SchadenDataService(connectionstring);
}
public async Task<Haltung> Create(Haltung entity)
{
string command = "INSERT INTO " + tableName + " (guid, ref_projekt_guid, objektbezeichnung, bewertungklasse) VALUES " +
"(@1,@2,@3,@4) RETURNING id";
using (var cmd = new NpgsqlCommand(command, conn))
{
cmd.Parameters.AddWithValue("1", entity.Guid.ToString());
cmd.Parameters.AddWithValue("2", entity.Ref_Projekt_Guid.ToString());
cmd.Parameters.AddWithValue("3", entity.Objektbezeichnung);
cmd.Parameters.AddWithValue("4", NpgsqlTypes.NpgsqlDbType.Oid, entity.Bewertungklasse);
using var reader = await cmd.ExecuteReaderAsync();
reader.Read();
entity.ID = reader.GetInt32(0);
}
await schadenDataService.InsertSchadenBulk(entity.Kodierungen);
return entity;
}
public async Task<IEnumerable<Haltung>> GetAllByProjekt(int projektID)
{
List<Haltung> result = new List<Haltung>();
//ISchachtDataService schachtDataService = new SchachtDataService(connString);
//IEnumerable<Schacht> schaechte = await schachtDataService.GetAllByProjekt(projektID);
string command = "SELECT * FROM " + tableName + ";";//" WHERE ref_projekt_id = @1";
using (var cmd = new NpgsqlCommand(command, conn))
{
//cmd.Parameters.AddWithValue("1", projektID);
using (var reader = await cmd.ExecuteReaderAsync())
{
while (reader.Read())
{
Haltung adding = parseHaltung(reader);
result.Add(adding);
}
}
}
return result;
}
private Haltung parseHaltung(NpgsqlDataReader reader)
{
Haltung result = new Haltung()
{
ID = reader.GetInt32(0),
Guid = Guid.Parse(reader.GetString(1)),
Ref_Projekt_Guid = Guid.Parse(reader.GetString(2)),
Objektbezeichnung = reader.GetString(3),
Bewertungklasse = Convert.ToUInt32(reader.GetValue(4)),
};
return result;
}
public async Task<bool> InsertHaltungBulk(List<Haltung> haltungen)
{
foreach (var item in haltungen)
{
await Create(item);
}
return true;
}
}
}

View File

@@ -0,0 +1,75 @@
using dcnsanplanung.shared.Model;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dcnsanplanung.DAL.Services.PostgresqlData
{
public class LVDataService : PostgresqlDataService
{
public LVDataService(string connectionstring) : base(connectionstring, "leistungsverzeichnisposition")
{
}
public async Task<LeistungsverzeichnisPosition> Create(LeistungsverzeichnisPosition entity)
{
string command = "INSERT INTO " + tableName + " (guid, positionsnummer, titel, einheit) VALUES " +
"(@1,@2,@3,@4) RETURNING id";
using (var cmd = new NpgsqlCommand(command, conn))
{
cmd.Parameters.AddWithValue("1", entity.Guid.ToString());
cmd.Parameters.AddWithValue("2", entity.Positionsnummer);
cmd.Parameters.AddWithValue("3", entity.Titel);
cmd.Parameters.AddWithValue("4", entity.Einheit);
using var reader = await cmd.ExecuteReaderAsync();
reader.Read();
entity.ID = reader.GetInt32(0);
}
return entity;
}
public async Task<IEnumerable<LeistungsverzeichnisPosition>> GetAll()
{
List<LeistungsverzeichnisPosition> result = new List<LeistungsverzeichnisPosition>();
string command = "SELECT * FROM " + tableName + ";";
using (var cmd = new NpgsqlCommand(command, conn))
{
using (var reader = await cmd.ExecuteReaderAsync())
{
while (reader.Read())
{
LeistungsverzeichnisPosition adding = parsePosition(reader);
result.Add(adding);
}
}
}
return result;
}
private LeistungsverzeichnisPosition parsePosition(NpgsqlDataReader reader)
{
LeistungsverzeichnisPosition result = new LeistungsverzeichnisPosition()
{
ID = reader.GetInt32(0),
Guid = Guid.Parse(reader.GetString(1)),
Positionsnummer = reader.GetString(2),
Titel = reader.GetString(3),
Einheit = reader.GetString(4)
};
return result;
}
public async Task<bool> InsertLeistungsverzeichnisPositionBulk(List<LeistungsverzeichnisPosition> position)
{
foreach (var item in position)
{
await Create(item);
}
return true;
}
}
}

View File

@@ -0,0 +1,47 @@
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dcnsanplanung.DAL.Services.PostgresqlData
{
public class PostgresqlDataService : IDisposable
{
protected readonly string connString; // = "Host = localhost; Database = sanplaner; Username = dcnsanplaner; Password = sanplaner";
NpgsqlDataSource dataSource;
protected NpgsqlConnection? conn = null;
protected string tableName = "";
public PostgresqlDataService(string connectionstring, string tableName)
{
this.connString = connectionstring;
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
dataSource = dataSourceBuilder.Build();
conn = dataSource.OpenConnection();
this.tableName = string.Format("public.{0}", tableName);
}
public virtual async Task<bool> Delete(int id)
{
string command = $"DELETE " + tableName + " WHERE \"Id\" = @1";
await using (var cmd = new NpgsqlCommand(command, conn))
{
cmd.Parameters.AddWithValue("1", id);
int res = await cmd.ExecuteNonQueryAsync();
}
return true;
}
public void Dispose()
{
if (conn != null && conn.State == System.Data.ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
}
}

View File

@@ -0,0 +1,44 @@
using dcnsanplanung.shared.Model;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dcnsanplanung.DAL.Services.PostgresqlData
{
public class SchadenDataService : PostgresqlDataService
{
public SchadenDataService(string connectionstring) : base(connectionstring, "schaden")
{
}
public async Task<Schaden> Create(Schaden entity)
{
string command = "INSERT INTO " + tableName + " (guid, ref_haltung_guid, entfernung, kodierung, schadensklasse) VALUES " +
"(@1,@2,@3,@4,@5) RETURNING id";
using(var cmd = new NpgsqlCommand(command,conn))
{
cmd.Parameters.AddWithValue("1", entity.Guid.ToString());
cmd.Parameters.AddWithValue("2", entity.Ref_Haltung_Guid.ToString());
cmd.Parameters.AddWithValue("3", entity.Entfernung);
cmd.Parameters.AddWithValue("4", entity.Kodierung);
cmd.Parameters.AddWithValue("5", NpgsqlTypes.NpgsqlDbType.Oid, entity.Schadensklasse);
using var reader = await cmd.ExecuteReaderAsync();
reader.Read();
entity.ID = reader.GetInt32(0);
}
return entity;
}
public async Task<bool> InsertSchadenBulk(List<Schaden> schaden)
{
foreach(var item in schaden)
{
await Create(item);
}
return true;
}
}
}

View File

@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="7.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\dcnsanplanung.shared\dcnsanplanung.shared.csproj" />
<ProjectReference Include="..\Schnittstelle\Schnittstelle\Schnittstelle.csproj" />
</ItemGroup>
</Project>