completely rework IDataProvider

- make it more dynamic
- prevent incremental method count increase
This commit is contained in:
Schedel Pascal
2024-07-09 10:13:38 +02:00
parent f1deeeda6c
commit 9e77090bd4
17 changed files with 486 additions and 282 deletions

View File

@@ -6,6 +6,6 @@ public interface ICharacterManager
Character? CreateCharacter(string? name);
Character? CreateCharacter(string? name, Group? destination);
bool DeleteCharacter(Character? character);
IEnumerable<Character>? GetCharacters(Group? group);
IEnumerable<Character>? GetCharacters(UserProfile? profile);
IQueryable<Character>? GetCharacters(Group? group);
IQueryable<Character>? GetCharacters(UserProfile? profile);
}

View File

@@ -1,33 +0,0 @@
using OwnChar.Data;
using OwnChar.Model;
namespace OwnChar.Api;
public interface IDataManager
{
public delegate void OnActionEventHandler(object sender, OnActionEventArgs e);
public delegate void OnCallbackEventHandler(object sender, OnCallbackEventArgs e);
event OnActionEventHandler? OnAction;
event OnCallbackEventHandler? OnCallback;
// Login
UserAccount? Login(string username, string password);
bool Logout(UserAccount? account);
// Action
DataManagerActionResult ExecuteAction(DataManagerAction action, DataManagerActionType actionType, UserAccount currentUser, object? obj, params object?[] parameters);
//// User management
//UserProfile? GetUserProfile(UserAccount account);
//// Group management
//UserProfile? GetOwner(UserAccount account, Group group);
//IEnumerable<UserProfile>? GetMembers(UserAccount account, Group group);
//bool AddMember(UserAccount account, Group group, UserProfile user);
//bool RemoveMember(UserAccount account, Group group, UserProfile user);
//// Character management
//UserProfile? GetOwner(UserAccount account, Character group);
//IEnumerable<Character>? GetCharacters(UserAccount account, Group group);
//IEnumerable<Character>? GetCharacters(UserAccount account, UserProfile profile);
}

View File

@@ -1,38 +0,0 @@
using OwnChar.Model;
namespace OwnChar.Api;
public interface IDataProvider
{
bool IsInitialized();
void SetInitialized();
bool SaveDatabase();
// Model
abstract T? Create<T>() where T : OwnCharObject;
abstract bool Save<T>(T obj) where T : OwnCharObject;
abstract bool Delete<T>(T obj) where T : OwnCharObject;
virtual IEnumerable<T>? Get<T>() where T : OwnCharObject => Get<T>(null);
abstract IEnumerable<T>? Get<T>(OwnCharObject? context) where T : OwnCharObject;
// Hierarchy
abstract bool SetParent(UserProfile profile, UserAccount parent);
abstract bool SetParent(Character character, Group parent);
abstract bool SetParent(Property property, Character character);
abstract bool SetOwner(Group group, UserProfile owner);
abstract bool SetOwner(Character group, UserProfile owner);
abstract UserProfile? GetOwner(Group group);
abstract UserProfile? GetOwner(Character character);
// Collections
abstract bool AddMember(Group group, UserProfile user);
abstract bool RemoveMember(Group group, UserProfile user);
// Gets
abstract UserAccount? GetUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(string username);
abstract IEnumerable<UserAccount>? GetUserAccounts();
abstract IEnumerable<UserProfile>? GetMembers(Group group);
abstract IEnumerable<Character>? GetCharacters(Group group);
abstract IEnumerable<Character>? GetCharacters(UserProfile jprofile);
}

View File

@@ -3,10 +3,12 @@
namespace OwnChar.Api;
public interface IGroupsManager
{
IQueryable<Group>? GetGroups(UserProfile? profile);
IQueryable<Group>? GetGroups();
bool AddMember(UserProfile? profile, Group? group);
Group? CreateGroup(string? name);
bool DeleteGroup(Group? group);
bool DeleteMember(UserProfile? profile, Group? group);
IEnumerable<UserProfile>? GetMembers(Group? group);
bool RemoveMember(UserProfile? profile, Group? group);
IQueryable<UserProfile>? GetMembers(Group? group);
UserProfile? GetOwner(Group? group);
}

View File

@@ -1,4 +1,5 @@
using OwnChar.Model;
using OwnChar.Data;
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Api;

