From 342c6dfcb25a0cef6125eb2d2b1c9fbc50962c07 Mon Sep 17 00:00:00 2001 From: Schedel Pascal Date: Mon, 8 Jul 2024 09:38:35 +0200 Subject: [PATCH] more extension methods --- OwnChar/Data/Managers/DefaultDataManager.cs | 32 ++++++++++----------- OwnChar/Extensions.cs | 24 ++++++++++++++++ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/OwnChar/Data/Managers/DefaultDataManager.cs b/OwnChar/Data/Managers/DefaultDataManager.cs index 2e1257e..0e4853c 100644 --- a/OwnChar/Data/Managers/DefaultDataManager.cs +++ b/OwnChar/Data/Managers/DefaultDataManager.cs @@ -41,38 +41,38 @@ public class DefaultDataManager : IDataManager protected virtual bool HandleGet(OnActionEventArgs e) { - if (e.Action != DataManagerActions.Get) + if (!e.Is(DataManagerActions.Get)) return false; // Character - if (e.Action == DataManagerActions.Getter.Character) + if (e.Is(DataManagerActions.Getter.Character)) { // Get - if (e.ActionType == DataManagerActionType.Set) + if (e.Is(DataManagerActionType.Set)) { - if (e.Object is Group group && e.Parameters.GetAt(0, out string? name)) + if (e.Object is Group group && e.GetParam(0, out string? name)) return e.SetResult(CreateCharacter(e.CurrentUser, name, group)); } } // Group - if (e.Action == DataManagerActions.Getter.Group) + if (e.Is(DataManagerActions.Getter.Group)) { // Get - if (e.ActionType == DataManagerActionType.Get) + if (e.Is(DataManagerActionType.Get)) { - if (e.Parameters.GetAt(0, out string? name)) + if (e.GetParam(0, out string? name)) return e.SetResult(CreateGroup(e.CurrentUser, name)); } } // User - if (e.Action == DataManagerActions.Getter.UserAccount) + if (e.Is(DataManagerActions.Getter.UserAccount)) { // Get - if (e.ActionType == DataManagerActionType.Get) + if (e.Is(DataManagerActionType.Get)) { - if (e.Parameters.GetAt(0, out string? username) && e.Parameters.GetAt(1, out string? password)) + if (e.GetParam(0, out string? username) && e.GetParam(1, out string? password)) return e.SetResult(CreateUserAccount(username, password)); } @@ -86,7 +86,7 @@ public class DefaultDataManager : IDataManager protected virtual bool HandleSave(OnActionEventArgs e) { - if (e.Action != DataManagerActions.Save) + if (!e.Is(DataManagerActions.Save)) return false; // ... @@ -96,7 +96,7 @@ public class DefaultDataManager : IDataManager protected virtual bool HandleDelete(OnActionEventArgs e) { - if (e.Action != DataManagerActions.Delete) + if (!e.Is(DataManagerActions.Delete)) return false; // Character @@ -116,10 +116,10 @@ public class DefaultDataManager : IDataManager protected virtual bool HandleAssociation(OnActionEventArgs e) { - if (e.Action != DataManagerActions.Associate) + if (!e.Is(DataManagerActions.Associate)) return false; - if (e.Action == DataManagerActions.Association.Owner) + if (e.Is(DataManagerActions.Association.Owner)) { if (e.Object is Group group) { @@ -128,7 +128,7 @@ public class DefaultDataManager : IDataManager case DataManagerActionType.Get: return e.SetResultT(DataProvider.GetOwner(group)); case DataManagerActionType.Set: - if (e.Parameters.Length >= 1 && e.Parameters[0] is UserProfile profile) + if (e.GetParam(0, out UserProfile? profile)) return DataProvider.SetOwner(group, profile); break; } @@ -141,7 +141,7 @@ public class DefaultDataManager : IDataManager case DataManagerActionType.Get: return e.SetResultT(DataProvider.GetOwner(character)); case DataManagerActionType.Set: - if (e.Parameters.Length >= 1 && e.Parameters[0] is UserProfile profile) + if (e.GetParam(0, out UserProfile? profile)) return DataProvider.SetOwner(character, profile); break; } diff --git a/OwnChar/Extensions.cs b/OwnChar/Extensions.cs index f3848d4..5e5b7a0 100644 --- a/OwnChar/Extensions.cs +++ b/OwnChar/Extensions.cs @@ -22,6 +22,8 @@ public static class Extensions return account.Type >= permissions; } + #region OnActionEventArgs + public static bool SetResult(this OnActionEventArgs @this, object? data) { @this.Result = data; @@ -33,4 +35,26 @@ public static class Extensions @this.Result = data; return true; } + + public static bool GetParam(this OnActionEventArgs @this, int index, [NotNullWhen(true)] out T? result) + { + return @this.Parameters.GetAt(index, out result); + } + + public static bool Is(this OnActionEventArgs @this, UserType minLevel) + { + return @this.CurrentUser.HasPermission(minLevel); + } + + public static bool Is(this OnActionEventArgs @this, DataManagerActionType actionType) + { + return @this.ActionType == actionType; + } + + public static bool Is(this OnActionEventArgs @this, DataManagerAction action) + { + return @this.Action == action; + } + + #endregion }