+ {
+ ///
+ /// Liefert ein einzelnes Model-Objekt vom Typ M zurück,
+ /// welches anhand dem übergebenen PrimaryKey geladen wird.
+ ///
+ /// Type des PrimaryKey
+ /// Wert des PrimaryKey
+ /// gefundenes Model-Objekt, ansonsten null
+ M GetSingle(P pkValue);
+
+ ///
+ /// Fügt das Model-Objekt zur Datenbank hinzu (Insert)
+ ///
+ /// zu speicherndes Model-Object
+ void Add(M entity);
+
+ ///
+ /// Löscht das Model-Objekt aus der Datenbank (Delete)
+ ///
+ /// zu löschendes Model-Object
+ void Delete(M entity);
+
+ ///
+ /// Aktualisiert das Model-Objekt in der Datenbank hinzu (Update)
+ ///
+ /// zu aktualisierendes Model-Object
+ void Update(M entity);
+
+ ///
+ /// Gibt eine Liste von Model-Objekten vom Typ M zurück,
+ /// die gemäss der WhereBedingung geladen wurden. Die Werte der
+ /// Where-Bedingung können als separat übergeben werden,
+ /// damit diese für PreparedStatements verwendet werden können.
+ /// (Verhinderung von SQL-Injection)
+ ///
+ /// WhereBedingung als string
+ /// z.B. "NetPrice > @netPrice and Active = @active and Description like @desc
+ /// Parameter-Werte für die Wherebedingung
+ /// bspw: {{"netPrice", 10.5}, {"active", true}, {"desc", "Wolle%"}}
+ ///
+ List GetAll(string whereCondition, Dictionary parameterValues);
+
+ ///
+ /// Gibt eine Liste aller in der DB vorhandenen Model-Objekte vom Typ M zurück
+ ///
+ ///
+ List GetAll();
+
+ ///
+ /// Zählt in der Datenbank die Anzahl Model-Objekte vom Typ M, die der
+ /// Where-Bedingung entsprechen
+ ///
+ /// WhereBedingung als string
+ /// z.B. "NetPrice > @netPrice and Active = @active and Description like @desc
+ /// Parameter-Werte für die Wherebedingung
+ /// bspw: {{"netPrice", 10.5}, {"active", true}, {"desc", "Wolle%"}}
+ ///
+ long Count(string whereCondition, Dictionary parameterValues);
+
+ ///
+ /// Zählt alle Model-Objekte vom Typ M
+ ///
+ ///
+ long Count();
+
+ ///
+ /// Gibt den Tabellennamen zurück, auf die sich das Repository bezieht
+ ///
+ string TableName { get; }
+ }
+}
\ No newline at end of file
diff --git a/LoggingClient/LoggingClient/Repository/LocationRepository.cs b/LoggingClient/LoggingClient/Repository/LocationRepository.cs
new file mode 100755
index 0000000..7d153d1
--- /dev/null
+++ b/LoggingClient/LoggingClient/Repository/LocationRepository.cs
@@ -0,0 +1,199 @@
+using LoggingClient.Model;
+using MySql.Data.MySqlClient;
+using System;
+using System.Collections.Generic;
+using System.Windows;
+
+namespace LoggingClient.Repository
+{
+ public class LocationRepository : RepositoryBase
+ {
+ public override string TableName => "Location";
+ public override string ColumnsForSelect => "location_id, parentId, address_fk, designation, building, room";
+ public override string ColumnsForAdd => "parentId, address_fk, designation, building, room";
+ public override string PrimaryKeyFromTable => "location_id";
+
+ public List Locations { get; set; }
+ public Location _Locations { get; set; }
+ public LocationRepository(string connectionString) : base(connectionString)
+ {
+ Locations = new List();
+ }
+ public override void Add(Location location)
+ {
+ try
+ {
+ location.AddressId = 2;
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText =
+ $"INSERT INTO {TableName} ({ColumnsForAdd}) " +
+ $"VALUES " +
+ $"(parentId = {location.ParentId}, address_fk = {location.AddressId} , designation = '{location.Designation}', building = {location.BuildingNr} , room = {location.RoomNr} )";
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ }
+
+ public override void Delete(Location location)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"DELETE FROM {TableName} WHERE location_id = {location.Id}";
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ }
+
+ public override List GetAll(string whereCondition, Dictionary parameterValues)
+ {
+ var whereCon = whereCondition;
+ if (parameterValues.Count > 0 && whereCondition != null)
+ {
+ foreach (KeyValuePair p in parameterValues)
+ {
+ whereCon = whereCon.Replace($"@{p.Key}", p.Value.ToString());
+ }
+ }
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName} WHERE {whereCon}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ Locations.Add(new Location(
+
+ reader.GetInt32("location_id"),
+ reader.GetInt32("parentId"),
+ reader.GetInt32("address_fk"),
+ reader.GetValue(reader.GetOrdinal("designation")) as string,
+ reader.GetInt32("building"),
+ reader.GetInt32("room")
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return Locations;
+ }
+
+ public override List GetAll()
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ Locations.Add(new Location(
+
+ reader.GetInt32("location_id"),
+ reader.GetInt32("parentId"),
+ reader.GetInt32("address_fk"),
+ reader.GetValue(reader.GetOrdinal("designation")) as string,
+ reader.GetInt32("building"),
+ reader.GetInt32("room")
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return Locations;
+ }
+
+ public override void CallStoredProcedure(Location entity)
+ {
+ throw new System.NotSupportedException();
+ }
+
+ public override Location GetSingle(P pkValue)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName} WHERE {PrimaryKeyFromTable} = {pkValue}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ _Locations = (new Location(
+
+ reader.GetInt32("location_id"),
+ reader.GetInt32("parentId"),
+ reader.GetInt32("address_fk"),
+ reader.GetValue(reader.GetOrdinal("designation")) as string,
+ reader.GetInt32("building"),
+ reader.GetInt32("room")
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return _Locations;
+ }
+
+ public override void Update(Location location)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText =
+ $"UPDATE {TableName} SET address_fk = {location.Id} , designation = '{location.Designation}', building = {location.BuildingNr} , room = {location.RoomNr} WHERE location_id = {location.Id}";
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/LoggingClient/LoggingClient/Repository/LoggingRepository.cs b/LoggingClient/LoggingClient/Repository/LoggingRepository.cs
new file mode 100755
index 0000000..6da4e24
--- /dev/null
+++ b/LoggingClient/LoggingClient/Repository/LoggingRepository.cs
@@ -0,0 +1,192 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.Data;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using MySql.Data.MySqlClient;
+using LoggingClient.Model;
+
+namespace LoggingClient.Repository
+{
+ class LoggingRepository : RepositoryBase
+ {
+ public override string TableName => "v_logentries";
+ public override string ColumnsForSelect => "id, pod, location, hostname, severity, timestamp, message";
+ public override string ColumnsForAdd { get; }
+ public override string PrimaryKeyFromTable => "id";
+
+ public List Logs { get; set; }
+ public Logging _Logs { get; set; }
+
+ public LoggingRepository(string connectionString) : base(connectionString)
+ {
+ Logs = new List();
+ }
+
+ public override Logging GetSingle(P pkValue)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName} WHERE {PrimaryKeyFromTable} = {pkValue}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ _Logs = (new Logging(
+
+ reader.GetInt32("id"),
+ reader.GetValue(reader.GetOrdinal("pod")) as string,
+ reader.GetValue(reader.GetOrdinal("location")) as string,
+ reader.GetValue(reader.GetOrdinal("hostname")) as string,
+ reader.GetString("severity"),
+ reader.GetDateTime("timestamp"),
+ reader.GetValue(reader.GetOrdinal("message")) as string
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return _Logs;
+ }
+ public override void Add(Logging newLogModelEntry)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = "LogMessageAdd";
+ cmd.CommandType = CommandType.StoredProcedure;
+
+ cmd.Parameters.Add("@i_pod", MySqlDbType.String).Value = newLogModelEntry.Pod;
+ cmd.Parameters.Add("@i_hostname", MySqlDbType.String).Value = newLogModelEntry.Hostname;
+ cmd.Parameters.Add("@i_severity", MySqlDbType.Int32).Value = newLogModelEntry.Severity;
+ cmd.Parameters.Add("@i_message", MySqlDbType.String).Value = newLogModelEntry.Message;
+
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ }
+ public override void Delete(Logging entity)
+ {
+ throw new System.NotSupportedException();
+ }
+ public override void Update(Logging entity)
+ {
+ throw new System.NotSupportedException();
+ }
+ public override List GetAll(string whereCondition, Dictionary parameterValues)
+ {
+ var whereCon = whereCondition;
+ if (parameterValues.Count > 0 && whereCondition != null)
+ {
+ foreach (KeyValuePair p in parameterValues)
+ {
+ whereCon = whereCon.Replace($"@{p.Key}", p.Value.ToString());
+ }
+ }
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName} WHERE {whereCon}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ Logs.Add(new Logging(
+
+ reader.GetInt32("id"),
+ reader.GetValue(reader.GetOrdinal("pod")) as string,
+ reader.GetValue(reader.GetOrdinal("location")) as string,
+ reader.GetValue(reader.GetOrdinal("hostname")) as string,
+ reader.GetString("severity"),
+ reader.GetDateTime("timestamp"),
+ reader.GetValue(reader.GetOrdinal("message")) as string
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return Logs;
+ }
+ public override List GetAll()
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (MySqlCommand cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = $"SELECT {ColumnsForSelect} FROM {TableName}";
+ var reader = cmd.ExecuteReader();
+ while (reader.Read())
+ {
+ Logs.Add(new Logging(
+
+ reader.GetInt32("id"),
+ reader.GetValue(reader.GetOrdinal("pod")) as string,
+ reader.GetValue(reader.GetOrdinal("location")) as string,
+ reader.GetValue(reader.GetOrdinal("hostname")) as string,
+ reader.GetString("severity"),
+ reader.GetDateTime("timestamp"),
+ reader.GetValue(reader.GetOrdinal("message")) as string
+ ));
+ }
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ }
+ return Logs;
+ }
+ public override void CallStoredProcedure(Logging logModelEntry)
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(ConnectionString))
+ {
+ conn.Open();
+ using (var cmd = conn.CreateCommand())
+ {
+ cmd.CommandText = "LogClear";
+ cmd.CommandType = CommandType.StoredProcedure;
+ cmd.Parameters.AddWithValue("_logentries_id", logModelEntry.Id);
+ cmd.ExecuteNonQuery();
+ }
+ }
+ }
+ catch (MySqlException ex)
+ {
+ MessageBox.Show(ex.ToString());
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/LoggingClient/LoggingClient/Repository/RepositoryBase.cs b/LoggingClient/LoggingClient/Repository/RepositoryBase.cs
new file mode 100755
index 0000000..f2289f9
--- /dev/null
+++ b/LoggingClient/LoggingClient/Repository/RepositoryBase.cs
@@ -0,0 +1,86 @@
+using MySql.Data.MySqlClient;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Windows;
+
+namespace LoggingClient.Repository
+{
+ public abstract class RepositoryBase : IRepositoryBase
+ {
+ protected RepositoryBase(string connectionString)
+ {
+ this.ConnectionString = connectionString;
+ }
+ protected string ConnectionString { get; }
+ public abstract M GetSingle(P pkValue);
+
+ public abstract void Add(M entity);
+
+ public abstract void Delete(M entity);
+
+ public abstract void Update(M entity);
+ public abstract List GetAll(string whereCondition, Dictionary parameterValues);
+
+ public abstract List GetAll();
+
+ public abstract void CallStoredProcedure(M entity);
+
+ public IQueryable Query(string whereCondition, Dictionary parameterValues)
+ {
+ throw new System.NotSupportedException();
+ }
+ public long Count(string whereCondition, Dictionary parameterValues)
+ {
+ var whereCon = whereCondition;
+ if (parameterValues.Count > 0 && whereCondition != null)
+ {
+ foreach (KeyValuePair p in parameterValues)
+ {
+ whereCon = whereCon.Replace($"@{p.Key}", p.Value.ToString());
+ }
+ }
+ try
+ {
+ using (var conn = new MySqlConnection(this.ConnectionString))
+ {
+ using (var cmd = conn.CreateCommand())
+ {
+ conn.Open();
+ cmd.CommandText = $"select count(*) from {this.TableName} where {whereCon}";
+ return (long)cmd.ExecuteScalar();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ return -1;
+ }
+ }
+ public long Count()
+ {
+ try
+ {
+ using (var conn = new MySqlConnection(this.ConnectionString))
+ {
+ using (var cmd = conn.CreateCommand())
+ {
+ conn.Open();
+ cmd.CommandText = $"select count(*) from {this.TableName}";
+ return (long)cmd.ExecuteScalar();
+ }
+ }
+ }
+ catch (Exception ex)
+ {
+ MessageBox.Show("Error occurred: " + ex.Message);
+ return -1;
+ }
+ }
+ public abstract string TableName { get; }
+ public abstract string ColumnsForSelect { get; }
+ public abstract string ColumnsForAdd { get; }
+ public abstract string PrimaryKeyFromTable { get; }
+ }
+}
\ No newline at end of file
diff --git a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs
old mode 100644
new mode 100755
index 06470c9..487f8e2
--- a/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs
+++ b/LoggingClient/LoggingClient/Validators/IntRangeValidationRule.cs
@@ -10,21 +10,9 @@ namespace LoggingClient.Validators
{
public class IntRangeValidationRule : ValidationRule
{
- private int Min = 1;
- private int Max = 3;
-
- public int MinimumLength
- {
- get { return Min; }
- set { Min = value; }
- }
-
- public int MaximumLength
- {
- get { return Max; }
- set { Max = value; }
- }
-
+ public int MinimumLength { get; set; }
+ public int MaximumLength { get; set; }
+ public string ErrorMessage { get; set; }
public override ValidationResult Validate(object value,
CultureInfo cultureInfo)
{
@@ -43,11 +31,11 @@ namespace LoggingClient.Validators
+ e.Message);
}
- if ((parameter < this.Min) || (parameter > this.Max))
+ if ((parameter < this.MinimumLength) || (parameter > this.MaximumLength))
{
return new ValidationResult(false,
- "Severity must be a number between "
- + this.Min + " - " + this.Max + ".");
+ "Input must be a number between "
+ + this.MinimumLength + " - " + this.MaximumLength + ".");
}
return new ValidationResult(true, null);
}
diff --git a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs
old mode 100644
new mode 100755
index 098285c..2e285ff
--- a/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs
+++ b/LoggingClient/LoggingClient/Validators/StringRangeValidationRule.cs
@@ -10,20 +10,8 @@ namespace LoggingClient.Validators
{
public class StringRangeValidationRule : ValidationRule
{
- private int _minimumLength = 3;
- private int _maximumLength = 255;
-
- public int MinimumLength
- {
- get { return _minimumLength; }
- set { _minimumLength = value; }
- }
-
- public int MaximumLength
- {
- get { return _maximumLength; }
- set { _maximumLength = value; }
- }
+ public int MinimumLength { get; set; }
+ public int MaximumLength { get; set; }
public string ErrorMessage { get; set; }
@@ -41,4 +29,4 @@ namespace LoggingClient.Validators
return result;
}
}
-}
+}
\ No newline at end of file
diff --git a/LoggingClient/LoggingClient/ViewModel/Commands/BaseCommand.cs b/LoggingClient/LoggingClient/ViewModel/Commands/BaseCommand.cs
new file mode 100755
index 0000000..e394e5c
--- /dev/null
+++ b/LoggingClient/LoggingClient/ViewModel/Commands/BaseCommand.cs
@@ -0,0 +1,30 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Input;
+
+namespace WpfControlNugget.ViewModel.Commands
+{
+ public class BaseCommand : ICommand
+ {
+ private Action