code cleanup

This commit is contained in:
2024-06-11 06:59:34 +02:00
parent ff4c352dee
commit 6fd51f4b7a
26 changed files with 444 additions and 472 deletions

View File

@@ -1,20 +1,19 @@
using OwnChar.Model;
namespace OwnChar.Data
namespace OwnChar.Data;
public interface IDataManager
{
public interface IDataManager
{
// Login
abstract UserAccount? Login(string username, string password);
abstract bool Logout(UserAccount? account);
// Login
abstract UserAccount? Login(string username, string password);
abstract bool Logout(UserAccount? account);
// User management
abstract UserAccount? CreateUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(UserAccount account);
abstract bool DeleteUserAccount(UserAccount account);
// User management
abstract UserAccount? CreateUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(UserAccount account);
abstract bool DeleteUserAccount(UserAccount account);
// Group management
abstract UserProfile? GetOwner(Group group);
abstract IEnumerable<UserProfile>? GetMembers(Group group);
}
// Group management
abstract UserProfile? GetOwner(Group group);
abstract IEnumerable<UserProfile>? GetMembers(Group group);
}

View File

@@ -1,29 +1,28 @@
using OwnChar.Model;
namespace OwnChar.Data
namespace OwnChar.Data;
public interface IDataProvider
{
public interface IDataProvider
{
// General
abstract bool IsInitialized();
// General
abstract bool IsInitialized();
// Model
abstract T? Create<T>() where T : class, IOwnCharObject;
abstract bool Save<T>(T obj) where T : class, IOwnCharObject;
abstract bool Delete<T>(T obj) where T : class, IOwnCharObject;
// Model
abstract T? Create<T>() where T : class, IOwnCharObject;
abstract bool Save<T>(T obj) where T : class, IOwnCharObject;
abstract bool Delete<T>(T obj) where T : class, IOwnCharObject;
// 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);
// 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);
// Gets
abstract UserAccount? GetUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(string username);
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
abstract UserProfile? GetOwner(Group group);
abstract UserProfile? GetOwner(Character character);
}
// Gets
abstract UserAccount? GetUserAccount(string username, string password);
abstract UserProfile? GetUserProfile(string username);
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
abstract UserProfile? GetOwner(Group group);
abstract UserProfile? GetOwner(Character character);
}

View File

@@ -1,6 +1,5 @@
namespace OwnChar.Data.Managers
namespace OwnChar.Data.Managers;
public class ClientDataManager
{
public class ClientDataManager
{
}
}

View File