View File

@@ -4,6 +4,7 @@ using Pilz.Cryptography;
namespace OwnChar.Api;
public interface IUserManager
{
IQueryable<UserAccount>? GetUserAccounts();
UserAccount? CreateUserAccount(string? username, SecureString? password);
bool DeleteUserAccount(UserAccount? account);
UserProfile? GetOwnUserProfile();

View File

@@ -12,12 +12,14 @@ public static class DataManagerActions
public static DataManagerAction UserAccount { get; } = new(Get, "useraccount");
public static DataManagerAction Group { get; } = new(Get, "group");
public static DataManagerAction Character { get; } = new(Get, "character");
public static DataManagerAction Property { get; } = new(Get, "property");
public static DataManagerAction PropertyCategory { get; } = new(Get, "propertycategory");
}
public static class Association
{
public static DataManagerAction Owner { get; } = new(Associate, "owner");
public static DataManagerAction Parent { get; } = new(Associate, "parent");
public static DataManagerAction Profile { get; } = new(Associate, "profile");
public static DataManagerAction Members { get; } = new(Associate, "members");
}
}

View File

@@ -0,0 +1,7 @@
namespace OwnChar.Data;
public enum DataProviderSetAction
{
Set,
Remove,
}

View File

@@ -0,0 +1,12 @@
namespace OwnChar.Data;
public static class HierarchyProperties
{
public static string PropProps => "Props";
public static string PropPropCats => "PropCats";
public static string PropCharacters => "Characters";
public static string PropProfile => "Profile";
public static string PropOwner => "Owner";
public static string PropMembers => "Members";
public static string PropGroups => "Groups";
}

View File

@@ -0,0 +1,19 @@
using OwnChar.Model;
namespace OwnChar.Data;
public interface IDataManager
{
public delegate void OnActionEventHandler(object sender, OnActionEventArgs e);
public delegate void OnCallbackEventHandler(object sender, OnCallbackEventArgs e);
event OnActionEventHandler? OnAction;
event OnCallbackEventHandler? OnCallback;
// Login
UserAccount? Login(string username, string password);
bool Logout(UserAccount? account);
// Action
DataManagerActionResult ExecuteAction(DataManagerAction action, DataManagerActionType actionType, UserAccount currentUser, object? obj, params object?[] parameters);
}

View File

@@ -0,0 +1,26 @@
using OwnChar.Model;
namespace OwnChar.Data;
public interface IDataProvider
{
bool IsInitialized();
void SetInitialized();
bool SaveDatabase();
// Model
T? Create<T>() where T : OwnCharObject;
bool Save<T>(T obj) where T : OwnCharObject;
bool Delete<T>(T obj) where T : OwnCharObject;
IQueryable<T>? GetAll<T>() where T : OwnCharObject;
// Hierarchy
bool SetChild(OwnCharObject parent, OwnCharObject? child, string property, DataProviderSetAction action);
T? GetChild<T>(OwnCharObject parent, string property) where T : OwnCharObject;
IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject;
T? GetParent<T>(OwnCharObject child, string property) where T : OwnCharObject;
// Explicite getters
UserAccount? GetUserAccount(string username, string password);
UserProfile? GetUserProfile(string username);
}

View File

