yea boy, now I got it!
This commit is contained in:
@@ -1,30 +0,0 @@
|
||||
using OwnChar.Model;
|
||||
using Pilz.Cryptography;
|
||||
|
||||
namespace OwnChar.Data.ClientServer
|
||||
{
|
||||
public class ClientServerDataProvider : IDataProvider
|
||||
{
|
||||
public UserAccount? CurrentUserAccount => throw new NotImplementedException();
|
||||
|
||||
public ClientServerDataProvider(string serverAddress)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserProfile? GetUserProfile(string username)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserAccount? Login(string username, SecureString password)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool Logout()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,29 @@
|
||||
using OwnChar.Model;
|
||||
using Pilz.Cryptography;
|
||||
|
||||
namespace OwnChar.Data
|
||||
{
|
||||
public interface IDataProvider
|
||||
{
|
||||
// Properties
|
||||
public bool IsLoggedIn => CurrentUserAccount != null;
|
||||
public UserProfile? CurrentUserProfile => CurrentUserAccount?.Profile;
|
||||
// General
|
||||
abstract bool IsInitialized();
|
||||
|
||||
// Properties (abstract)
|
||||
abstract UserAccount? CurrentUserAccount { get; }
|
||||
// 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;
|
||||
|
||||
// Methods (abstract)
|
||||
abstract UserAccount? Initialize(bool force);
|
||||
// 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 UserAccount? Login(string username, string password);
|
||||
abstract bool Logout();
|
||||
abstract UserAccount? CreateUser(string username, string password);
|
||||
abstract IEnumerable<UserProfile> GetGroupMembers(Group group);
|
||||
abstract UserProfile? GetOwner(Group group);
|
||||
abstract UserProfile? GetOwner(Character character);
|
||||
}
|
||||
}
|
||||
|
||||
19
OwnChar/Data/IDataProxy.cs
Normal file
19
OwnChar/Data/IDataProxy.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data
|
||||
{
|
||||
public interface IDataProxy
|
||||
{
|
||||
// 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);
|
||||
|
||||
// Group management
|
||||
abstract UserProfile? GetOwner(Group group);
|
||||
abstract IEnumerable<UserProfile> GetMembers(Group group);
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.JsonFile
|
||||
{
|
||||
public class JsonFile
|
||||
{
|
||||
public List<UserAccount> UserAccounts { get; } = [];
|
||||
public List<UserProfile> UserProfiles { get; } = [];
|
||||
public List<Character> Characters { get; } = [];
|
||||
public List<Group> Groups { get; } = [];
|
||||
public List<Property> Properties { get; } = [];
|
||||
public List<PropertyCategory> PropertyCategories { get; } = [];
|
||||
}
|
||||
}
|
||||
@@ -1,87 +0,0 @@
|
||||
using Newtonsoft.Json;
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.JsonFile
|
||||
{
|
||||
public class JsonFileDataProvider : IDataProvider
|
||||
{
|
||||
public JsonFile JsonFile { get; protected set; }
|
||||
public string JsonFilePath { get; protected set; }
|
||||
public UserAccount? CurrentUserAccount { get; protected set; }
|
||||
|
||||
public JsonFileDataProvider(string filePath)
|
||||
{
|
||||
JsonFilePath = filePath;
|
||||
LoadFile();
|
||||
|
||||
// Get rid of bad compiler warnings
|
||||
if (JsonFile == null)
|
||||
throw new Exception("Something went incredible wrong at initialization of the JsonFile.");
|
||||
}
|
||||
|
||||
public UserAccount? Initialize(bool force)
|
||||
{
|
||||
if (force || JsonFile.UserAccounts.Count == 0)
|
||||
CreateUser("admin", "admin");
|
||||
return CurrentUserAccount;
|
||||
}
|
||||
|
||||
protected void LoadFile()
|
||||
{
|
||||
if (JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
|
||||
JsonFile = jsonFile;
|
||||
JsonFile ??= new();
|
||||
CurrentUserAccount = Initialize(false);
|
||||
}
|
||||
|
||||
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 UserAccount? Login(string username, string password)
|
||||
{
|
||||
return CurrentUserAccount = JsonFile.UserAccounts.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Username) && !string.IsNullOrWhiteSpace(n.Password) && n.Username == username && n.Password == password);
|
||||
}
|
||||
|
||||
public bool Logout()
|
||||
{
|
||||
CurrentUserAccount = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
public UserAccount? CreateUser(string username, string password)
|
||||
{
|
||||
var account = new UserAccount
|
||||
{
|
||||
Username = username,
|
||||
Password = password,
|
||||
Profile = new()
|
||||
{
|
||||
Name = username,
|
||||
}
|
||||
};
|
||||
|
||||
JsonFile.UserAccounts.Add(account);
|
||||
JsonFile.UserProfiles.Add(account.Profile);
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public UserProfile? GetUserProfile(string username)
|
||||
{
|
||||
return JsonFile.UserProfiles.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Account?.Username) && n.Account.Username == username);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
OwnChar/Data/Managers/ClientDataManager.cs
Normal file
6
OwnChar/Data/Managers/ClientDataManager.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace OwnChar.Data.Managers
|
||||
{
|
||||
public class ClientDataManager
|
||||
{
|
||||
}
|
||||
}
|
||||
73
OwnChar/Data/Managers/DefaultDataManager.cs
Normal file
73
OwnChar/Data/Managers/DefaultDataManager.cs
Normal file
@@ -0,0 +1,73 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Managers
|
||||
{
|
||||
public class DefaultDataManager : IDataProxy
|
||||
{
|
||||
public IDataProvider DataProvider { get; }
|
||||
|
||||
public DefaultDataManager(IDataProvider dataProvider)
|
||||
{
|
||||
DataProvider = dataProvider;
|
||||
}
|
||||
|
||||
public UserAccount? CreateUserAccount(string username, string password)
|
||||
{
|
||||
var account = DataProvider.Create<UserAccount>();
|
||||
var profile = DataProvider.Create<UserProfile>();
|
||||
var group = DataProvider.Create<Group>();
|
||||
|
||||
ArgumentNullException.ThrowIfNull(account, nameof(account));
|
||||
ArgumentNullException.ThrowIfNull(profile, nameof(profile));
|
||||
ArgumentNullException.ThrowIfNull(group, nameof(group));
|
||||
|
||||
account.Username = username;
|
||||
account.Password = password;
|
||||
|
||||
profile.Name = username;
|
||||
DataProvider.SetParent(profile, account);
|
||||
|
||||
group.IsInternal = true;
|
||||
DataProvider.SetOwner(group, profile);
|
||||
|
||||
DataProvider.Save(account);
|
||||
DataProvider.Save(profile);
|
||||
DataProvider.Save(group);
|
||||
|
||||
return account;
|
||||
}
|
||||
|
||||
public IEnumerable<UserProfile> GetMembers(Group group)
|
||||
{
|
||||
return DataProvider.GetGroupMembers(group);
|
||||
}
|
||||
|
||||
public UserProfile? GetOwner(Group group)
|
||||
{
|
||||
return DataProvider.GetOwner(group);
|
||||
}
|
||||
|
||||
public UserProfile? GetUserProfile(UserAccount account)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(account.Username, nameof(account.Username));
|
||||
return DataProvider.GetUserProfile(account.Username);
|
||||
}
|
||||
|
||||
public UserAccount? Login(string username, string password)
|
||||
{
|
||||
return DataProvider.GetUserAccount(username, password);
|
||||
}
|
||||
|
||||
public bool Logout(UserAccount? account)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool Initialize(bool force)
|
||||
{
|
||||
if (force || !DataProvider.IsInitialized())
|
||||
return CreateUserAccount("admin", "admin") != null;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
12
OwnChar/Data/Providers/JsonFile/JsonFile.cs
Normal file
12
OwnChar/Data/Providers/JsonFile/JsonFile.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using OwnChar.Data.Providers.JsonFile.Model;
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile
|
||||
{
|
||||
public class JsonFile
|
||||
{
|
||||
public List<JsonUserAccount> UserAccounts { get; } = [];
|
||||
public List<JsonCharacter> Characters { get; } = [];
|
||||
public List<JsonGroup> Groups { get; } = [];
|
||||
}
|
||||
}
|
||||
148
OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs
Normal file
148
OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs
Normal file
@@ -0,0 +1,148 @@
|
||||
using Newtonsoft.Json;
|
||||
using OwnChar.Data.Providers.JsonFile.Model;
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile
|
||||
{
|
||||
public class JsonFileDataProvider : IDataProvider
|
||||
{
|
||||
public JsonFile JsonFile { get; protected set; }
|
||||
public string JsonFilePath { get; protected set; }
|
||||
|
||||
public JsonFileDataProvider(string filePath)
|
||||
{
|
||||
JsonFilePath = filePath;
|
||||
LoadFile();
|
||||
|
||||
// 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 (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);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsInitialized()
|
||||
{
|
||||
return JsonFile.UserAccounts.Count != 0;
|
||||
}
|
||||
|
||||
bool IDataProvider.Delete<T>(T obj)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SetParent(UserProfile profile, UserAccount parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SetParent(Character character, Group parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SetParent(Property property, Character character)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SetOwner(Group group, UserProfile owner)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public bool SetOwner(Character group, UserProfile owner)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserAccount? GetUserAccount(string username, string password)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserProfile? GetUserProfile(string username)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IEnumerable<UserProfile> GetGroupMembers(Group group)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserProfile? GetOwner(Group group)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public UserProfile? GetOwner(Character character)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
10
OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs
Normal file
10
OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonCharacter : Character
|
||||
{
|
||||
public List<JsonProp> Properties { get; } = [];
|
||||
public List<JsonPropCat> PropertyCategories { get; } = [];
|
||||
}
|
||||
}
|
||||
11
OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs
Normal file
11
OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonGroup : Group
|
||||
{
|
||||
public List<JsonUserProfile> Owner { get; } = [];
|
||||
public List<JsonUserProfile> Members { get; } = [];
|
||||
public List<JsonCharacter> Characters { get; } = [];
|
||||
}
|
||||
}
|
||||
9
OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs
Normal file
9
OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonProp : Property
|
||||
{
|
||||
public JsonPropCat? Category { get; set; } = null;
|
||||
}
|
||||
}
|
||||
8
OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs
Normal file
8
OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonPropCat : PropertyCategory
|
||||
{
|
||||
}
|
||||
}
|
||||
9
OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs
Normal file
9
OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonUserAccount : UserAccount
|
||||
{
|
||||
public JsonUserProfile? Profile { get; set; }
|
||||
}
|
||||
}
|
||||
8
OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs
Normal file
8
OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonUserProfile : UserProfile
|
||||
{
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user