Files
SewerGenerator/SewerStammGen.DAL/Services/PostgresqlData/SchachtDataService.cs
2023-04-20 20:37:39 +02:00

128 lines
5.0 KiB
C#

using Npgsql;
using SewerStammGen.Shared.Contracts;
using SewerStammGen.Shared.Domain;
using SewerStammGen.Shared.Enum;
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,"schacht")
{
}
public async Task<Schacht> Create(Schacht entity)
{
string command = "INSERT INTO " + tableName + " (" +
"objektbezeichnung,deckelrechtswert,deckelhochwert," +
"sohlrechtswert,sohlhochwert,sohlhoehe,deckelhoehe,entwaesserung,ref_projekt_id) VALUES " +
"(@1,@2,@3,@4,@5,@6,@7,@8,@9) RETURNING schacht_id";
using(var cmd = new NpgsqlCommand(command,conn))
{
cmd.Parameters.AddWithValue("1", entity.Objektbezeichnung);
cmd.Parameters.AddWithValue("2", entity.DeckelRechtsWert);
cmd.Parameters.AddWithValue("3", entity.DeckelHochWert);
cmd.Parameters.AddWithValue("4", entity.SohlRechtsWert);
cmd.Parameters.AddWithValue("5", entity.SohlHochWert);
cmd.Parameters.AddWithValue("6", entity.SohlHoehe);
cmd.Parameters.AddWithValue("7", entity.DeckelHoehe);
cmd.Parameters.AddWithValue("8", (int)entity.Entwaesserung);
cmd.Parameters.AddWithValue("9", 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),
DeckelRechtsWert = reader.GetDecimal(2),
DeckelHochWert = reader.GetDecimal(3),
DeckelHoehe = reader.GetDecimal(4),
SohlRechtsWert = reader.GetDecimal(5),
SohlHochWert = reader.GetDecimal(6),
SohlHoehe = reader.GetDecimal(7),
Entwaesserung = (EEntwaeserung)reader.GetInt32(8),
Projekt = new Projekt() { Id = reader.GetInt32(9) },
};
}
public async Task<IEnumerable<Schacht>> GetAllByProjekt(int projektID)
{
List<Schacht> result = new List<Schacht>();
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())
{
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, deckelrechtswert=@2, deckelhochwert=@3, deckelhoehe=@4, " +
"sohlrechtswert=@5, sohlhochwert=@6, sohlhoehe=@7, entwaesserung=@8, ref_projekt_id=@9 WHERE schacht_id=@10";
using(var cmd = new NpgsqlCommand(command,conn))
{
cmd.Parameters.AddWithValue("1", entity.Objektbezeichnung);
cmd.Parameters.AddWithValue("2", entity.DeckelRechtsWert);
cmd.Parameters.AddWithValue("3", entity.DeckelHochWert);
cmd.Parameters.AddWithValue("4", entity.DeckelHoehe);
cmd.Parameters.AddWithValue("5", entity.SohlRechtsWert);
cmd.Parameters.AddWithValue("6", entity.SohlHochWert);
cmd.Parameters.AddWithValue("7", entity.SohlHoehe);
cmd.Parameters.AddWithValue("8", (int)entity.Entwaesserung);
cmd.Parameters.AddWithValue("9", entity.Projekt.Id);
cmd.Parameters.AddWithValue("10", entity.Id);
await cmd.ExecuteNonQueryAsync();
}
return entity;
}
public async Task<bool> InsertSchachtBulk(List<Schacht> schacht)
{
foreach(var item in schacht)
{
await Create(item);
}
return true;
}
}
}