@@ -1,4 +1,4 @@
using OwnChar.Api;
using OwnChar.Manager.Modules;
using OwnChar.Model;
namespace OwnChar.Data.Managers;
@@ -50,14 +50,21 @@ public class DefaultDataManager : IDataManager
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User) && e.GetObject(out Group? group))
return e.SetResult(DataProvider.GetCharacters(group));
if (e.Is(UserType.User))
{
if (e.GetObject(out Group? group))
return e.SetResult(DataProvider.GetChilds<Character>(group, HierarchyProperties.PropCharacters));
if (e.GetObject(out UserProfile? profile))
return e.SetResult(DataProvider.GetChilds<Character>(profile, HierarchyProperties.PropCharacters));
}
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetAll<Character>());
}
// Create
else if (e.Is(DataManagerActionType.Set))
{
if (e.Object is Group group && e.GetParam(0, out string? name))
if (e.GetObject(out Group? group) && e.GetParam(0, out string? name))
return e.SetResult(CreateCharacter(e.CurrentUser, name, group));
}
}
@@ -68,8 +75,13 @@ public class DefaultDataManager : IDataManager
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Object is UserProfile profile)
return e.SetResult(DataProvider.GetGroups(profile));
if (e.Is(UserType.User))
{
if (e.GetObject(out UserProfile? profile))
return e.SetResult(DataProvider.GetChilds<Group>(profile, HierarchyProperties.PropGroups));
}
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetAll<Group>());
}
// Create
@@ -87,7 +99,7 @@ public class DefaultDataManager : IDataManager
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.Admin))
return e.SetResult(DataProvider.GetUserAccounts());
return e.SetResult(DataProvider.GetAll<UserAccount>());
}
// Create
@@ -96,11 +108,43 @@ public class DefaultDataManager : IDataManager
if (e.GetParam(0, out string? username) && e.GetParam(1, out string? password))
return e.SetResult(CreateUserAccount(username, password));
}
}
// Property
else if (e.Is(DataManagerActions.Getter.Property))
{
// Get
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User) && e.GetObject(out Character? character))
return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats));
}
// Create
//else if (e.ActionType == DataManagerActionType.Set(
// return e.SetResult(DataProvider.GetUserAccounts());
if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetObject(out Character? character))
return e.SetResult(CreateProperty(e.CurrentUser, character));
}
}
//// PropertyCategory
//else if (e.Is(DataManagerActions.Getter.PropertyCategory))
//{
// // Get
// if (e.Is(DataManagerActionType.Get))
// {
// if (e.Is(UserType.User) && e.GetObject(out Character? character))
// return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats));
// }
// // Create
// if (e.Is(DataManagerActionType.Set))
// {
// if (e.Is(UserType.User) && e.GetObject(out Character? character))
// return e.SetResult(CreateProperty(character));
// }
//}
return false;
}
@@ -132,6 +176,13 @@ public class DefaultDataManager : IDataManager
if (e.Object is UserAccount userAccount)
return DeleteUserAccount(userAccount);
// Property
if (e.Object is Property property)
{
if (e.GetParam(0, out Character? paramChar))
return DeleteProperty(e.CurrentUser, paramChar, property);
}
return false;
}
@@ -140,31 +191,45 @@ public class DefaultDataManager : IDataManager
if (!e.Is(DataManagerActions.Associate))
return false;
if (e.Is(DataManagerActions.Association.Owner))
if (e.Is(DataManagerActions.Association.Profile))
{
if (e.Object is Group group)
if (e.GetObject(out UserAccount? account))
{
switch (e.ActionType)
if (e.Is(DataManagerActionType.Get))
{
case DataManagerActionType.Get:
return e.SetResultT(DataProvider.GetOwner(group));
case DataManagerActionType.Set:
if (e.GetParam(0, out UserProfile? profile))
return DataProvider.SetOwner(group, profile);
break;
if (e.Is(UserType.Admin) || (e.Is(UserType.User) && e.CurrentUser == account))
return e.SetResult(DataProvider.GetChild<UserProfile>(account, HierarchyProperties.PropProfile));
}
}
}
if (e.Object is Character character)
else if (e.Is(DataManagerActions.Association.Owner))
{
switch (e.ActionType)
if (e.GetObject(out Group? group))
{
case DataManagerActionType.Get:
return e.SetResultT(DataProvider.GetOwner(character));
case DataManagerActionType.Set:
if (e.GetParam(0, out UserProfile? profile))
return DataProvider.SetOwner(character, profile);
break;
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
return e.SetResultT(DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner));
}
else if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetParam(0, out UserProfile? profile))
return DataProvider.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
}
}
if (e.GetObject(out Character? character))
{
if (e.Is(DataManagerActionType.Get))
{
if (e.Is(UserType.User))
return e.SetResultT(DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner));
}
else if (e.Is(DataManagerActionType.Set))
{
if (e.Is(UserType.User) && e.GetParam(0, out UserProfile? profile))
return DataProvider.SetChild(character, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
}
}
}
@@ -176,21 +241,21 @@ public class DefaultDataManager : IDataManager
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetMembers(group);
return DataProvider.GetChilds<UserProfile>(group, HierarchyProperties.PropMembers);
}
public UserProfile? GetOwner(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetOwner(group);
return DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner);
}
public UserProfile? GetOwner(UserAccount account, Character character)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetOwner(character);
return DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner);
}
public UserProfile? GetUserProfile(UserAccount account)
@@ -224,6 +289,28 @@ public class DefaultDataManager : IDataManager
return result;
}
protected virtual Property? CreateProperty(UserAccount account, Character character)
{
ArgumentNullException.ThrowIfNull(character, nameof(character));
if (!account.HasPermission(UserType.User))
return null;
var prop = DataProvider.Create<Property>();
ArgumentNullException.ThrowIfNull(prop, nameof(prop));
DataProvider.SetChild(character, prop, HierarchyProperties.PropProps, DataProviderSetAction.Set);
return prop;
}
protected virtual bool DeleteProperty(UserAccount account, Character character, Property property)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.Delete(property);
}
protected virtual UserAccount? CreateUserAccount(string username, string password)
{
var account = DataProvider.Create<UserAccount>();
@@ -238,10 +325,10 @@ public class DefaultDataManager : IDataManager
account.Password = password;
profile.Name = username;
DataProvider.SetParent(profile, account);
DataProvider.SetChild(account, profile, HierarchyProperties.PropProfile, DataProviderSetAction.Set);
group.IsInternal = true;
DataProvider.SetOwner(group, profile);
DataProvider.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
DataProvider.Save(account);
DataProvider.Save(profile);
@@ -258,34 +345,6 @@ public class DefaultDataManager : IDataManager
return true;
}
public IEnumerable<Character>? GetCharacters(UserAccount account, Group group)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetCharacters(group);
}
public IEnumerable<Character>? GetCharacters(UserAccount account, UserProfile profile)
{
if (!account.HasPermission(UserType.Guest))
return null;
return DataProvider.GetCharacters(profile);
}
public bool AddMember(UserAccount account, Group group, UserProfile user)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.AddMember(group, user);
}
public bool RemoveMember(UserAccount account, Group group, UserProfile user)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.RemoveMember(group, user);
}
protected virtual Group? CreateGroup(UserAccount account, string name)
{
if (!account.HasPermission(UserType.User) || GetUserProfile(account) is not UserProfile profile || DataProvider.Create<Group>() is not Group group)
@@ -295,14 +354,14 @@ public class DefaultDataManager : IDataManager
DataProvider.Save(group);
DataProvider.SetOwner(group, profile);
DataProvider.SetChild(group, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
return group;
}
protected virtual bool DeleteGroup(UserAccount account, Group group)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(group) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(group, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
return DataProvider.Delete(group);
}
@@ -316,21 +375,18 @@ public class DefaultDataManager : IDataManager
DataProvider.Save(character);
DataProvider.SetOwner(character, profile);
DataProvider.SetChild(character, profile, HierarchyProperties.PropOwner, DataProviderSetAction.Set);
if (group != null)
DataProvider.SetParent(character, group);
DataProvider.SetChild(group, character, HierarchyProperties.PropCharacters, DataProviderSetAction.Set);
return character;
}
protected virtual bool DeleteCharacter(UserAccount account, Character character)
{
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetOwner(character) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
if (GetUserProfile(account) is not UserProfile profile || DataProvider.GetChild<UserProfile>(character, HierarchyProperties.PropOwner) is not UserProfile owner || !account.HasPermission(profile == owner ? UserType.User : UserType.Admin))
return false;
DataProvider.Delete(character);
return true;
return DataProvider.Delete(character);
}
}

