SchachtDataService hinzugefügt
This commit is contained in:
@@ -7,31 +7,15 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.DAL.Services
|
||||
namespace SewerStammGen.DAL.Services.PostgresqlData
|
||||
{
|
||||
public class AuftraggeberDataService : PostgresqlDataService, IAuftraggeberDataService
|
||||
public class AuftraggeberDataService : PostgresqlDataService, IAuftraggeberDataService
|
||||
{
|
||||
/*
|
||||
* CREATE TABLE public."Auftraggebers"
|
||||
(
|
||||
"Id" serial,
|
||||
"Name" text,
|
||||
"Strasse" text,
|
||||
"Ort" text,
|
||||
"Postleitzahl" text,
|
||||
"Ansprechpartner" text,
|
||||
"Telefonnummer" text,
|
||||
PRIMARY KEY ("Id")
|
||||
);
|
||||
public AuftraggeberDataService(string connectionstring) : base(connectionstring,"Auftraggebers") { }
|
||||
|
||||
ALTER TABLE IF EXISTS public."Auftraggebers"
|
||||
OWNER to "SewerGen";
|
||||
*/
|
||||
public AuftraggeberDataService() : base("public.\"Auftraggebers\"") { }
|
||||
|
||||
public async Task<Auftraggeber> Create(Auftraggeber entity)
|
||||
{
|
||||
string command = "INSERT INTO public.\"Auftraggebers\" (\"Name\", \"Strasse\", \"Ort\", \"Postleitzahl\", \"Ansprechpartner\", \"Telefonnummer\") " +
|
||||
string command = "INSERT INTO "+tableName+" (\"Name\", \"Strasse\", \"Ort\", \"Postleitzahl\", \"Ansprechpartner\", \"Telefonnummer\") " +
|
||||
"VALUES(@1,@2,@3,@4,@5,@6) RETURNING \"Id\"";
|
||||
|
||||
await using (var cmd = new NpgsqlCommand(command, conn))
|
||||
@@ -42,7 +26,7 @@ ALTER TABLE IF EXISTS public."Auftraggebers"
|
||||
cmd.Parameters.AddWithValue("4", entity.Postleitzahl);
|
||||
cmd.Parameters.AddWithValue("5", entity.Ansprechpartner);
|
||||
cmd.Parameters.AddWithValue("6", entity.Telefonnummer);
|
||||
|
||||
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
reader.Read();
|
||||
entity.Id = reader.GetInt32(0);
|
||||
@@ -53,14 +37,14 @@ ALTER TABLE IF EXISTS public."Auftraggebers"
|
||||
public async Task<Auftraggeber> Get(int id)
|
||||
{
|
||||
Auftraggeber result = new Auftraggeber();
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM public.\"Auftraggebers\" WHERE \"Id\" = @1", conn))
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM "+tableName+" WHERE \"Id\" = @1", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", id);
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
reader.Read();
|
||||
result = parseAuftraggeber(reader);
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
private Auftraggeber parseAuftraggeber(NpgsqlDataReader reader)
|
||||
@@ -80,7 +64,7 @@ ALTER TABLE IF EXISTS public."Auftraggebers"
|
||||
public async Task<IEnumerable<Auftraggeber>> GetAll()
|
||||
{
|
||||
List<Auftraggeber> result = new List<Auftraggeber>();
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM public.\"Auftraggebers\"", conn))
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM "+tableName, conn))
|
||||
using (var reader = await cmd.ExecuteReaderAsync())
|
||||
{
|
||||
while (reader.Read())
|
||||
@@ -0,0 +1,85 @@
|
||||
using Npgsql;
|
||||
using SewerStammGen.Shared.Contracts;
|
||||
using SewerStammGen.Shared.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.DAL.Services.PostgresqlData
|
||||
{
|
||||
public class HaltungDataService : PostgresqlDataService, IHaltungDataService
|
||||
{
|
||||
public HaltungDataService(string connectionstring) : base(connectionstring, "Kanaele")
|
||||
{
|
||||
}
|
||||
|
||||
public Task<Kanal> Create(Kanal entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<Kanal> Get(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Kanal>> GetAll()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Kanal>> GetAllByProjekt(int projektID)
|
||||
{
|
||||
List<Kanal> result = new List<Kanal>();
|
||||
ISchachtDataService schachtDataService = new SchachtDataService(connString);
|
||||
IEnumerable<Schacht> schaechte = await schachtDataService.GetAllByProjekt(projektID);
|
||||
|
||||
string command = "SELECT * FROM " + tableName + " WHERE \"ProjektId\" = @1";
|
||||
using (var cmd = new NpgsqlCommand(command, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", projektID);
|
||||
using (var reader = await cmd.ExecuteReaderAsync())
|
||||
{
|
||||
while(reader.Read())
|
||||
{
|
||||
Kanal adding = parseHaltung(reader);
|
||||
|
||||
adding.StartSchacht = schaechte.Where(x => x.Id == reader.GetInt32(2)).Last();
|
||||
adding.EndSchacht = schaechte.Where(x => x.Id == reader.GetInt32(3)).Last();
|
||||
result.Add(adding);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private Kanal parseHaltung(NpgsqlDataReader reader)
|
||||
{
|
||||
return new Kanal()
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Objektbezeichnung = reader.GetString(1),
|
||||
//StartSchacht = reader.GetInt32(2),
|
||||
//EndSchacht = reader.GetInt32(3),
|
||||
DN = reader.GetInt32(4),
|
||||
Material = reader.GetString(5),
|
||||
Haltungslaenge = reader.GetDecimal(6),
|
||||
Entwaesserung = (EEntwaeserung)reader.GetInt32(7),
|
||||
Projekt = new Projekt() { Id = reader.GetInt32(8) }
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Kanal>> GetAllByProjekt(Projekt projekt)
|
||||
{
|
||||
return await GetAllByProjekt(projekt.Id);
|
||||
}
|
||||
|
||||
public Task<Kanal> Update(Kanal entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,29 +6,30 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.DAL.Services
|
||||
namespace SewerStammGen.DAL.Services.PostgresqlData
|
||||
{
|
||||
public class PostgresqlDataService : IDisposable
|
||||
{
|
||||
string connString = "Host = localhost; Database = SewerGen; Username = SewerGen; Password = SewerGen";
|
||||
protected readonly string connString; // = "Host = localhost; Database = SewerGen; Username = SewerGen; Password = SewerGen";
|
||||
NpgsqlDataSource dataSource;
|
||||
protected NpgsqlConnection? conn = null;
|
||||
private string tableName = "";
|
||||
protected string tableName = "";
|
||||
|
||||
public PostgresqlDataService(string tableName)
|
||||
public PostgresqlDataService(string connectionstring, string tableName)
|
||||
{
|
||||
this.connString = connectionstring;
|
||||
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
|
||||
dataSource = dataSourceBuilder.Build();
|
||||
|
||||
conn = dataSource.OpenConnection();
|
||||
this.tableName = tableName;
|
||||
this.tableName = string.Format("public.\"{0}\"", tableName);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public virtual async Task<bool> Delete(int id)
|
||||
{
|
||||
string command = $"DELETE "+tableName+" WHERE \"Id\" = @1";
|
||||
string command = $"DELETE " + tableName + " WHERE \"Id\" = @1";
|
||||
await using (var cmd = new NpgsqlCommand(command, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", id);
|
||||
@@ -39,7 +40,7 @@ namespace SewerStammGen.DAL.Services
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if(conn != null && conn.State == System.Data.ConnectionState.Open)
|
||||
if (conn != null && conn.State == System.Data.ConnectionState.Open)
|
||||
{
|
||||
conn.Close();
|
||||
conn.Dispose();
|
||||
@@ -10,21 +10,28 @@ using SewerStammGen.Shared.Domain;
|
||||
using SewerStammGen.Shared.Enum;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace SewerStammGen.DAL.Services
|
||||
namespace SewerStammGen.DAL.Services.PostgresqlData
|
||||
{
|
||||
public class ProjektDataService : PostgresqlDataService, IProjektDataService
|
||||
{
|
||||
public ProjektDataService() : base("public.\"Projekte\"")
|
||||
public ProjektDataService(string connectionstring) : base(connectionstring,"Projekte")
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
public async Task<Projekt> Create(Projekt entity)
|
||||
{
|
||||
string command = "INSERT INTO public.\"Projekte\" (\"Projektname\", \"Erstelldatum\", \"Strasse\", \"Ort\", \"ExportType\", \"Kodierungssystem\",\"AuftraggeberId\") " +
|
||||
string command = "INSERT INTO "+tableName+" (\"Projektname\", \"Erstelldatum\", \"Strasse\", \"Ort\", \"ExportType\", \"Kodierungssystem\",\"AuftraggeberId\") " +
|
||||
"VALUES(@1,@2,@3,@4,@5,@6,@7) RETURNING \"Id\"";
|
||||
|
||||
await using(var cmd = new NpgsqlCommand(command, conn))
|
||||
if (entity.Auftraggeber.Id == 0)
|
||||
{
|
||||
IAuftraggeberDataService auftraggeberDataService = new AuftraggeberDataService(connString);
|
||||
var s = await auftraggeberDataService.GetAll();
|
||||
entity.Auftraggeber = s.ToList().Last();
|
||||
}
|
||||
|
||||
await using (var cmd = new NpgsqlCommand(command, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", entity.Projektname);
|
||||
cmd.Parameters.AddWithValue("2", entity.Erstelldatum);
|
||||
@@ -44,8 +51,8 @@ namespace SewerStammGen.DAL.Services
|
||||
public async Task<Projekt> Get(int id)
|
||||
{
|
||||
Projekt result = new Projekt();
|
||||
IAuftraggeberDataService auftraggeberDataService = new AuftraggeberDataService();
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM public.\"Projekte\" WHERE \"Id\" = @1", conn))
|
||||
IAuftraggeberDataService auftraggeberDataService = new AuftraggeberDataService(connString);
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM "+tableName+" WHERE \"Id\" = @1", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", id);
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
@@ -72,12 +79,12 @@ namespace SewerStammGen.DAL.Services
|
||||
|
||||
public async Task<IEnumerable<Projekt>> GetAll()
|
||||
{
|
||||
IAuftraggeberDataService auftraggeberDataService = new AuftraggeberDataService();
|
||||
IAuftraggeberDataService auftraggeberDataService = new AuftraggeberDataService(connString);
|
||||
|
||||
IEnumerable<Auftraggeber> auftraggebers = await auftraggeberDataService.GetAll();
|
||||
|
||||
List<Projekt> result = new List<Projekt>();
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM public.\"Projekte\"", conn))
|
||||
using (var cmd = new NpgsqlCommand($"SELECT * FROM " + tableName, conn))
|
||||
using (var reader = await cmd.ExecuteReaderAsync())
|
||||
{
|
||||
while (reader.Read())
|
||||
@@ -86,14 +93,14 @@ namespace SewerStammGen.DAL.Services
|
||||
projekt.Auftraggeber = auftraggebers.Where(x => x.Id == reader.GetInt32(7)).ToList().Last();
|
||||
result.Add(projekt);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Projekt> Update(Projekt entity)
|
||||
{
|
||||
string command = "UPDATE public.\"Projekte\" SET \"Projektname\"=@1, \"Erstelldatum\"=@2, \"Strasse\"=@3, \"Ort\"=@4, \"ExportType\"=@5, \"Kodierungssystem\"=@6 WHERE \"Id\" = @8";
|
||||
using(var cmd = new NpgsqlCommand(command, conn))
|
||||
string command = "UPDATE "+tableName+" SET \"Projektname\"=@1, \"Erstelldatum\"=@2, \"Strasse\"=@3, \"Ort\"=@4, \"ExportType\"=@5, \"Kodierungssystem\"=@6 WHERE \"Id\" = @8";
|
||||
using (var cmd = new NpgsqlCommand(command, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", entity.Projektname);
|
||||
cmd.Parameters.AddWithValue("2", entity.Erstelldatum);
|
||||
@@ -108,27 +115,4 @@ namespace SewerStammGen.DAL.Services
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* CREATE TABLE public."Projekte"
|
||||
(
|
||||
"Id" serial,
|
||||
"Projektname" text,
|
||||
"Erstelldatum" text,
|
||||
"Strasse" text,
|
||||
"Ort" text,
|
||||
"ExportType" bigint,
|
||||
"Kodierungssystem" bigint,
|
||||
"AuftraggeberId" bigint,
|
||||
PRIMARY KEY ("Id"),
|
||||
CONSTRAINT "Auftraggeber" FOREIGN KEY ("AuftraggeberId")
|
||||
REFERENCES public."Auftraggebers" ("Id") MATCH SIMPLE
|
||||
ON UPDATE NO ACTION
|
||||
ON DELETE NO ACTION
|
||||
NOT VALID
|
||||
);
|
||||
|
||||
ALTER TABLE IF EXISTS public."Projekte"
|
||||
OWNER to "SewerGen";
|
||||
*/
|
||||
}
|
||||
109
SewerStammGen.DAL/Services/PostgresqlData/SchachtDataService.cs
Normal file
109
SewerStammGen.DAL/Services/PostgresqlData/SchachtDataService.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Npgsql;
|
||||
using SewerStammGen.Shared.Contracts;
|
||||
using SewerStammGen.Shared.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.DAL.Services.PostgresqlData
|
||||
{
|
||||
public class SchachtDataService : PostgresqlDataService, ISchachtDataService
|
||||
{
|
||||
public SchachtDataService(string connectionstring) : base(connectionstring,"Schaechte")
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Schacht> Create(Schacht entity)
|
||||
{
|
||||
string command = "INSERT INTO " + tableName + " (\"Objektbezeichnung\",\"RechtsWert\",\"HochWert\",\"SohlHoehe\",\"DeckelHoehe\",\"Entwaesserung\",\"ProjektId\") VALUES " +
|
||||
"(@1,@2,@3,@4,@5,@6,@7) RETURNING \"Id\"";
|
||||
using(var cmd = new NpgsqlCommand(command,conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", entity.Objektbezeichnung);
|
||||
cmd.Parameters.AddWithValue("2", entity.RechtsWert);
|
||||
cmd.Parameters.AddWithValue("3", entity.HochWert);
|
||||
cmd.Parameters.AddWithValue("4", entity.SohlHoehe);
|
||||
cmd.Parameters.AddWithValue("5", entity.DeckelHoehe);
|
||||
cmd.Parameters.AddWithValue("6", (int)entity.Entwaesserung);
|
||||
cmd.Parameters.AddWithValue("7", entity.Projekt.Id);
|
||||
using var reader = await cmd.ExecuteReaderAsync();
|
||||
reader.Read();
|
||||
entity.Id = reader.GetInt32(0);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Task<Schacht> Get(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Schacht>> GetAll()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private Schacht parseSchacht(NpgsqlDataReader? reader)
|
||||
{
|
||||
return new Schacht()
|
||||
{
|
||||
Id = reader.GetInt32(0),
|
||||
Objektbezeichnung = reader.IsDBNull(1) ? "": reader.GetString(1),
|
||||
RechtsWert = reader.GetDecimal(2),
|
||||
HochWert = reader.GetDecimal(3),
|
||||
SohlHoehe = reader.GetDecimal(4),
|
||||
DeckelHoehe = reader.GetDecimal(5),
|
||||
Entwaesserung = (EEntwaeserung)reader.GetInt32(6),
|
||||
Projekt = new Projekt() { Id = reader.GetInt32(7) },
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Schacht>> GetAllByProjekt(int projektID)
|
||||
{
|
||||
List<Schacht> result = new List<Schacht>();
|
||||
string command = @"SELECT * FROM " + tableName + " WHERE \"ProjektId\" = @1";
|
||||
using (var cmd = new NpgsqlCommand(command, conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", projektID);
|
||||
using (var reader = await cmd.ExecuteReaderAsync())
|
||||
{
|
||||
while(reader.Read())
|
||||
{
|
||||
result.Add(parseSchacht(reader));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Schacht>> GetAllByProjekt(Projekt projekt)
|
||||
{
|
||||
return await GetAllByProjekt(projekt.Id);
|
||||
}
|
||||
|
||||
public async Task<Schacht> Update(Schacht entity)
|
||||
{
|
||||
string command = @"UPDATE " + tableName + "SET \"Objektbezeichnung\" =@1, \"RechtsWert\"=@2," +
|
||||
" \"HochWert\"=@3, \"SohlHoehe\"=@4, \"DeckelHoehe\"=@5, \"Entwaesserung\"=@6," +
|
||||
" \"ProjektId\"=@7 WHERE \"Id\"=@8";
|
||||
using(var cmd = new NpgsqlCommand(command,conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("1", entity.Objektbezeichnung);
|
||||
cmd.Parameters.AddWithValue("2", entity.RechtsWert);
|
||||
cmd.Parameters.AddWithValue("3", entity.HochWert);
|
||||
cmd.Parameters.AddWithValue("4", entity.SohlHoehe);
|
||||
cmd.Parameters.AddWithValue("5", entity.DeckelHoehe);
|
||||
cmd.Parameters.AddWithValue("6", (int)entity.Entwaesserung);
|
||||
cmd.Parameters.AddWithValue("7", entity.Projekt.Id);
|
||||
cmd.Parameters.AddWithValue("8", entity.Id);
|
||||
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
return entity;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
using Npgsql;
|
||||
using SewerStammGen.Shared.Contracts;
|
||||
using SewerStammGen.Shared.Domain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SewerStammGen.DAL.Services
|
||||
{
|
||||
public class SchachtDataService : PostgresqlDataService, ISchachtDataService
|
||||
{
|
||||
public SchachtDataService() : base("public.\"Schaechte\"")
|
||||
{
|
||||
}
|
||||
|
||||
public Task<Schacht> Create(Schacht entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<Schacht> Get(int id)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Schacht>> GetAll()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private Schacht parseSchacht(NpgsqlDataReader? reader)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Schacht>> GetAllByProjekt(int projektID)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Schacht>> GetAllByProjekt(Projekt projekt)
|
||||
{
|
||||
return await GetAllByProjekt(projekt.Id);
|
||||
}
|
||||
|
||||
public Task<Schacht> Update(Schacht entity)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,4 +15,8 @@
|
||||
<ProjectReference Include="..\Shared\SewerStammGen.Shared.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Services\SQLiteData\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user