@@ -1,79 +1,78 @@
using OwnChar.Model;
namespace OwnChar.Data.Managers
namespace OwnChar.Data.Managers;
public class DefaultDataManager(IDataProvider dataProvider) : IDataManager
{
public class DefaultDataManager(IDataProvider dataProvider) : IDataManager
private const string defaultUsername = "admin";
private const string defaultPassword = "admin";
public IDataProvider DataProvider { get; } = dataProvider;
public IEnumerable<UserProfile>? GetMembers(Group group)
{
private const string defaultUsername = "admin";
private const string defaultPassword = "admin";
return DataProvider.GetGroupMembers(group);
}
public IDataProvider DataProvider { get; } = dataProvider;
public UserProfile? GetOwner(Group group)
{
return DataProvider.GetOwner(group);
}
public IEnumerable<UserProfile>? GetMembers(Group group)
{
return DataProvider.GetGroupMembers(group);
}
public UserProfile? GetUserProfile(UserAccount account)
{
ArgumentException.ThrowIfNullOrWhiteSpace(account.Username, nameof(account.Username));
return DataProvider.GetUserProfile(account.Username);
}
public UserProfile? GetOwner(Group group)
{
return DataProvider.GetOwner(group);
}
public UserAccount? Login(string username, string password)
{
return DataProvider.GetUserAccount(username, password);
}
public UserProfile? GetUserProfile(UserAccount account)
{
ArgumentException.ThrowIfNullOrWhiteSpace(account.Username, nameof(account.Username));
return DataProvider.GetUserProfile(account.Username);
}
public bool Logout(UserAccount? account)
{
return true;
}
public UserAccount? Login(string username, string password)
{
return DataProvider.GetUserAccount(username, password);
}
public bool Initialize(bool force)
{
if (force || !DataProvider.IsInitialized())
return CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null;
return true;
}
public bool Logout(UserAccount? account)
{
return true;
}
public UserAccount? CreateUserAccount(string username, string password)
{
var account = DataProvider.Create<UserAccount>();
var profile = DataProvider.Create<UserProfile>();
var group = DataProvider.Create<Group>();
public bool Initialize(bool force)
{
if (force || !DataProvider.IsInitialized())
return CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null;
return true;
}
ArgumentNullException.ThrowIfNull(account, nameof(account));
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));
public UserAccount? CreateUserAccount(string username, string password)
{
var account = DataProvider.Create<UserAccount>();
var profile = DataProvider.Create<UserProfile>();
var group = DataProvider.Create<Group>();
account.Username = username;
account.Password = password;
ArgumentNullException.ThrowIfNull(account, nameof(account));
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
ArgumentNullException.ThrowIfNull(group, nameof(group));
profile.Name = username;
DataProvider.SetParent(profile, account);
account.Username = username;
account.Password = password;
group.IsInternal = true;
DataProvider.SetOwner(group, profile);
profile.Name = username;
DataProvider.SetParent(profile, account);
DataProvider.Save(account);
DataProvider.Save(profile);
DataProvider.Save(group);
group.IsInternal = true;
DataProvider.SetOwner(group, profile);
return account;
}
DataProvider.Save(account);
DataProvider.Save(profile);
DataProvider.Save(group);
return account;
}
public bool DeleteUserAccount(UserAccount account)
{
if (!string.IsNullOrWhiteSpace(account.Username) && DataProvider.GetUserProfile(account.Username) is UserProfile userProfile)
userProfile.Name = "Deleted user";
DataProvider.Delete(account);
return true;
}
public bool DeleteUserAccount(UserAccount account)
{
if (!string.IsNullOrWhiteSpace(account.Username) && DataProvider.GetUserProfile(account.Username) is UserProfile userProfile)
userProfile.Name = "Deleted user";
DataProvider.Delete(account);
return true;
}
}

View File

@@ -1,12 +1,10 @@
using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile
namespace OwnChar.Data.Providers.JsonFile;
public class JsonFile
{
public class JsonFile
{
public List<JsonUserAccount> UserAccounts { get; } = [];
public List<JsonCharacter> Characters { get; } = [];
public List<JsonGroup> Groups { get; } = [];
}
public List<JsonUserAccount> UserAccounts { get; } = [];
public List<JsonCharacter> Characters { get; } = [];
public List<JsonGroup> Groups { get; } = [];
}

View File