View File

@@ -1,13 +1,15 @@
using Newtonsoft.Json;
using OwnChar.Api;
using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model;
using System.Collections;
using System.Linq;
namespace OwnChar.Data.Providers.JsonFile;
public class JsonFileDataProvider : IDataProvider
{
protected readonly Random random = new();
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
@@ -33,6 +35,11 @@ public class JsonFileDataProvider : IDataProvider
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
}
protected int GenerateNewObjectId()
{
return random.Next();
}
protected virtual JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
@@ -54,6 +61,12 @@ public class JsonFileDataProvider : IDataProvider
JsonFile.IsInitialized = true;
}
public bool SaveDatabase()
{
SaveFile();
return true;
}
public T? Create<T>() where T : OwnCharObject
{
var t = typeof(T);
@@ -74,7 +87,13 @@ public class JsonFileDataProvider : IDataProvider
else
obj = null;
return obj as T;
if (obj is T objT)
{
objT.Id = GenerateNewObjectId();
return objT;
}
return null;
}
public bool Save<T>(T obj) where T : OwnCharObject
@@ -140,89 +159,261 @@ public class JsonFileDataProvider : IDataProvider
return false;
}
public IEnumerable<T>? Get<T>(OwnCharObject? context) where T : OwnCharObject
public IQueryable<T>? GetAll<T>(OwnCharObject? context) where T : OwnCharObject
{
var t = typeof(T);
static IEnumerable<T>? asList(IList list) => list as IEnumerable<T>;
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>;
if (t == typeof(Property))
{
// ...
}
else if (t == typeof(PropertyCategory))
{
// ...
}
else if (t == typeof(Character))
{
if (context is JsonGroup group)
return (IEnumerable<T>)group.Characters;
if (context is UserProfile profile)
return (IEnumerable<T>)JsonFile.Characters.Where(n => n.Owner == profile);
return (IEnumerable<T>)JsonFile.Characters;
}
if (t == typeof(Character))
return asList(JsonFile.Characters);
else if (t == typeof(Group))
{
if (context is UserProfile profile)
return (IEnumerable<T>)JsonFile.Groups.Where(g => g.Owner == profile);
return (IEnumerable<T>)JsonFile.Groups;
}
return asList(JsonFile.Groups);
else if (t == typeof(UserAccount))
return (IEnumerable<T>)JsonFile.UserAccounts;
return asList(JsonFile.UserAccounts);
return null;
}
public bool SetParent(UserProfile profile, UserAccount parent)
public bool SetChild(OwnCharObject parent, OwnCharObject? child, string property, DataProviderSetAction action)
{
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
// UserAccount
if (parent is JsonUserAccount parentAccount)
{
jaccount.Profile = jprofile;
// User profil
if (property == HierarchyProperties.PropProfile)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentAccount.Profile = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentAccount.Profile = childProfile;
return true;
}
}
}
// Group
if (parent is JsonGroup parentGroup)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentGroup.Characters.Clear();
return true;
}
// Add/Remove
else if (child is JsonCharacter childCharacter)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentGroup.Characters.Remove(childCharacter);
return true;
}
// Add
else
{
if (!parentGroup.Characters.Contains(childCharacter))
parentGroup.Characters.Add(childCharacter);
return true;
}
}
}
// Members
// ...
// Owner
if (property == HierarchyProperties.PropOwner)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentGroup.Owner = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentGroup.Owner = childProfile;
return true;
}
}
}
// Character
if (parent is JsonCharacter parentCharacter)
{
// Properties
if (property == HierarchyProperties.PropProps)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentCharacter.Properties.Clear();
return true;
}
// Add/Remove
else if (child is JsonProp childProperty)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentCharacter.Properties.Remove(childProperty);
return true;
}
// Add
else
{
if (!parentCharacter.Properties.Contains(childProperty))
parentCharacter.Properties.Add(childProperty);
return true;
}
}
}
// Property categories
else if (property == HierarchyProperties.PropPropCats)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentCharacter.PropertyCategories.Clear();
return true;
}
// Add/Remove
else if (child is JsonPropCat childPropertyCategory)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentCharacter.PropertyCategories.Remove(childPropertyCategory);
return true;
}
// Add
else
{
if (!parentCharacter.PropertyCategories.Contains(childPropertyCategory))
parentCharacter.PropertyCategories.Add(childPropertyCategory);
return true;
}
}
}
// Owner
else if (property == HierarchyProperties.PropOwner)
{
// Remove
if (child is null || action == DataProviderSetAction.Remove)
{
parentCharacter.Owner = null;
return true;
}
// Set
else if (child is JsonUserProfile childProfile)
{
parentCharacter.Owner = childProfile;
return true;
}
}
}
return false;
}
public bool SetParent(Character character, Group parent)
public T? GetParent<T>(OwnCharObject child, string property) where T : OwnCharObject
{
if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup)
{
if (!jgroup.Characters.Contains(jcharacter))
jgroup.Characters.Add(jcharacter);
return true;
}
return false;
throw new NotImplementedException("Not yet implemented as not needed.");
}
public bool SetParent(Property property, Character character)
public T? GetChild<T>(OwnCharObject parent, string property) where T : OwnCharObject
{
if (property is JsonProp jprop && character is JsonCharacter jcharacter)
// UserAccount
if (parent is JsonUserAccount parentAccount)
{
if (!jcharacter.Properties.Contains(jprop))
jcharacter.Properties.Add(jprop);
return true;
}
return false;
// User profile
if (property == HierarchyProperties.PropProfile)
return parentAccount.Profile as T;
}
public bool SetOwner(Group group, UserProfile owner)
// Group
else if (parent is JsonGroup parentGroup)
{
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
{
jgroup.Owner = jprofile;
return true;
}
return false;
// Owner
if (property == HierarchyProperties.PropCharacters)
return parentGroup.Owner as T;
}
public bool SetOwner(Character character, UserProfile owner)
// Character
else if (parent is JsonCharacter parentCharacter)
{
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
{
jcharacter.Owner = jprofile;
return true;
// Owner
if (property == HierarchyProperties.PropOwner)
return parentCharacter.Owner as T;
}
return false;
return null;
}
public IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject
{
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>;
// Group
if (parent is JsonGroup parentGroup)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
return asList(parentGroup.Characters);
// Members
if (property == HierarchyProperties.PropMembers)
return asList(parentGroup.Members);
}
// Character
else if (parent is JsonCharacter parentCharacter)
{
// Properties
if (property == HierarchyProperties.PropProps)
return asList(parentCharacter.Properties);
// Property categories
if (property == HierarchyProperties.PropPropCats)
return asList(parentCharacter.PropertyCategories);
}
// UserProfile
else if (parent is JsonUserProfile parentUserProfile)
{
// Characters
if (property == HierarchyProperties.PropCharacters)
return asList(JsonFile.Characters.Where(c => c.Owner == parentUserProfile));
// Groups
if (property == HierarchyProperties.PropGroups)
return asList(JsonFile.Groups.Where(g => g.Owner == parentUserProfile));
}
return null;
}
public UserAccount? GetUserAccount(string username, string password)
@@ -236,66 +427,4 @@ public class JsonFileDataProvider : IDataProvider
{
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
}
public IEnumerable<UserProfile>? GetMembers(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Members;
return null;
}
public UserProfile? GetOwner(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Owner;
return null;
}
public UserProfile? GetOwner(Character character)
{
if (character is JsonCharacter jcharacter)
return jcharacter.Owner;
return null;
}
public IEnumerable<Character>? GetCharacters(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Characters;
return null;
}
public IEnumerable<Character>? GetCharacters(UserProfile profile)
{
if (profile is UserProfile jprofile)
return JsonFile.Characters.Where(n => n.Owner == profile);
return null;
}
public bool AddMember(Group group, UserProfile user)
{
if (group is JsonGroup jgroup && user is JsonUserProfile jprofile)
{
if (!jgroup.Members.Contains(jprofile))
jgroup.Members.Add(jprofile);
return true;
}
return false;
}
public bool RemoveMember(Group group, UserProfile user)
{
if (group is JsonGroup jgroup && user is JsonUserProfile jprofile)
{
jgroup.Members.Remove(jprofile);
return true;
}
return false;
}
public bool SaveDatabase()
{
SaveFile();
return true;
}
}

