and even more

This commit is contained in:
Schedel Pascal
2024-07-09 10:57:20 +02:00
parent bd0053cc03
commit a721b5ab6c
2 changed files with 81 additions and 30 deletions

View File

@@ -1,5 +1,6 @@
using OwnChar.Manager.Modules; using OwnChar.Manager.Modules;
using OwnChar.Model; using OwnChar.Model;
using System.Reflection.Metadata;
namespace OwnChar.Data.Managers; namespace OwnChar.Data.Managers;
@@ -117,7 +118,7 @@ public class DefaultDataManager : IDataManager
if (e.Is(DataManagerActionType.Get)) if (e.Is(DataManagerActionType.Get))
{ {
if (e.Is(UserType.User) && e.GetObject(out Character? character)) if (e.Is(UserType.User) && e.GetObject(out Character? character))
return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats)); return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropProps));
} }
// Create // Create
@@ -128,23 +129,23 @@ public class DefaultDataManager : IDataManager
} }
} }
//// PropertyCategory // PropertyCategory
//else if (e.Is(DataManagerActions.Getter.PropertyCategory)) else if (e.Is(DataManagerActions.Getter.PropertyCategory))
//{ {
// // Get // Get
// if (e.Is(DataManagerActionType.Get)) if (e.Is(DataManagerActionType.Get))
// { {
// if (e.Is(UserType.User) && e.GetObject(out Character? character)) if (e.Is(UserType.User) && e.GetObject(out Character? character))
// return e.SetResult(DataProvider.GetChilds<Property>(character, HierarchyProperties.PropPropCats)); return e.SetResult(DataProvider.GetChilds<PropertyCategory>(character, HierarchyProperties.PropPropCats));
// } }
// // Create // Create
// if (e.Is(DataManagerActionType.Set)) if (e.Is(DataManagerActionType.Set))
// { {
// if (e.Is(UserType.User) && e.GetObject(out Character? character)) if (e.Is(UserType.User) && e.GetObject(out Character? character))
// return e.SetResult(CreateProperty(character)); return e.SetResult(CreatePropertyCategory(e.CurrentUser, character));
// } }
//} }
return false; return false;
} }
@@ -154,7 +155,8 @@ public class DefaultDataManager : IDataManager
if (!e.Is(DataManagerActions.Save)) if (!e.Is(DataManagerActions.Save))
return false; return false;
// ... if (e.Is(UserType.User) && e.GetObject(out OwnCharObject? obj))
DataProvider.Save(obj);
return false; return false;
} }
@@ -165,22 +167,29 @@ public class DefaultDataManager : IDataManager
return false; return false;
// Character // Character
if (e.Object is Character character) if (e.GetObject(out Character? character))
return DeleteCharacter(e.CurrentUser, character); return DeleteCharacter(e.CurrentUser, character);
// Group // Group
if (e.Object is Group group) if (e.GetObject(out Group? group))
return DeleteGroup(e.CurrentUser, group); return DeleteGroup(e.CurrentUser, group);
// User // User
if (e.Object is UserAccount userAccount) if (e.GetObject(out UserAccount? userAccount))
return DeleteUserAccount(userAccount); return DeleteUserAccount(userAccount);
// Property // Property
if (e.Object is Property property) if (e.GetObject(out Property? property))
{ {
if (e.GetParam(0, out Character? paramChar)) if (e.GetParam(0, out Character? paramChar))
return DeleteProperty(e.CurrentUser, paramChar, property); return DeleteIfOwnerOrAdmin(e.CurrentUser, paramChar, property);
}
// Property
if (e.GetObject(out PropertyCategory? propertyCategory))
{
if (e.GetParam(0, out Character? paramChar))
return DeleteIfOwnerOrAdmin(e.CurrentUser, paramChar, propertyCategory);
} }
return false; return false;
@@ -289,6 +298,13 @@ public class DefaultDataManager : IDataManager
return result; return result;
} }
protected virtual bool DeleteIfOwnerOrAdmin<T>(UserAccount account, Character character, T property) where T : OwnCharObject
{
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 Property? CreateProperty(UserAccount account, Character character) protected virtual Property? CreateProperty(UserAccount account, Character character)
{ {
ArgumentNullException.ThrowIfNull(character, nameof(character)); ArgumentNullException.ThrowIfNull(character, nameof(character));
@@ -304,11 +320,19 @@ public class DefaultDataManager : IDataManager
return prop; return prop;
} }
protected virtual bool DeleteProperty(UserAccount account, Character character, Property property) protected virtual PropertyCategory? CreatePropertyCategory(UserAccount account, Character character)
{ {
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)) ArgumentNullException.ThrowIfNull(character, nameof(character));
return false;
return DataProvider.Delete(property); if (!account.HasPermission(UserType.User))
return null;
var cat = DataProvider.Create<PropertyCategory>();
ArgumentNullException.ThrowIfNull(cat, nameof(cat));
DataProvider.SetChild(character, cat, HierarchyProperties.PropPropCats, DataProviderSetAction.Set);
return cat;
} }
protected virtual UserAccount? CreateUserAccount(string username, string password) protected virtual UserAccount? CreateUserAccount(string username, string password)

View File

@@ -163,7 +163,7 @@ public class JsonFileDataProvider : IDataProvider
{ {
var t = typeof(T); var t = typeof(T);
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>; static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
if (t == typeof(Character)) if (t == typeof(Character))
return asList(JsonFile.Characters); return asList(JsonFile.Characters);
@@ -233,7 +233,34 @@ public class JsonFileDataProvider : IDataProvider
} }
// Members // Members
// ... if (property == HierarchyProperties.PropMembers)
{
// Clear
if (child is null && action == DataProviderSetAction.Remove)
{
parentGroup.Members.Clear();
return true;
}
// Add/Remove
else if (child is JsonUserProfile childProfile)
{
// Remove
if (action == DataProviderSetAction.Remove)
{
parentGroup.Members.Remove(childProfile);
return true;
}
// Add
else
{
if (!parentGroup.Members.Contains(childProfile))
parentGroup.Members.Add(childProfile);
return true;
}
}
}
// Owner // Owner
if (property == HierarchyProperties.PropOwner) if (property == HierarchyProperties.PropOwner)
@@ -375,7 +402,7 @@ public class JsonFileDataProvider : IDataProvider
public IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject public IQueryable<T>? GetChilds<T>(OwnCharObject parent, string property) where T : OwnCharObject
{ {
static IQueryable<T>? asList(IEnumerable list) => list as IQueryable<T>; static IQueryable<T>? asList(IEnumerable list) => list.AsQueryable() as IQueryable<T>;
// Group // Group
if (parent is JsonGroup parentGroup) if (parent is JsonGroup parentGroup)