99 lines
3.3 KiB
C#

using DatenDB;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace Deckungsbeitrag.AA_Klassen
{
internal class Saison
{
public const string COLUMNS = "saison_id, bezeichnung, start_date, end_date, aktiv";
private const string TABLE = "kundenverwaltung.saison";
public static Saison GetAktivSaison()
{
DatenbankConnection.GetConnection().Open();
Saison saison = null;
using (NpgsqlCommand command = new NpgsqlCommand())
{
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} where aktiv = {true}";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) saison = new Saison(reader);
reader.Close();
}
DatenbankConnection.GetConnection().Close();
return saison;
}
public static List<Saison> LadeSaisons()
{
DatenbankConnection.GetConnection().Open();
List<Saison> resultList = new List<Saison>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} order by bezeichnung";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultList.Add(new Saison(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultList;
}
public Saison(NpgsqlDataReader reader)
{
this.SaisonId = reader.GetInt32(0);
this.Bezeichnung = reader.GetString(1);
this.StartDatum = reader.IsDBNull(2) ? (DateTime?)null : reader.GetDateTime(2);
this.EndDatum = reader.IsDBNull(3) ? (DateTime?)null : reader.GetDateTime(3);
this.Aktiv = reader.GetBoolean(4);
}
public int? SaisonId { get; set; }
public string Bezeichnung { get; set; }
public bool Aktiv { get; set; }
public DateTime? StartDatum { get; set; }
public DateTime? EndDatum { get; set; }
public override string ToString() => Bezeichnung;
public int Save()
{
DatenbankConnection.GetConnection().Open();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
if (this.SaisonId.HasValue & this.SaisonId != 0)
{
command.CommandText = $"update {TABLE} set bezeichnung = :p1, start_date = :p2, end_date = :p3, aktiv = :p4 where saison_id = :p0";
}
else
{
command.CommandText = "select nextval('kundenverwaltung.saison_seq')";
this.SaisonId = (int)(long)command.ExecuteScalar();
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4)";
}
command.Parameters.AddWithValue("p0", this.SaisonId);
command.Parameters.AddWithValue("p1", this.Bezeichnung);
command.Parameters.AddWithValue("p2", this.StartDatum.HasValue ? this.StartDatum.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p3", this.EndDatum.HasValue ? this.EndDatum.Value : (object)DBNull.Value);
command.Parameters.AddWithValue("p4", this.Aktiv);
int result = command.ExecuteNonQuery();
DatenbankConnection.GetConnection().Close();
return result;
}
}
}