View File

@@ -1,26 +1,27 @@
using OwnChar.Api;
using OwnChar.Data;
using OwnChar.Model;
namespace OwnChar.Manager.Modules;
public class CharacterManager(OwnCharManager manager) : OwnCharManagerModule(manager), ICharacterManager
{
public IEnumerable<Character>? GetCharacters(Group? group)
public IQueryable<Character>? GetCharacters(Group? group)
{
Manager.CheckLogin();
if (group != null)
return Manager.DataManager?.GetCharacters(Manager.CurrentUser!, group);
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Character, DataManagerActionType.Get, Manager.CurrentUser, group).Result as IQueryable<Character>;
return null;
}
public IEnumerable<Character>? GetCharacters(UserProfile? profile)
public IQueryable<Character>? GetCharacters(UserProfile? profile)
{
Manager.CheckLogin();
if (profile != null)
return Manager.DataManager?.GetCharacters(Manager.CurrentUser!, profile);
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Character, DataManagerActionType.Get, Manager.CurrentUser, profile).Result as IQueryable<Character>;
return null;
}
@@ -33,18 +34,14 @@ public class CharacterManager(OwnCharManager manager) : OwnCharManagerModule(man
public Character? CreateCharacter(string? name, Group? destination)
{
ArgumentException.ThrowIfNullOrWhiteSpace(name, nameof(name));
Manager.CheckLogin();
return Manager.DataManager?.CreateCharacter(Manager.CurrentUser!, name, destination);
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.PropertyCategory, DataManagerActionType.Set, Manager.CurrentUser, null, name, destination).Result as Character;
}
public bool DeleteCharacter(Character? character)
{
ArgumentNullException.ThrowIfNull(character, nameof(character));
Manager.CheckLogin();
return Manager.DataManager?.DeleteCharacter(Manager.CurrentUser!, character) ?? false;
return Manager.DataManager.ExecuteAction(DataManagerActions.Delete, DataManagerActionType.Default, Manager.CurrentUser, character).HasSuccess;
}
}

