Speicher-Logik erweitern
This commit is contained in:
parent
aa0c1d81d9
commit
bbb986260c
@ -16,6 +16,7 @@ using System.Windows.Navigation;
|
|||||||
using System.Windows.Shapes;
|
using System.Windows.Shapes;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using brecal.model;
|
using brecal.model;
|
||||||
|
using brecal.mysql;
|
||||||
|
|
||||||
namespace RoleEditor
|
namespace RoleEditor
|
||||||
{
|
{
|
||||||
@ -26,12 +27,13 @@ namespace RoleEditor
|
|||||||
{
|
{
|
||||||
#region private fields
|
#region private fields
|
||||||
|
|
||||||
private ObservableCollection<Participant> _participants = new ObservableCollection<Participant>();
|
private readonly ObservableCollection<Participant> _participants = new ObservableCollection<Participant>();
|
||||||
private ObservableCollection<Role> _roles = new ObservableCollection<Role>();
|
private readonly ObservableCollection<Role> _roles = new ObservableCollection<Role>();
|
||||||
private ObservableCollection<Securable> _securables = new ObservableCollection<Securable>();
|
private readonly ObservableCollection<Securable> _securables = new ObservableCollection<Securable>();
|
||||||
private ObservableCollection<User> _users = new ObservableCollection<User>();
|
private readonly ObservableCollection<User> _users = new ObservableCollection<User>();
|
||||||
private ObservableCollection<Role> _assignedRoles = new ObservableCollection<Role>();
|
private readonly ObservableCollection<Role> _assignedRoles = new ObservableCollection<Role>();
|
||||||
private ObservableCollection<Securable> _assignedSecurables = new ObservableCollection<Securable>();
|
private readonly ObservableCollection<Securable> _assignedSecurables = new ObservableCollection<Securable>();
|
||||||
|
private DBManager? _dbManager;
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -45,11 +47,20 @@ namespace RoleEditor
|
|||||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
// try database connection
|
// try database connection
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_dbManager = new();
|
||||||
|
|
||||||
// load all participants
|
// load all participants
|
||||||
|
|
||||||
// load all roles
|
|
||||||
|
|
||||||
|
// load all roles
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
MessageBox.Show($"Database connection couldn't be established: {ex.Message}");
|
||||||
|
this.Close();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace brecal.model
|
namespace brecal.model
|
||||||
{
|
{
|
||||||
public class DbEntity
|
public abstract class DbEntity
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// DB primary key
|
/// DB primary key
|
||||||
@ -23,5 +23,14 @@ namespace brecal.model
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public DateTime? Modified { get; set; }
|
public DateTime? Modified { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Each database entity must be able to save itself to the database
|
||||||
|
/// </summary>
|
||||||
|
public abstract void Save(IDBManager manager);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Each entity must be able to delete itself
|
||||||
|
/// </summary>
|
||||||
|
public abstract void Delete(IDBManager manager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,7 +10,9 @@ namespace brecal.model
|
|||||||
public interface IDBManager
|
public interface IDBManager
|
||||||
{
|
{
|
||||||
|
|
||||||
void Load(Action<IDbCommand> prepareAction, Action<IDataReader> loadAction);
|
delegate List<DbEntity> LoadFunc<T>(T entity);
|
||||||
|
|
||||||
|
Task<List<DbEntity>> Load(Action<IDbCommand> prepareAction, LoadFunc<IDataReader> loadAction);
|
||||||
|
|
||||||
Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction);
|
Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction);
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Data;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@ -22,6 +23,126 @@ namespace brecal.model
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
#region abstract method implementation
|
||||||
|
|
||||||
|
// TODO: Diese Funktionen in die Basisklasse verschieben und die SetUpdate SetCreate SetDelete
|
||||||
|
// abstract machen
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
if (this.Created.HasValue)
|
||||||
|
await manager.ExecuteNonQuery(this.SetUpdate);
|
||||||
|
else
|
||||||
|
await manager.ExecuteNonQuery(this.SetCreate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
await manager.ExecuteNonQuery(this.SetDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region public static methods
|
||||||
|
|
||||||
|
public static async Task<List<Participant>> LoadAll(IDBManager manager)
|
||||||
|
{
|
||||||
|
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems);
|
||||||
|
List<Participant> result = new List<Participant>();
|
||||||
|
foreach (Participant p in loadResultList.Cast<Participant>())
|
||||||
|
result.Add(p);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region parameter funcs
|
||||||
|
|
||||||
|
public static void SetLoadQuery(IDbCommand cmd)
|
||||||
|
{
|
||||||
|
cmd.CommandText = "SELECT id, name, street, postal_code, city, flags, created, modified FROM participant";
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetUpdate(IDbCommand cmd)
|
||||||
|
{
|
||||||
|
cmd.CommandText = "UPDATE participant set name = @NAME, street = @STREET, postal_code = @POSTAL_CODE, city = @CITY, flags = @FLAGS WHERE id = @ID";
|
||||||
|
this.SetParameters(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetCreate(IDbCommand cmd)
|
||||||
|
{
|
||||||
|
cmd.CommandText = "INSERT INTO participant (name, street, postal_code, city, flags) VALUES ( @NAME, @STREET, @POSTAL_CODE, @CITY, @FLAGS)";
|
||||||
|
this.SetParameters(cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void SetDelete(IDbCommand cmd)
|
||||||
|
{
|
||||||
|
cmd.CommandText = "DELETE FROM participant WHERE id = @ID";
|
||||||
|
|
||||||
|
IDataParameter idParam = cmd.CreateParameter();
|
||||||
|
idParam.ParameterName = "ID";
|
||||||
|
idParam.Value = this.Id;
|
||||||
|
cmd.Parameters.Add(idParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<DbEntity> LoadElems(IDataReader reader)
|
||||||
|
{
|
||||||
|
List<DbEntity> result = new List<DbEntity>();
|
||||||
|
while(reader.Read())
|
||||||
|
{
|
||||||
|
Participant p = new();
|
||||||
|
p.Id = (uint) reader.GetInt32(0);
|
||||||
|
if(!reader.IsDBNull(1)) p.Name = reader.GetString(1);
|
||||||
|
if(!reader.IsDBNull(2)) p.Street = reader.GetString(2);
|
||||||
|
if (!reader.IsDBNull(3)) p.PostalCode = reader.GetString(3);
|
||||||
|
if (!reader.IsDBNull(4)) p.City = reader.GetString(4);
|
||||||
|
if(!reader.IsDBNull(5)) p.Flags = (uint) reader.GetInt32(5);
|
||||||
|
if (!reader.IsDBNull(6)) p.Created = reader.GetDateTime(6);
|
||||||
|
if(!reader.IsDBNull(7)) p.Modified = reader.GetDateTime(7);
|
||||||
|
result.Add(p);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region private methods
|
||||||
|
|
||||||
|
private void SetParameters(IDbCommand cmd)
|
||||||
|
{
|
||||||
|
IDbDataParameter name = cmd.CreateParameter();
|
||||||
|
name.ParameterName = "NAME";
|
||||||
|
name.Value = this.Name;
|
||||||
|
cmd.Parameters.Add(name);
|
||||||
|
|
||||||
|
IDbDataParameter street = cmd.CreateParameter();
|
||||||
|
street.ParameterName = "STREET";
|
||||||
|
street.Value = this.Street;
|
||||||
|
cmd.Parameters.Add(street);
|
||||||
|
|
||||||
|
IDataParameter postal_code = cmd.CreateParameter();
|
||||||
|
postal_code.ParameterName = "POSTAL_CODE";
|
||||||
|
postal_code.Value = this.PostalCode;
|
||||||
|
cmd.Parameters.Add(postal_code);
|
||||||
|
|
||||||
|
IDataParameter city = cmd.CreateParameter();
|
||||||
|
city.ParameterName = "CITY";
|
||||||
|
city.Value = this.City;
|
||||||
|
cmd.Parameters.Add(city);
|
||||||
|
|
||||||
|
IDataParameter flags = cmd.CreateParameter();
|
||||||
|
flags.ParameterName = "FLAGS";
|
||||||
|
flags.Value = this.Flags;
|
||||||
|
cmd.Parameters.Add(flags);
|
||||||
|
|
||||||
|
IDataParameter idParam = cmd.CreateParameter();
|
||||||
|
idParam.ParameterName = "ID";
|
||||||
|
idParam.Value = this.Id;
|
||||||
|
cmd.Parameters.Add(idParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
#region overrides
|
#region overrides
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
|||||||
@ -19,6 +19,16 @@ namespace brecal.model
|
|||||||
|
|
||||||
#region overrides
|
#region overrides
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
|
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
|
||||||
|
|||||||
@ -17,5 +17,15 @@ namespace brecal.model
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,6 +17,16 @@ namespace brecal.model
|
|||||||
|
|
||||||
#region overrides
|
#region overrides
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
|
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
|
||||||
|
|||||||
@ -15,5 +15,16 @@ namespace brecal.model
|
|||||||
public Securable? AssignedSecurable { get; set; }
|
public Securable? AssignedSecurable { get; set; }
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -18,6 +18,16 @@
|
|||||||
|
|
||||||
#region overrides
|
#region overrides
|
||||||
|
|
||||||
|
public async override void Save(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public async override void Delete(IDBManager manager)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return this.Username ?? $"{base.Id} - {this.GetType().Name}";
|
return this.Username ?? $"{base.Id} - {this.GetType().Name}";
|
||||||
|
|||||||
@ -2,35 +2,26 @@
|
|||||||
using MySqlConnector;
|
using MySqlConnector;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
using System.Runtime.CompilerServices;
|
using System.Runtime.CompilerServices;
|
||||||
|
using static brecal.model.IDBManager;
|
||||||
|
|
||||||
namespace brecal.mysql
|
namespace brecal.mysql
|
||||||
{
|
{
|
||||||
public class DBManager : IDBManager
|
public class DBManager : IDBManager
|
||||||
{
|
{
|
||||||
private static string _connectionString = "";
|
// TODO: remove this and use certificates instead
|
||||||
|
private static readonly string _connectionString = "Server=localhost;User ID=ds;Password=HalloWach23;Database=bremen_calling";
|
||||||
|
|
||||||
|
public async Task<List<DbEntity>> Load(Action<IDbCommand> prepareAction, LoadFunc<IDataReader> loadAction)
|
||||||
public async void Generic()
|
|
||||||
{
|
|
||||||
await using var connection = new MySqlConnection("Server=myserver;User ID=mylogin;Password=mypass;Database=mydatabase");
|
|
||||||
await connection.OpenAsync();
|
|
||||||
|
|
||||||
using var command = new MySqlCommand("SELECT field FROM table;", connection);
|
|
||||||
await using var reader = await command.ExecuteReaderAsync();
|
|
||||||
while (await reader.ReadAsync())
|
|
||||||
Console.WriteLine(reader.GetString(0));
|
|
||||||
}
|
|
||||||
|
|
||||||
public async void Load(Action<IDbCommand> prepareAction, Action<IDataReader> loadAction)
|
|
||||||
{
|
{
|
||||||
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
||||||
await connection.OpenAsync();
|
await connection.OpenAsync();
|
||||||
using MySqlCommand cmd = new MySqlCommand();
|
using MySqlCommand cmd = new();
|
||||||
cmd.Connection = connection;
|
cmd.Connection = connection;
|
||||||
prepareAction(cmd);
|
prepareAction(cmd);
|
||||||
MySqlDataReader reader = await cmd.ExecuteReaderAsync();
|
MySqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||||
loadAction(reader);
|
List<DbEntity> result = loadAction(reader);
|
||||||
reader.Close();
|
reader.Close();
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction)
|
public async Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction)
|
||||||
@ -38,7 +29,7 @@ namespace brecal.mysql
|
|||||||
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
||||||
await connection.OpenAsync();
|
await connection.OpenAsync();
|
||||||
|
|
||||||
using MySqlCommand cmd = new MySqlCommand();
|
using MySqlCommand cmd = new();
|
||||||
cmd.Connection = connection;
|
cmd.Connection = connection;
|
||||||
prepareAction(cmd);
|
prepareAction(cmd);
|
||||||
object? result = await cmd.ExecuteScalarAsync();
|
object? result = await cmd.ExecuteScalarAsync();
|
||||||
@ -47,10 +38,10 @@ namespace brecal.mysql
|
|||||||
|
|
||||||
public async Task<int> ExecuteNonQuery(Action<IDbCommand> prepareAction)
|
public async Task<int> ExecuteNonQuery(Action<IDbCommand> prepareAction)
|
||||||
{
|
{
|
||||||
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
await using MySqlConnection connection = new(_connectionString);
|
||||||
await connection.OpenAsync();
|
await connection.OpenAsync();
|
||||||
|
|
||||||
using MySqlCommand cmd = new MySqlCommand();
|
using MySqlCommand cmd = new();
|
||||||
cmd.Connection = connection;
|
cmd.Connection = connection;
|
||||||
prepareAction(cmd);
|
prepareAction(cmd);
|
||||||
int result = await cmd.ExecuteNonQueryAsync();
|
int result = await cmd.ExecuteNonQueryAsync();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user