added models and a database controller

This commit is contained in:
Daniel Schick 2023-04-12 12:08:40 +02:00
parent d68e92a464
commit aa0c1d81d9
16 changed files with 359 additions and 2 deletions

View File

@ -175,6 +175,8 @@ CREATE TABLE `role` (
`id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL DEFAULT '0' COMMENT 'unique role name',
`description` VARCHAR(255) NULL DEFAULT '0' COMMENT 'role description',
`created` DATETIME NULL DEFAULT current_timestamp(),
`modified` DATETIME NULL DEFAULT NULL ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name` (`name`)
)
@ -187,6 +189,8 @@ ENGINE=InnoDB
CREATE TABLE `securable` (
`id` INT(10) UNSIGNED NOT NULL,
`name` VARCHAR(50) NOT NULL DEFAULT '',
`created` DATETIME NULL DEFAULT current_timestamp(),
`modified` DATETIME NULL DEFAULT NULL ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
UNIQUE INDEX `name` (`name`)
)
@ -199,6 +203,8 @@ CREATE TABLE `user_role_map` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_id` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`role_id` INT(10) UNSIGNED NOT NULL DEFAULT 0,
`created` DATETIME NULL DEFAULT current_timestamp(),
`modified` DATETIME NULL DEFAULT NULL ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
INDEX `FK_USER_ROLE` (`user_id`),
INDEX `FK_ROLE_USER` (`role_id`),
@ -214,6 +220,8 @@ CREATE TABLE `role_securable_map` (
`id` INT(10) UNSIGNED NOT NULL,
`role_id` INT(10) UNSIGNED NOT NULL,
`securable_id` INT(10) UNSIGNED NOT NULL,
`created` DATETIME NULL DEFAULT current_timestamp(),
`modified` DATETIME NULL DEFAULT NULL ON UPDATE current_timestamp(),
PRIMARY KEY (`id`),
INDEX `FK_ROLE_SECURABLE` (`role_id`),
INDEX `FK_SECURABLE_ROLE` (`securable_id`),

View File

@ -5,7 +5,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:RoleEditor"
mc:Ignorable="d"
Title="Bremen calling admin editor" Height="600" Width="800" Icon="Resources/lock_preferences.ico">
Title="Bremen calling admin editor" Height="600" Width="800" Icon="Resources/lock_preferences.ico" Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=".5*" />

View File

@ -14,6 +14,8 @@ using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using brecal.model;
namespace RoleEditor
{
@ -22,11 +24,36 @@ namespace RoleEditor
/// </summary>
public partial class MainWindow : Window
{
#region private fields
private ObservableCollection<Participant> _participants = new ObservableCollection<Participant>();
private ObservableCollection<Role> _roles = new ObservableCollection<Role>();
private ObservableCollection<Securable> _securables = new ObservableCollection<Securable>();
private ObservableCollection<User> _users = new ObservableCollection<User>();
private ObservableCollection<Role> _assignedRoles = new ObservableCollection<Role>();
private ObservableCollection<Securable> _assignedSecurables = new ObservableCollection<Securable>();
#endregion
#region Construction / Loading
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// try database connection
// load all participants
// load all roles
}
#endregion
#region button callbacks
private void buttonParticipantSave_Click(object sender, RoutedEventArgs e)
@ -71,6 +98,8 @@ namespace RoleEditor
#endregion
#region listbox selection callbacks
private void listBoxParticipant_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
@ -91,6 +120,10 @@ namespace RoleEditor
}
#endregion
#region menuitem callbacks
private void menuItemDeleteParticipant_Click(object sender, RoutedEventArgs e)
{
@ -130,5 +163,8 @@ namespace RoleEditor
{
}
#endregion
}
}

View File

@ -21,6 +21,11 @@
<None Remove="Resources\safe.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\brecal.model\brecal.model.csproj" />
<ProjectReference Include="..\brecal.mysql\brecal.mysql.csproj" />
</ItemGroup>
<ItemGroup>
<Resource Include="Resources/lock_preferences.ico" />
</ItemGroup>

View File

@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RoleEditor", "RoleEditor.csproj", "{8A8CB0D3-7728-468E-AB11-E811BA5B5BC0}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RoleEditor", "RoleEditor.csproj", "{8A8CB0D3-7728-468E-AB11-E811BA5B5BC0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "brecal.model", "..\brecal.model\brecal.model.csproj", "{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "brecal.mysql", "..\brecal.mysql\brecal.mysql.csproj", "{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -15,6 +19,14 @@ Global
{8A8CB0D3-7728-468E-AB11-E811BA5B5BC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A8CB0D3-7728-468E-AB11-E811BA5B5BC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A8CB0D3-7728-468E-AB11-E811BA5B5BC0}.Release|Any CPU.Build.0 = Release|Any CPU
{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F3BC5ADC-BF57-47DC-A5D5-CC4A13857DEE}.Release|Any CPU.Build.0 = Release|Any CPU
{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E88F908B-48C9-46BD-A3AE-C36FBE9EDF1F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class DbEntity
{
/// <summary>
/// DB primary key
/// </summary>
public uint Id { get; set; }
/// <summary>
/// Creation timestamp, if null record is unsaved
/// </summary>
public DateTime? Created { get; set; }
/// <summary>
/// Modified timestamp, if null record was never modified
/// </summary>
public DateTime? Modified { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public interface IDBManager
{
void Load(Action<IDbCommand> prepareAction, Action<IDataReader> loadAction);
Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction);
Task<int> ExecuteNonQuery(Action<IDbCommand> prepareAction);
}
}

View File

@ -0,0 +1,35 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class Participant : DbEntity
{
#region Properties
public string? Name { get; set; }
public string? Street { get; set; }
public string? PostalCode { get; set; }
public string? City { get; set; }
public uint Flags { get; set; }
#endregion
#region overrides
public override string ToString()
{
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
}
#endregion
}
}

30
src/brecal.model/Role.cs Normal file
View File

@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class Role : DbEntity
{
#region Properties
public string? Name { get; set; }
public string? Description { get; set; }
#endregion
#region overrides
public override string ToString()
{
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
}
#endregion
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class RoleAssignment : DbEntity
{
#region Properties
public Role? AssignedRole { get; set; }
public User? AssignedUser { get; set; }
#endregion
}
}

View File

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class Securable : DbEntity
{
#region Properties
public string? Name { get; set; }
#endregion
#region overrides
public override string ToString()
{
return this.Name ?? $"{base.Id} - {this.GetType().Name}";
}
#endregion
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace brecal.model
{
public class SecurableAssignment : DbEntity
{
#region Properties
public Role? AssignedRole { get; set; }
public Securable? AssignedSecurable { get; set; }
#endregion
}
}

29
src/brecal.model/User.cs Normal file
View File

@ -0,0 +1,29 @@
namespace brecal.model
{
public class User : DbEntity
{
#region Properties
public string? Firstname { get; set; }
public string? Lastname { get; set; }
public string Username { get; set; } = "";
public string? PasswordHash { get; set; }
public string? APIKey { get; set; }
#endregion
#region overrides
public override string ToString()
{
return this.Username ?? $"{base.Id} - {this.GetType().Name}";
}
#endregion
}
}

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,61 @@
using brecal.model;
using MySqlConnector;
using System.Data;
using System.Runtime.CompilerServices;
namespace brecal.mysql
{
public class DBManager : IDBManager
{
private static string _connectionString = "";
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 connection.OpenAsync();
using MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
prepareAction(cmd);
MySqlDataReader reader = await cmd.ExecuteReaderAsync();
loadAction(reader);
reader.Close();
}
public async Task<object?> ExecuteScalar(Action<IDbCommand> prepareAction)
{
await using MySqlConnection connection = new MySqlConnection(_connectionString);
await connection.OpenAsync();
using MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
prepareAction(cmd);
object? result = await cmd.ExecuteScalarAsync();
return result;
}
public async Task<int> ExecuteNonQuery(Action<IDbCommand> prepareAction)
{
await using MySqlConnection connection = new MySqlConnection(_connectionString);
await connection.OpenAsync();
using MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connection;
prepareAction(cmd);
int result = await cmd.ExecuteNonQueryAsync();
return result;
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MySqlConnector" Version="2.3.0-beta.1" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\brecal.model\brecal.model.csproj" />
</ItemGroup>
</Project>