View File

@@ -10,14 +10,27 @@ public class GroupsManager(OwnCharManager manager) : OwnCharManagerModule(manage
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Get, Manager.CurrentUser, group).HasSuccess;
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Owner, DataManagerActionType.Get, Manager.CurrentUser, group).Result as UserProfile;
}
public IEnumerable<UserProfile>? GetMembers(Group? group)
public IQueryable<Group>? GetGroups(UserProfile? profile)
{
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Group, DataManagerActionType.Get, Manager.CurrentUser, profile) as IQueryable<Group>;
}
public IQueryable<Group>? GetGroups()
{
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.Group, DataManagerActionType.Get, Manager.CurrentUser, null) as IQueryable<Group>;
}
public IQueryable<UserProfile>? GetMembers(Group? group)
{
ArgumentNullException.ThrowIfNull(group, nameof(group));
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Get, Manager.CurrentUser, group).HasSuccess;
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Get, Manager.CurrentUser, group).Result as IQueryable<UserProfile>;
}
public bool AddMember(UserProfile? profile, Group? group)
@@ -28,7 +41,7 @@ public class GroupsManager(OwnCharManager manager) : OwnCharManagerModule(manage
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Members, DataManagerActionType.Set, Manager.CurrentUser, group, profile).HasSuccess;
}
public bool DeleteMember(UserProfile? profile, Group? group)
public bool RemoveMember(UserProfile? profile, Group? group)
{
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));

