Loading of participant and user functional
This commit is contained in:
parent
bbb986260c
commit
a01b88e0c9
@ -33,7 +33,7 @@ namespace RoleEditor
|
||||
private readonly ObservableCollection<User> _users = new ObservableCollection<User>();
|
||||
private readonly ObservableCollection<Role> _assignedRoles = new ObservableCollection<Role>();
|
||||
private readonly ObservableCollection<Securable> _assignedSecurables = new ObservableCollection<Securable>();
|
||||
private DBManager? _dbManager;
|
||||
private DBManager _dbManager;
|
||||
|
||||
#endregion
|
||||
|
||||
@ -42,19 +42,29 @@ namespace RoleEditor
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
_dbManager = new();
|
||||
}
|
||||
|
||||
private void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
private async void Window_Loaded(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// try database connection
|
||||
try
|
||||
{
|
||||
_dbManager = new();
|
||||
|
||||
{
|
||||
// load all participants
|
||||
|
||||
foreach(Participant p in await Participant.LoadAll(_dbManager))
|
||||
_participants.Add(p);
|
||||
this.listBoxParticipant.ItemsSource = _participants;
|
||||
|
||||
// load all roles
|
||||
foreach(Role r in await Role.LoadAll(_dbManager))
|
||||
_roles.Add(r);
|
||||
this.listBoxRoles.ItemsSource = _roles;
|
||||
|
||||
// set other item sources
|
||||
this.listBoxUser.ItemsSource = _users;
|
||||
this.listBoxSecurables.ItemsSource = _securables;
|
||||
this.listBoxRoleSecurables.ItemsSource = _assignedSecurables;
|
||||
this.listBoxUserRoles.ItemsSource = _assignedRoles;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@ -111,9 +121,22 @@ namespace RoleEditor
|
||||
|
||||
#region listbox selection callbacks
|
||||
|
||||
private void listBoxParticipant_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
private async void listBoxParticipant_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
Participant? p = this.listBoxParticipant.SelectedItem as Participant;
|
||||
|
||||
this.textBoxParticipantName.Text = (p != null) ? p.Name : string.Empty;
|
||||
this.textBoxParticipantStreet.Text = (p != null) ? p.Street : string.Empty;
|
||||
this.textBoxParticipantPostalCode.Text = (p != null) ? p.PostalCode : string.Empty;
|
||||
this.textBoxParticipantCity.Text = (p != null) ? p.City : string.Empty;
|
||||
// this.checkboxParticipantActive.Checked = (p != null) ? p.
|
||||
this.textBoxParticipantCreated.Text = (p != null) ? p.Created.ToString() : string.Empty;
|
||||
this.textBoxParticipantModified.Text = (p != null) ? p.Modified.ToString() : string.Empty;
|
||||
|
||||
// -> load users for this participant selection
|
||||
this._users.Clear();
|
||||
foreach (User u in await User.LoadForParticipant(p, _dbManager))
|
||||
_users.Add(u);
|
||||
}
|
||||
|
||||
private void listBoxRoles_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
@ -123,6 +146,13 @@ namespace RoleEditor
|
||||
|
||||
private void listBoxUser_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
||||
{
|
||||
User? u = this.listBoxUser.SelectedItem as User;
|
||||
this.textBoxUserFirstName.Text = (u != null) ? u.Firstname : string.Empty;
|
||||
this.textBoxUserLastName.Text = (u != null) ? u.Lastname : string.Empty;
|
||||
this.textBoxUserUserName.Text = (u != null) ? u.Username : string.Empty;
|
||||
this.textBoxUserAPIKey.Text = (u != null) ? u.APIKey : string.Empty;
|
||||
this.textBoxUserCreated.Text = (u != null) ? u.Created.ToString() : string.Empty;
|
||||
this.textBoxUserModified.Text = (u != null) ? u.Modified.ToString() : string.Empty;
|
||||
|
||||
}
|
||||
|
||||
@ -147,7 +177,14 @@ namespace RoleEditor
|
||||
|
||||
private void menuItemNewUser_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
Participant? p = this.listBoxParticipant.SelectedItem as Participant;
|
||||
if(p != null)
|
||||
{
|
||||
User u = new();
|
||||
u.Participant_Id = p.Id;
|
||||
_users.Add(u);
|
||||
this.listBoxUser.SelectedItem = u;
|
||||
}
|
||||
}
|
||||
|
||||
private void menuItemDeleteUser_Click(object sender, RoutedEventArgs e)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -23,14 +24,42 @@ namespace brecal.model
|
||||
/// </summary>
|
||||
public DateTime? Modified { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Set query and cmd parameters for an update query
|
||||
/// </summary>
|
||||
/// <param name="cmd">CMD created by DB manager</param>
|
||||
public abstract void SetUpdate(IDbCommand cmd);
|
||||
|
||||
/// <summary>
|
||||
/// set query and cmd parameters for a create query
|
||||
/// </summary>
|
||||
/// <param name="cmd">CMD created by DB manager</param>
|
||||
public abstract void SetCreate(IDbCommand cmd);
|
||||
|
||||
/// <summary>
|
||||
/// set query and cmd parameters for a delete query
|
||||
/// </summary>
|
||||
/// <param name="cmd">CMD created by DB manager</param>
|
||||
public abstract void SetDelete(IDbCommand cmd);
|
||||
|
||||
/// <summary>
|
||||
/// Each database entity must be able to save itself to the database
|
||||
/// </summary>
|
||||
public abstract void Save(IDBManager manager);
|
||||
public async void Save(IDBManager manager)
|
||||
{
|
||||
if (this.Created.HasValue)
|
||||
await manager.ExecuteNonQuery(this.SetUpdate);
|
||||
else
|
||||
await manager.ExecuteNonQuery(this.SetCreate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Each entity must be able to delete itself
|
||||
/// </summary>
|
||||
public abstract void Delete(IDBManager manager);
|
||||
public async void Delete(IDBManager manager)
|
||||
{
|
||||
await manager.ExecuteNonQuery(this.SetDelete);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -12,7 +12,9 @@ namespace brecal.model
|
||||
|
||||
delegate List<DbEntity> LoadFunc<T>(T entity);
|
||||
|
||||
Task<List<DbEntity>> Load(Action<IDbCommand> prepareAction, LoadFunc<IDataReader> loadAction);
|
||||
delegate void QueryFunc(IDbCommand cmd, params object[] args);
|
||||
|
||||
Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object[] args);
|
||||
|
||||
Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction);
|
||||
|
||||
|
||||
@ -21,61 +21,60 @@ namespace brecal.model
|
||||
|
||||
public uint Flags { get; set; }
|
||||
|
||||
#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
|
||||
#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>();
|
||||
List<Participant> result = new();
|
||||
foreach (Participant p in loadResultList.Cast<Participant>())
|
||||
result.Add(p);
|
||||
return result;
|
||||
}
|
||||
|
||||
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 parameter funcs
|
||||
|
||||
public static void SetLoadQuery(IDbCommand cmd)
|
||||
public static void SetLoadQuery(IDbCommand cmd, params object[] list)
|
||||
{
|
||||
cmd.CommandText = "SELECT id, name, street, postal_code, city, flags, created, modified FROM participant";
|
||||
}
|
||||
|
||||
public void SetUpdate(IDbCommand cmd)
|
||||
#endregion
|
||||
|
||||
#region abstract method implementation
|
||||
|
||||
public override 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)
|
||||
public override 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)
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
cmd.CommandText = "DELETE FROM participant WHERE id = @ID";
|
||||
|
||||
@ -83,26 +82,7 @@ namespace brecal.model
|
||||
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
|
||||
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -17,16 +18,62 @@ namespace brecal.model
|
||||
|
||||
#endregion
|
||||
|
||||
#region overrides
|
||||
#region public static methods
|
||||
|
||||
public async override void Save(IDBManager manager)
|
||||
public static async Task<List<Role>> LoadAll(IDBManager manager)
|
||||
{
|
||||
|
||||
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems);
|
||||
List<Role> result = new();
|
||||
foreach (Role r in loadResultList.Cast<Role>())
|
||||
result.Add(r);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async override void Delete(IDBManager manager)
|
||||
public static void SetLoadQuery(IDbCommand cmd, params object[] list)
|
||||
{
|
||||
cmd.CommandText = "SELECT id, name, description, created, modified FROM role";
|
||||
}
|
||||
|
||||
public static List<DbEntity> LoadElems(IDataReader reader)
|
||||
{
|
||||
List<DbEntity> result = new List<DbEntity>();
|
||||
while (reader.Read())
|
||||
{
|
||||
Role r = new();
|
||||
r.Id = (uint)reader.GetInt32(0);
|
||||
if (!reader.IsDBNull(1)) r.Name = reader.GetString(1);
|
||||
if (!reader.IsDBNull(2)) r.Description = reader.GetString(2);
|
||||
if (!reader.IsDBNull(3)) r.Created = reader.GetDateTime(3);
|
||||
if (!reader.IsDBNull(4)) r.Modified = reader.GetDateTime(4);
|
||||
result.Add(r);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region overrides
|
||||
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
cmd.CommandText = "UPDATE role set name = @NAME, description = @DESC WHERE id = @ID";
|
||||
this.SetParameters(cmd);
|
||||
}
|
||||
|
||||
public override void SetCreate(IDbCommand cmd)
|
||||
{
|
||||
cmd.CommandText = "INSERT INTO role (name, description) VALUES ( @NAME, @DESC)";
|
||||
this.SetParameters(cmd);
|
||||
}
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
cmd.CommandText = "DELETE FROM role WHERE id = @ID";
|
||||
|
||||
IDataParameter idParam = cmd.CreateParameter();
|
||||
idParam.ParameterName = "ID";
|
||||
idParam.Value = this.Id;
|
||||
cmd.Parameters.Add(idParam);
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
@ -36,5 +83,27 @@ namespace brecal.model
|
||||
|
||||
#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 desc = cmd.CreateParameter();
|
||||
desc.ParameterName = "DESC";
|
||||
desc.Value = this.Description;
|
||||
cmd.Parameters.Add(desc);
|
||||
|
||||
IDataParameter idParam = cmd.CreateParameter();
|
||||
idParam.ParameterName = "ID";
|
||||
idParam.Value = this.Id;
|
||||
cmd.Parameters.Add(idParam);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -17,14 +18,19 @@ namespace brecal.model
|
||||
|
||||
#endregion
|
||||
|
||||
public async override void Save(IDBManager manager)
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async override void Delete(IDBManager manager)
|
||||
public override void SetCreate(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -17,14 +18,19 @@ namespace brecal.model
|
||||
|
||||
#region overrides
|
||||
|
||||
public async override void Save(IDBManager manager)
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async override void Delete(IDBManager manager)
|
||||
public override void SetCreate(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@ -16,14 +17,19 @@ namespace brecal.model
|
||||
|
||||
#endregion
|
||||
|
||||
public async override void Save(IDBManager manager)
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async override void Delete(IDBManager manager)
|
||||
public override void SetCreate(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,4 +1,6 @@
|
||||
namespace brecal.model
|
||||
using System.Data;
|
||||
|
||||
namespace brecal.model
|
||||
{
|
||||
public class User : DbEntity
|
||||
{
|
||||
@ -14,18 +16,67 @@
|
||||
|
||||
public string? APIKey { get; set; }
|
||||
|
||||
public uint? Participant_Id { get; set; }
|
||||
|
||||
#endregion
|
||||
|
||||
#region public static methods
|
||||
|
||||
public static async Task<List<User>> LoadForParticipant(Participant? p, IDBManager manager)
|
||||
{
|
||||
List<DbEntity> loadResultList = await manager.Load(SetLoadQuery, LoadElems, args: p);
|
||||
List<User> result = new();
|
||||
foreach (User u in loadResultList.Cast<User>())
|
||||
result.Add(u);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static void SetLoadQuery(IDbCommand cmd, params object[] args)
|
||||
{
|
||||
cmd.CommandText = "SELECT id, first_name, last_name, user_name, api_key, created, modified FROM user WHERE participant_id = @PID";
|
||||
if (args.Length != 1 || !(args[0] is Participant))
|
||||
throw new ArgumentException("loader needs single partipant as argument");
|
||||
IDataParameter pid = cmd.CreateParameter();
|
||||
pid.ParameterName = "PID";
|
||||
pid.Value = ((Participant)args[0]).Id;
|
||||
cmd.Parameters.Add(pid);
|
||||
}
|
||||
|
||||
public static List<DbEntity> LoadElems(IDataReader reader)
|
||||
{
|
||||
List<DbEntity> result = new List<DbEntity>();
|
||||
while (reader.Read())
|
||||
{
|
||||
User u = new();
|
||||
u.Id = (uint)reader.GetInt32(0);
|
||||
if (!reader.IsDBNull(1)) u.Firstname = reader.GetString(1);
|
||||
if (!reader.IsDBNull(2)) u.Lastname = reader.GetString(2);
|
||||
if (!reader.IsDBNull(3)) u.Username = reader.GetString(3);
|
||||
if (!reader.IsDBNull(4)) u.APIKey = reader.GetString(4);
|
||||
if (!reader.IsDBNull(5)) u.Created = reader.GetDateTime(5);
|
||||
if (!reader.IsDBNull(6)) u.Modified = reader.GetDateTime(6);
|
||||
result.Add(u);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region overrides
|
||||
|
||||
public async override void Save(IDBManager manager)
|
||||
public override void SetUpdate(IDbCommand cmd)
|
||||
{
|
||||
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public async override void Delete(IDBManager manager)
|
||||
public override void SetCreate(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override void SetDelete(IDbCommand cmd)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
@ -33,6 +84,8 @@
|
||||
return this.Username ?? $"{base.Id} - {this.GetType().Name}";
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
|
||||
@ -11,13 +11,13 @@ namespace brecal.mysql
|
||||
// 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 Task<List<DbEntity>> Load(QueryFunc prepareAction, LoadFunc<IDataReader> loadAction, params object[] args)
|
||||
{
|
||||
await using MySqlConnection connection = new MySqlConnection(_connectionString);
|
||||
await connection.OpenAsync();
|
||||
using MySqlCommand cmd = new();
|
||||
cmd.Connection = connection;
|
||||
prepareAction(cmd);
|
||||
prepareAction(cmd, args);
|
||||
MySqlDataReader reader = await cmd.ExecuteReaderAsync();
|
||||
List<DbEntity> result = loadAction(reader);
|
||||
reader.Close();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user