@@ -1,221 +1,219 @@
using Newtonsoft.Json;
using OwnChar.Data.Providers.JsonFile.Model;
using OwnChar.Model;
using System.Net.NetworkInformation;
namespace OwnChar.Data.Providers.JsonFile
namespace OwnChar.Data.Providers.JsonFile;
public class JsonFileDataProvider : IDataProvider
{
public class JsonFileDataProvider : IDataProvider
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
public JsonFileDataProvider(string filePath)
{
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
JsonFilePath = filePath;
LoadFile();
public JsonFileDataProvider(string filePath)
// Get rid of bad compiler warnings
if (JsonFile == null)
throw new Exception("Something went incredible wrong at initialization of the JsonFile.");
}
protected void LoadFile()
{
if (File.Exists(JsonFilePath) && JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
JsonFile = jsonFile;
JsonFile ??= new();
}
protected void SaveFile()
{
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
}
protected JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
{
JsonFilePath = filePath;
LoadFile();
PreserveReferencesHandling = PreserveReferencesHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented,
};
}
public T? Create<T>() where T : class, IOwnCharObject
{
var t = typeof(T);
IOwnCharObject? obj;
// Get rid of bad compiler warnings
if (JsonFile == null)
throw new Exception("Something went incredible wrong at initialization of the JsonFile.");
if (t == typeof(Property))
obj = new JsonProp();
else if (t == typeof(PropertyCategory))
obj = new JsonPropCat();
else if (t == typeof(Character))
obj = new JsonCharacter();
else if (t == typeof(Group))
obj = new JsonGroup();
else if (t == typeof(UserAccount))
obj = new JsonUserAccount();
else if (t == typeof(UserProfile))
obj = new JsonUserProfile();
else
obj = null;
return obj as T;
}
public bool Save<T>(T obj) where T : class, IOwnCharObject
{
if (obj is JsonCharacter character)
{
if (!JsonFile.Characters.Contains(character))
JsonFile.Characters.Add(character);
}
else if (obj is JsonGroup group)
{
if (!JsonFile.Groups.Contains(group))
JsonFile.Groups.Add(group);
}
else if (obj is JsonUserAccount account)
{
if (!JsonFile.UserAccounts.Contains(account))
JsonFile.UserAccounts.Add(account);
}
protected void LoadFile()
return true;
}
public bool IsInitialized()
{
return JsonFile.UserAccounts.Count != 0;
}
public bool Delete<T>(T obj) where T : class, IOwnCharObject
{
if (obj is JsonCharacter character)
{
if (File.Exists(JsonFilePath) && JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
JsonFile = jsonFile;
JsonFile ??= new();
}
protected void SaveFile()
{
File.WriteAllText(JsonFilePath, JsonConvert.SerializeObject(JsonFile, CreateJsonSerializerSettings()));
}
protected JsonSerializerSettings CreateJsonSerializerSettings()
{
return new JsonSerializerSettings
{
PreserveReferencesHandling = PreserveReferencesHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
TypeNameHandling = TypeNameHandling.Auto,
Formatting = Formatting.Indented,
};
}
public T? Create<T>() where T : class, IOwnCharObject
{
var t = typeof(T);
IOwnCharObject? obj;
if (t == typeof(Property))
obj = new JsonProp();
else if (t == typeof(PropertyCategory))
obj = new JsonPropCat();
else if (t == typeof(Character))
obj = new JsonCharacter();
else if (t == typeof(Group))
obj = new JsonGroup();
else if (t == typeof(UserAccount))
obj = new JsonUserAccount();
else if (t == typeof(UserProfile))
obj = new JsonUserProfile();
else
obj = null;
return obj as T;
}
public bool Save<T>(T obj) where T : class, IOwnCharObject
{
if (obj is JsonCharacter character)
{
if (!JsonFile.Characters.Contains(character))
JsonFile.Characters.Add(character);
}
else if (obj is JsonGroup group)
{
if (!JsonFile.Groups.Contains(group))
JsonFile.Groups.Add(group);
}
else if(obj is JsonUserAccount account)
{
if (!JsonFile.UserAccounts.Contains(account))
JsonFile.UserAccounts.Add(account);
}
JsonFile.Groups.ForEach(n => n.Characters.Remove(character));
return true;
}
public bool IsInitialized()
else if (obj is JsonProp prop)
{
return JsonFile.UserAccounts.Count != 0;
JsonFile.Characters.ForEach(n => n.Properties.Remove(prop));
return true;
}
public bool Delete<T>(T obj) where T : class, IOwnCharObject
else if (obj is JsonPropCat propCat)
{
if (obj is JsonCharacter character)
JsonFile.Characters.ForEach(n => n.Properties.ForEach(m =>
{
JsonFile.Groups.ForEach(n => n.Characters.Remove(character));
return true;
}
else if (obj is JsonProp prop)
{
JsonFile.Characters.ForEach(n => n.Properties.Remove(prop));
return true;
}
else if (obj is JsonPropCat propCat)
{
JsonFile.Characters.ForEach(n => n.Properties.ForEach(m =>
{
if (m.Category == propCat)
m.Category = null;
}));
JsonFile.Characters.ForEach(n => n.PropertyCategories.Remove(propCat));
return true;
}
else if (obj is JsonGroup group)
{
JsonFile.Groups.Remove(group);
return true;
}
else if (obj is JsonUserAccount account)
{
JsonFile.UserAccounts.Remove(account);
return true;
}
else if (obj is JsonUserProfile profile)
{
// We don't delete profiles at the moment!
profile.Name = "Deleted user";
return false;
}
if (m.Category == propCat)
m.Category = null;
}));
JsonFile.Characters.ForEach(n => n.PropertyCategories.Remove(propCat));
return true;
}
else if (obj is JsonGroup group)
{
JsonFile.Groups.Remove(group);
return true;
}
else if (obj is JsonUserAccount account)
{
JsonFile.UserAccounts.Remove(account);
return true;
}
else if (obj is JsonUserProfile profile)
{
// We don't delete profiles at the moment!
profile.Name = "Deleted user";
return false;
}
public bool SetParent(UserProfile profile, UserAccount parent)
{
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
{
jaccount.Profile = jprofile;
return true;
}
return false;
}
return false;
}
public bool SetParent(Character character, Group parent)
public bool SetParent(UserProfile profile, UserAccount parent)
{
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
{
if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup)
{
if (!jgroup.Characters.Contains(jcharacter))
jgroup.Characters.Add(jcharacter);
return true;
}
return false;
jaccount.Profile = jprofile;
return true;
}
return false;
}
public bool SetParent(Property property, Character character)
public bool SetParent(Character character, Group parent)
{
if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup)
{
if (property is JsonProp jprop && character is JsonCharacter jcharacter)
{
if (!jcharacter.Properties.Contains(jprop))
jcharacter.Properties.Add(jprop);
return true;
}
return false;
if (!jgroup.Characters.Contains(jcharacter))
jgroup.Characters.Add(jcharacter);
return true;
}
return false;
}
public bool SetOwner(Group group, UserProfile owner)
public bool SetParent(Property property, Character character)
{
if (property is JsonProp jprop && character is JsonCharacter jcharacter)
{
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
{
jgroup.Owner = jprofile;
return true;
}
return false;
if (!jcharacter.Properties.Contains(jprop))
jcharacter.Properties.Add(jprop);
return true;
}
return false;
}
public bool SetOwner(Character character, UserProfile owner)
public bool SetOwner(Group group, UserProfile owner)
{
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
{
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
{
jcharacter.Owner = jprofile;
return true;
}
return false;
jgroup.Owner = jprofile;
return true;
}
return false;
}
public UserAccount? GetUserAccount(string username, string password)
public bool SetOwner(Character character, UserProfile owner)
{
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username && n.Password == password);
jcharacter.Owner = jprofile;
return true;
}
return false;
}
public UserProfile? GetUserProfile(string username)
{
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
}
public UserAccount? GetUserAccount(string username, string password)
{
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username && n.Password == password);
}
public IEnumerable<UserProfile>? GetGroupMembers(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Members;
return null;
}
public UserProfile? GetUserProfile(string username)
{
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
}
public UserProfile? GetOwner(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Owner;
return null;
}
public IEnumerable<UserProfile>? GetGroupMembers(Group group)
{
if (group is JsonGroup jgroup)
return jgroup.Members;
return null;
}
public UserProfile? GetOwner(Character character)
{
if (character is JsonCharacter jcharacter)
return jcharacter.Owner;
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;
}
}