View File

@@ -1,4 +1,5 @@
using OwnChar.Api;
using OwnChar.Data;
using OwnChar.Model;
using Pilz.Cryptography;
@@ -9,20 +10,28 @@ public class UserManager(OwnCharManager manager) : OwnCharManagerModule(manager)
public UserProfile? GetOwnUserProfile()
{
Manager.CheckLogin();
return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!);
return Manager.DataManager.ExecuteAction(DataManagerActions.Association.Profile, DataManagerActionType.Get, Manager.CurrentUser, Manager.CurrentUser).Result as UserProfile;
}
public IQueryable<UserAccount>? GetUserAccounts()
{
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.UserAccount, DataManagerActionType.Get, Manager.CurrentUser, null).Result as IQueryable<UserAccount>;
}
public UserAccount? CreateUserAccount(string? username, SecureString? password)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
Manager.CheckLogin();
username = username.Trim().ToLower();
return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password));
return Manager.DataManager.ExecuteAction(DataManagerActions.Getter.UserAccount, DataManagerActionType.Set, Manager.CurrentUser, null, username, Utils.HashPassword(username, password)).Result as UserAccount;
}
public bool DeleteUserAccount(UserAccount? account)
{
ArgumentNullException.ThrowIfNull(account, nameof(account));
return Manager.DataManager?.DeleteUserAccount(account) ?? false;
Manager.CheckLogin();
return Manager.DataManager.ExecuteAction(DataManagerActions.Delete, DataManagerActionType.Default, Manager.CurrentUser, account).HasSuccess;
}
}

View File

@@ -1,5 +1,6 @@
using OwnChar.Api;
using OwnChar.Api.Exceptions;
using OwnChar.Data;
using OwnChar.Manager.Modules;
using OwnChar.Model;
using Pilz.Cryptography;