124 lines
4.6 KiB
C#
124 lines
4.6 KiB
C#
using Npgsql;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Web.Profile;
|
|
using System.Windows.Forms.PropertyGridInternal;
|
|
|
|
namespace DatenDB
|
|
{
|
|
public enum Kategorie
|
|
{
|
|
Allgemein = 0,
|
|
Fahrer = 1,
|
|
Intern = 2,
|
|
Waschstrasse = 3
|
|
}
|
|
public class Aufgabe
|
|
{
|
|
//AUFGABE TABLE IN DB INTEGRIEREN
|
|
private static string TABLE = "kundenverwaltung.aufgabe";
|
|
public static string COLUMNS = "aufgabe_id, bezeichnung, beschreibung, kategorie, farbe";
|
|
public Aufgabe()
|
|
{
|
|
}
|
|
public static Aufgabe GetAufgabe(string bezeichnung, int? id)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
Aufgabe result = new Aufgabe();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
if (!string.IsNullOrEmpty(bezeichnung)) command.CommandText = $"select {COLUMNS} from {TABLE} where bezeichnung = '{bezeichnung}'";
|
|
if (id != null) command.CommandText = $"select {COLUMNS} from {TABLE} where aufgabe_id = {id}";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) result = new Aufgabe(reader);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
}
|
|
public static int GetAufgabeID(string text)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
int result = 0;
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
command.CommandText = $"select aufgabe_id from {TABLE} where bezeichnung = '{text}'";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) result = reader.GetInt32(0);
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
}
|
|
public static List<Aufgabe> GetList(int? aufgabekat)
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
List<Aufgabe> resultList = new List<Aufgabe>();
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
if(aufgabekat != null) command.CommandText = $"select {COLUMNS} from {TABLE} where kategorie = {aufgabekat} order by aufgabe_id";
|
|
else command.CommandText = $"select {COLUMNS} from {TABLE} order by aufgabe_id";
|
|
NpgsqlDataReader reader = command.ExecuteReader();
|
|
while (reader.Read()) resultList.Add(new Aufgabe(reader));
|
|
reader.Close();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return resultList;
|
|
|
|
}
|
|
public int Save()
|
|
{
|
|
DatenbankConnection.GetConnection().Open();
|
|
|
|
NpgsqlCommand command = new NpgsqlCommand();
|
|
command.Connection = DatenbankConnection.GetConnection();
|
|
|
|
if (this.AufgabeID.HasValue & this.AufgabeID != 0)
|
|
{
|
|
command.CommandText = $"update {TABLE} set bezeichnung = :p1, beschreibung = :p2, kategorie = :p3, farbe = :p4 WHERE aufgabe_id = :p0";
|
|
}
|
|
else
|
|
{
|
|
command.CommandText = "select nextval('kundenverwaltung.aufgabe_seq')";
|
|
this.AufgabeID = (int)(long)command.ExecuteScalar();
|
|
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2, :p3, :p4)";
|
|
}
|
|
|
|
command.Parameters.AddWithValue("p0", this.AufgabeID);
|
|
command.Parameters.AddWithValue("p1", string.IsNullOrEmpty(this.Bezeichnung) ? (object)DBNull.Value : this.Bezeichnung);
|
|
command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Beschreibung) ? (object)DBNull.Value : this.Beschreibung);
|
|
command.Parameters.AddWithValue("p3", (int)this.Kategorie);
|
|
command.Parameters.AddWithValue("p4", (string)HexConverter(this.Farbe, string.Empty));
|
|
|
|
|
|
|
|
int result = command.ExecuteNonQuery();
|
|
DatenbankConnection.GetConnection().Close();
|
|
return result;
|
|
}
|
|
public Aufgabe(NpgsqlDataReader reader)
|
|
{
|
|
this.AufgabeID = reader.GetInt32(0);
|
|
this.Bezeichnung = reader.IsDBNull(1) ? string.Empty : reader.GetString(1);
|
|
this.Beschreibung = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
|
|
this.Kategorie = reader.IsDBNull(3) ? Kategorie.Fahrer : (Kategorie)reader.GetInt16(3);
|
|
this.Farbe = reader.IsDBNull(4) ? Color.FromArgb(1, 53, 101) : (Color)HexConverter(Color.FromArgb(1, 53, 101), reader.GetString(4));
|
|
}
|
|
public int? AufgabeID { get; set; }
|
|
public string Bezeichnung { get; set; }
|
|
public string Beschreibung { get; set; }
|
|
public Kategorie Kategorie { get; set; }
|
|
public Color Farbe { get; set; }
|
|
|
|
private static object HexConverter(Color c, string s)
|
|
{
|
|
ColorConverter cc = new ColorConverter();
|
|
if (string.IsNullOrEmpty(s)) { s = "#" + c.R.ToString("X2") + c.G.ToString("X2") + c.B.ToString("X2"); return s; }
|
|
else { c = (Color)cc.ConvertFromString(s); return c; }
|
|
|
|
|
|
}
|
|
}
|
|
}
|