From 991987bb6dfda98ede8ae1f5ad905dc4e4c64f1a Mon Sep 17 00:00:00 2001 From: Pilzinsel64 Date: Tue, 28 May 2024 15:18:27 +0200 Subject: [PATCH] yea boy, now I got it! --- .../ClientServer/ClientServerDataProvider.cs | 30 ---- OwnChar/Data/IDataProvider.cs | 29 ++-- OwnChar/Data/IDataProxy.cs | 19 +++ OwnChar/Data/JsonFile/JsonFile.cs | 14 -- OwnChar/Data/JsonFile/JsonFileDataProvider.cs | 87 ---------- OwnChar/Data/Managers/ClientDataManager.cs | 6 + OwnChar/Data/Managers/DefaultDataManager.cs | 73 +++++++++ OwnChar/Data/Providers/JsonFile/JsonFile.cs | 12 ++ .../JsonFile/JsonFileDataProvider.cs | 148 ++++++++++++++++++ .../Providers/JsonFile/Model/JsonCharacter.cs | 10 ++ .../Providers/JsonFile/Model/JsonGroup.cs | 11 ++ .../Data/Providers/JsonFile/Model/JsonProp.cs | 9 ++ .../Providers/JsonFile/Model/JsonPropCat.cs | 8 + .../JsonFile/Model/JsonUserAccount.cs | 9 ++ .../JsonFile/Model/JsonUserProfile.cs | 8 + OwnChar/Manager/CharacterManager.cs | 49 +----- OwnChar/Manager/GroupsManager.cs | 41 +---- OwnChar/Manager/OwnCharManager.cs | 24 +-- OwnChar/Manager/UserManager.cs | 48 +----- OwnChar/Model/Character.cs | 8 +- OwnChar/Model/Group.cs | 15 +- OwnChar/Model/IOwnCharObject.cs | 6 + OwnChar/Model/Property.cs | 8 +- OwnChar/Model/PropertyCategory.cs | 5 +- OwnChar/Model/UserAccount.cs | 10 +- OwnChar/Model/UserProfile.cs | 6 +- OwnChar/OwnChar.csproj | 4 + OwnChar/Utils.cs | 11 ++ 28 files changed, 396 insertions(+), 312 deletions(-) delete mode 100644 OwnChar/Data/ClientServer/ClientServerDataProvider.cs create mode 100644 OwnChar/Data/IDataProxy.cs delete mode 100644 OwnChar/Data/JsonFile/JsonFile.cs delete mode 100644 OwnChar/Data/JsonFile/JsonFileDataProvider.cs create mode 100644 OwnChar/Data/Managers/ClientDataManager.cs create mode 100644 OwnChar/Data/Managers/DefaultDataManager.cs create mode 100644 OwnChar/Data/Providers/JsonFile/JsonFile.cs create mode 100644 OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs create mode 100644 OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs create mode 100644 OwnChar/Model/IOwnCharObject.cs create mode 100644 OwnChar/Utils.cs diff --git a/OwnChar/Data/ClientServer/ClientServerDataProvider.cs b/OwnChar/Data/ClientServer/ClientServerDataProvider.cs deleted file mode 100644 index 3731ccc..0000000 --- a/OwnChar/Data/ClientServer/ClientServerDataProvider.cs +++ /dev/null @@ -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(); - } - } -} diff --git a/OwnChar/Data/IDataProvider.cs b/OwnChar/Data/IDataProvider.cs index 7513991..1266c1e 100644 --- a/OwnChar/Data/IDataProvider.cs +++ b/OwnChar/Data/IDataProvider.cs @@ -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() where T : class, IOwnCharObject; + abstract bool Save(T obj) where T : class, IOwnCharObject; + abstract bool Delete(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 GetGroupMembers(Group group); + abstract UserProfile? GetOwner(Group group); + abstract UserProfile? GetOwner(Character character); } } diff --git a/OwnChar/Data/IDataProxy.cs b/OwnChar/Data/IDataProxy.cs new file mode 100644 index 0000000..3c5f64e --- /dev/null +++ b/OwnChar/Data/IDataProxy.cs @@ -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 GetMembers(Group group); + } +} diff --git a/OwnChar/Data/JsonFile/JsonFile.cs b/OwnChar/Data/JsonFile/JsonFile.cs deleted file mode 100644 index 581e8b1..0000000 --- a/OwnChar/Data/JsonFile/JsonFile.cs +++ /dev/null @@ -1,14 +0,0 @@ -using OwnChar.Model; - -namespace OwnChar.Data.JsonFile -{ - public class JsonFile - { - public List UserAccounts { get; } = []; - public List UserProfiles { get; } = []; - public List Characters { get; } = []; - public List Groups { get; } = []; - public List Properties { get; } = []; - public List PropertyCategories { get; } = []; - } -} diff --git a/OwnChar/Data/JsonFile/JsonFileDataProvider.cs b/OwnChar/Data/JsonFile/JsonFileDataProvider.cs deleted file mode 100644 index 3ebab76..0000000 --- a/OwnChar/Data/JsonFile/JsonFileDataProvider.cs +++ /dev/null @@ -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(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); - } - } -} diff --git a/OwnChar/Data/Managers/ClientDataManager.cs b/OwnChar/Data/Managers/ClientDataManager.cs new file mode 100644 index 0000000..e8ba8c1 --- /dev/null +++ b/OwnChar/Data/Managers/ClientDataManager.cs @@ -0,0 +1,6 @@ +namespace OwnChar.Data.Managers +{ + public class ClientDataManager + { + } +} diff --git a/OwnChar/Data/Managers/DefaultDataManager.cs b/OwnChar/Data/Managers/DefaultDataManager.cs new file mode 100644 index 0000000..9a7e3cd --- /dev/null +++ b/OwnChar/Data/Managers/DefaultDataManager.cs @@ -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(); + var profile = DataProvider.Create(); + var group = DataProvider.Create(); + + 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 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; + } + } +} diff --git a/OwnChar/Data/Providers/JsonFile/JsonFile.cs b/OwnChar/Data/Providers/JsonFile/JsonFile.cs new file mode 100644 index 0000000..3d3ea27 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/JsonFile.cs @@ -0,0 +1,12 @@ +using OwnChar.Data.Providers.JsonFile.Model; +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile +{ + public class JsonFile + { + public List UserAccounts { get; } = []; + public List Characters { get; } = []; + public List Groups { get; } = []; + } +} diff --git a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs new file mode 100644 index 0000000..b69e0b0 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs @@ -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(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() 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 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 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 GetGroupMembers(Group group) + { + throw new NotImplementedException(); + } + + public UserProfile? GetOwner(Group group) + { + throw new NotImplementedException(); + } + + public UserProfile? GetOwner(Character character) + { + throw new NotImplementedException(); + } + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs new file mode 100644 index 0000000..12cf538 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs @@ -0,0 +1,10 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonCharacter : Character + { + public List Properties { get; } = []; + public List PropertyCategories { get; } = []; + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs new file mode 100644 index 0000000..8bb4461 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs @@ -0,0 +1,11 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonGroup : Group + { + public List Owner { get; } = []; + public List Members { get; } = []; + public List Characters { get; } = []; + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs new file mode 100644 index 0000000..ab75e75 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs @@ -0,0 +1,9 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonProp : Property + { + public JsonPropCat? Category { get; set; } = null; + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs new file mode 100644 index 0000000..746ec8e --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs @@ -0,0 +1,8 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonPropCat : PropertyCategory + { + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs new file mode 100644 index 0000000..d16aaa1 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs @@ -0,0 +1,9 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonUserAccount : UserAccount + { + public JsonUserProfile? Profile { get; set; } + } +} diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs new file mode 100644 index 0000000..c204936 --- /dev/null +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs @@ -0,0 +1,8 @@ +using OwnChar.Model; + +namespace OwnChar.Data.Providers.JsonFile.Model +{ + public class JsonUserProfile : UserProfile + { + } +} diff --git a/OwnChar/Manager/CharacterManager.cs b/OwnChar/Manager/CharacterManager.cs index 10f806d..8e0c64d 100644 --- a/OwnChar/Manager/CharacterManager.cs +++ b/OwnChar/Manager/CharacterManager.cs @@ -1,54 +1,7 @@ -using OwnChar.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Manager +namespace OwnChar.Manager { public class CharacterManager(OwnCharManager manager) { public OwnCharManager Manager { get; } = manager; - - /// - /// Gets all personal characters (private & within groups). - /// - /// - /// Returns a collection with personal characters. - public IEnumerable? GetCharacters() - { - if (!IsLoggedIn || UserProfile == null) - return null; - - return GetCharacters(Manager.CurrentUser); - } - - /// - /// Gets all public ore accessable (e.g. via shares) characters from the specified group. - /// - /// - /// Returns a collection with characters for the specified group. - public IEnumerable GetCharacters(Group group) - { - } - - /// - /// Saves a character. If it doesn't exists, it will be created. - /// - /// The caracter to save. - /// if success, otherwise . - public bool SaveCharacter(Character character) - { - } - - /// - /// Deletes a given character. If it doesn't exists, it will be ignored. - /// - /// - /// if the given character has been deleted successfully or doesn't exist, otherwise . - public bool DeleteCharacter(Character character) - { - } } } diff --git a/OwnChar/Manager/GroupsManager.cs b/OwnChar/Manager/GroupsManager.cs index 914f7ad..b5ebbd9 100644 --- a/OwnChar/Manager/GroupsManager.cs +++ b/OwnChar/Manager/GroupsManager.cs @@ -1,9 +1,4 @@ using OwnChar.Model; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace OwnChar.Manager { @@ -11,40 +6,16 @@ namespace OwnChar.Manager { public OwnCharManager Manager { get; } = manager; - public Group? GetGroup() + public UserProfile? GetOwner(Group? group) { - if (!IsLoggedIn || UserProfile == null) - return null; - - // ... + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataProxy?.GetOwner(group); } - public IEnumerable? GetGroups() - { - if (!IsLoggedIn || UserProfile == null) - return null; - - return GetGroups(UserProfile); - } - - public IEnumerable? GetGroups(UserProfile profile) - { - if (!IsLoggedIn) - return null; - - // ... - } - - public Group? CreateGroup(UserProfile owner) - { - } - - public bool SaveGroup(Group group) - { - } - - public bool DeleteGroup(Group group) + public IEnumerable? GetMembers(Group? group) { + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataProxy?.GetMembers(group); } } } diff --git a/OwnChar/Manager/OwnCharManager.cs b/OwnChar/Manager/OwnCharManager.cs index e2232c5..cf30ebc 100644 --- a/OwnChar/Manager/OwnCharManager.cs +++ b/OwnChar/Manager/OwnCharManager.cs @@ -8,11 +8,11 @@ namespace OwnChar.Manager public class OwnCharManager { // User - public bool IsLoggedIn => DataProvider != null && DataProvider.IsLoggedIn; - public UserAccount? CurrentUser => DataProvider?.CurrentUserAccount; + public bool IsLoggedIn => CurrentUser != null; + public UserAccount? CurrentUser { get; private set; } // Data Provider - public IDataProvider? DataProvider { get; set; } + public IDataProxy? DataProxy { get; set; } // Manager public UserManager Users { get; } @@ -26,7 +26,7 @@ namespace OwnChar.Manager Characters = new(this); } - private void CheckLogin() + internal protected void CheckLogin() { if (!IsLoggedIn) throw new LoginException("You are already logged in!"); @@ -36,11 +36,15 @@ namespace OwnChar.Manager /// Tries to login on the given data provider. /// /// Returns if the login was successfull and if not. - public bool Login(IDataProvider? dataProvider, string username, SecureString password) + public bool Login(IDataProxy? proxy, string? username, SecureString? password) { - CheckLogin(); - ArgumentNullException.ThrowIfNull(dataProvider); - return dataProvider.Login(username, password) != null; + ArgumentNullException.ThrowIfNull(proxy, nameof(proxy)); + ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); + ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); + + CurrentUser = proxy.Login(username, password); + + return IsLoggedIn; } /// @@ -49,9 +53,7 @@ namespace OwnChar.Manager /// public bool Logout() { - if (DataProvider != null) - return DataProvider.Logout(); - return true; + return DataProxy?.Logout(CurrentUser) ?? true; } } } diff --git a/OwnChar/Manager/UserManager.cs b/OwnChar/Manager/UserManager.cs index 03b1195..efd3b03 100644 --- a/OwnChar/Manager/UserManager.cs +++ b/OwnChar/Manager/UserManager.cs @@ -1,5 +1,4 @@ using OwnChar.Model; -using Pilz.Cryptography; namespace OwnChar.Manager { @@ -7,50 +6,17 @@ namespace OwnChar.Manager { public OwnCharManager Manager { get; } = manager; - public UserProfile? GetUserProfile(UserAccount account) + public UserProfile? GetOwnUserProfile(UserAccount account) { + Manager.CheckLogin(); + return Manager.DataProxy!.GetUserProfile(Manager.CurrentUser!); } - public UserAccount? CreateAccount(string username, SecureString password, string email, string displayName) - { - // Create account - // ... - - // Create profile - // ... - } - - /// - /// Saves the current logged in user account. - /// - /// if success, otherwise . - public bool SaveAccount() - { - } - - public bool DeleteAccount() - { - } - - public UserProfile? CreateProfile() - { - // Create profile - // ... - - // Create pre-defined group - // ... - } - - /// - /// Saves the current logged in user profile. - /// - /// if success, otherwise . - public bool SaveProfile() - { - } - - public bool DeleteProfile() + public UserAccount? CreateUserAccount(string? username, string? password) { + ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); + ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); + return Manager.DataProxy?.CreateUserAccount(username, password); } } } diff --git a/OwnChar/Model/Character.cs b/OwnChar/Model/Character.cs index a34aefb..3676e72 100644 --- a/OwnChar/Model/Character.cs +++ b/OwnChar/Model/Character.cs @@ -1,11 +1,7 @@ namespace OwnChar.Model { - public class Character(Group group, string name) + public abstract class Character : IOwnCharObject { - public ulong Id { get; set; } - public string Name { get; set; } = name; - public Group Group { get; set; } = group; - public List Properties { get; set; } = []; - public List PropertyCategories { get; set; } = []; + public virtual string? Name { get; set; } } } diff --git a/OwnChar/Model/Group.cs b/OwnChar/Model/Group.cs index 41f776e..0486a8e 100644 --- a/OwnChar/Model/Group.cs +++ b/OwnChar/Model/Group.cs @@ -1,15 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace OwnChar.Model +namespace OwnChar.Model { - public class Group(UserProfile owner) + public abstract class Group : IOwnCharObject { - public int Id { get; set; } - public UserProfile Owner { get; set; } = owner; - public List Members { get; set; } = []; + public virtual string? Name { get; set; } + public virtual bool IsInternal { get; set; } } } diff --git a/OwnChar/Model/IOwnCharObject.cs b/OwnChar/Model/IOwnCharObject.cs new file mode 100644 index 0000000..190e8ba --- /dev/null +++ b/OwnChar/Model/IOwnCharObject.cs @@ -0,0 +1,6 @@ +namespace OwnChar.Model +{ + public interface IOwnCharObject + { + } +} diff --git a/OwnChar/Model/Property.cs b/OwnChar/Model/Property.cs index c660550..858997c 100644 --- a/OwnChar/Model/Property.cs +++ b/OwnChar/Model/Property.cs @@ -1,10 +1,8 @@ namespace OwnChar.Model { - public class Property(string name) + public abstract class Property : IOwnCharObject { - public ulong Id { get; set; } - public string Name { get; set; } = name; - public PropertyCategory? Category { get; set; } - public object? Value { get; set; } + public virtual string? Name { get; set; } + public virtual object? Value { get; set; } } } diff --git a/OwnChar/Model/PropertyCategory.cs b/OwnChar/Model/PropertyCategory.cs index c9c0e01..ee91d58 100644 --- a/OwnChar/Model/PropertyCategory.cs +++ b/OwnChar/Model/PropertyCategory.cs @@ -1,8 +1,7 @@ namespace OwnChar.Model { - public class PropertyCategory(string name) + public abstract class PropertyCategory : IOwnCharObject { - public ulong Id { get; set; } - public string Name { get; set; } = name; + public virtual string? Name { get; set; } } } diff --git a/OwnChar/Model/UserAccount.cs b/OwnChar/Model/UserAccount.cs index 0cdeb96..08b45de 100644 --- a/OwnChar/Model/UserAccount.cs +++ b/OwnChar/Model/UserAccount.cs @@ -1,11 +1,9 @@ namespace OwnChar.Model { - public class UserAccount + public abstract class UserAccount : IOwnCharObject { - public ulong Id { get; set; } - public string? Username { get; set; } - public string? Password { get; set; } - public string? Email { get; set; } - public UserProfile? Profile { get; set; } + public virtual string? Username { get; set; } + public virtual string? Password { get; set; } + public virtual string? Email { get; set; } } } diff --git a/OwnChar/Model/UserProfile.cs b/OwnChar/Model/UserProfile.cs index 2c8d0c8..91ad514 100644 --- a/OwnChar/Model/UserProfile.cs +++ b/OwnChar/Model/UserProfile.cs @@ -1,9 +1,7 @@ namespace OwnChar.Model { - public class UserProfile + public abstract class UserProfile : IOwnCharObject { - public ulong Id { get; set; } - public string? Name { get; set; } - public UserAccount? Account { get; set; } + public virtual string? Name { get; set; } } } diff --git a/OwnChar/OwnChar.csproj b/OwnChar/OwnChar.csproj index ccf8ed2..f1eee71 100644 --- a/OwnChar/OwnChar.csproj +++ b/OwnChar/OwnChar.csproj @@ -10,4 +10,8 @@ + + + + diff --git a/OwnChar/Utils.cs b/OwnChar/Utils.cs new file mode 100644 index 0000000..046e052 --- /dev/null +++ b/OwnChar/Utils.cs @@ -0,0 +1,11 @@ +namespace OwnChar +{ + public static class Utils + { + public static string HashPassword(string username, string password) + { + // TODO: Implement a good hasing algorythmus (like MD5) BEFORE going productive! + return (username + ":" + password).GetHashCode().ToString(); + } + } +}