View File

@@ -1,11 +1,10 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonCharacter : Character
{
public class JsonCharacter : Character
{
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonProp> Properties { get; } = [];
public virtual List<JsonPropCat> PropertyCategories { get; } = [];
}
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonProp> Properties { get; } = [];
public virtual List<JsonPropCat> PropertyCategories { get; } = [];
}

View File

@@ -1,11 +1,10 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonGroup : Group
{
public class JsonGroup : Group
{
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonUserProfile> Members { get; } = [];
public virtual List<JsonCharacter> Characters { get; } = [];
}
public virtual JsonUserProfile? Owner { get; set; }
public virtual List<JsonUserProfile> Members { get; } = [];
public virtual List<JsonCharacter> Characters { get; } = [];
}

View File

@@ -1,9 +1,8 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonProp : Property
{
public class JsonProp : Property
{
public virtual JsonPropCat? Category { get; set; } = null;
}
public virtual JsonPropCat? Category { get; set; } = null;
}

View File

@@ -1,8 +1,7 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonPropCat : PropertyCategory
{
public class JsonPropCat : PropertyCategory
{
}
}

View File

@@ -2,13 +2,12 @@
using Newtonsoft.Json.Converters;
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
{
public class JsonUserAccount : UserAccount
{
public virtual JsonUserProfile? Profile { get; set; }
namespace OwnChar.Data.Providers.JsonFile.Model;
[JsonConverter(typeof(StringEnumConverter))]
public override UserType Type { get => base.Type; set => base.Type = value; }
}
public class JsonUserAccount : UserAccount
{
public virtual JsonUserProfile? Profile { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public override UserType Type { get => base.Type; set => base.Type = value; }
}

View File

@@ -1,8 +1,7 @@
using OwnChar.Model;
namespace OwnChar.Data.Providers.JsonFile.Model
namespace OwnChar.Data.Providers.JsonFile.Model;
public class JsonUserProfile : UserProfile
{
public class JsonUserProfile : UserProfile
{
}
}