diff --git a/OwnChar/Data/IDataManager.cs b/OwnChar/Data/IDataManager.cs index e629549..16f8819 100644 --- a/OwnChar/Data/IDataManager.cs +++ b/OwnChar/Data/IDataManager.cs @@ -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? GetMembers(Group group); - } + // Group management + abstract UserProfile? GetOwner(Group group); + abstract IEnumerable? GetMembers(Group group); } diff --git a/OwnChar/Data/IDataProvider.cs b/OwnChar/Data/IDataProvider.cs index 3550827..250ca49 100644 --- a/OwnChar/Data/IDataProvider.cs +++ b/OwnChar/Data/IDataProvider.cs @@ -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() where T : class, IOwnCharObject; - abstract bool Save(T obj) where T : class, IOwnCharObject; - abstract bool Delete(T obj) where T : class, IOwnCharObject; + // 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; - // 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? 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? GetGroupMembers(Group group); + abstract UserProfile? GetOwner(Group group); + abstract UserProfile? GetOwner(Character character); } diff --git a/OwnChar/Data/Managers/ClientDataManager.cs b/OwnChar/Data/Managers/ClientDataManager.cs index e8ba8c1..2f97ae4 100644 --- a/OwnChar/Data/Managers/ClientDataManager.cs +++ b/OwnChar/Data/Managers/ClientDataManager.cs @@ -1,6 +1,5 @@ -namespace OwnChar.Data.Managers +namespace OwnChar.Data.Managers; + +public class ClientDataManager { - public class ClientDataManager - { - } } diff --git a/OwnChar/Data/Managers/DefaultDataManager.cs b/OwnChar/Data/Managers/DefaultDataManager.cs index dafe058..04c3761 100644 --- a/OwnChar/Data/Managers/DefaultDataManager.cs +++ b/OwnChar/Data/Managers/DefaultDataManager.cs @@ -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? 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? 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(); + var profile = DataProvider.Create(); + var group = DataProvider.Create(); - 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(); - var profile = DataProvider.Create(); - var group = DataProvider.Create(); + 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; } } diff --git a/OwnChar/Data/Providers/JsonFile/JsonFile.cs b/OwnChar/Data/Providers/JsonFile/JsonFile.cs index 3d3ea27..a8edcd6 100644 --- a/OwnChar/Data/Providers/JsonFile/JsonFile.cs +++ b/OwnChar/Data/Providers/JsonFile/JsonFile.cs @@ -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 UserAccounts { get; } = []; - public List Characters { get; } = []; - public List Groups { get; } = []; - } + 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 index 7493df3..83f1f8a 100644 --- a/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs +++ b/OwnChar/Data/Providers/JsonFile/JsonFileDataProvider.cs @@ -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(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() 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 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 obj) where T : class, IOwnCharObject + { + if (obj is JsonCharacter character) { - if (File.Exists(JsonFilePath) && 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); - } - + 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 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? 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? 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; } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs index ca9baa1..f2ac565 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonCharacter.cs @@ -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 Properties { get; } = []; - public virtual List PropertyCategories { get; } = []; - } + public virtual JsonUserProfile? Owner { get; set; } + public virtual List Properties { get; } = []; + public virtual List PropertyCategories { get; } = []; } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs index 3f4c0ec..62f1aaf 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonGroup.cs @@ -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 Members { get; } = []; - public virtual List Characters { get; } = []; - } + public virtual JsonUserProfile? Owner { get; set; } + public virtual List Members { get; } = []; + public virtual List Characters { get; } = []; } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs index 42df6b8..23f030f 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonProp.cs @@ -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; } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs index 746ec8e..fd0a558 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonPropCat.cs @@ -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 - { - } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs index 042c6a8..80ee01b 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonUserAccount.cs @@ -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; } } diff --git a/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs b/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs index c204936..e0b0039 100644 --- a/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs +++ b/OwnChar/Data/Providers/JsonFile/Model/JsonUserProfile.cs @@ -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 - { - } } diff --git a/OwnChar/Manager/CharacterManager.cs b/OwnChar/Manager/CharacterManager.cs index a2764af..2c05234 100644 --- a/OwnChar/Manager/CharacterManager.cs +++ b/OwnChar/Manager/CharacterManager.cs @@ -1,19 +1,18 @@ using OwnChar.Model; -namespace OwnChar.Manager +namespace OwnChar.Manager; + +public class CharacterManager(OwnCharManager manager) { - public class CharacterManager(OwnCharManager manager) + public OwnCharManager Manager { get; } = manager; + + public IEnumerable? GetCharacters(Group? group) { - public OwnCharManager Manager { get; } = manager; + throw new NotImplementedException(); + } - public IEnumerable? GetCharacters(Group? group) - { - throw new NotImplementedException(); - } - - public IEnumerable? GetCharacters(UserProfile? profile) - { - throw new NotImplementedException(); - } + public IEnumerable? GetCharacters(UserProfile? profile) + { + throw new NotImplementedException(); } } diff --git a/OwnChar/Manager/Exceptions/LoginException.cs b/OwnChar/Manager/Exceptions/LoginException.cs index 7bc6712..f6b2803 100644 --- a/OwnChar/Manager/Exceptions/LoginException.cs +++ b/OwnChar/Manager/Exceptions/LoginException.cs @@ -1,6 +1,5 @@ -namespace OwnChar.Manager.Exceptions +namespace OwnChar.Manager.Exceptions; + +public class LoginException(string message) : Exception(message) { - public class LoginException(string message) : Exception(message) - { - } } diff --git a/OwnChar/Manager/GroupsManager.cs b/OwnChar/Manager/GroupsManager.cs index d935b9c..d5287e6 100644 --- a/OwnChar/Manager/GroupsManager.cs +++ b/OwnChar/Manager/GroupsManager.cs @@ -1,21 +1,20 @@ using OwnChar.Model; -namespace OwnChar.Manager +namespace OwnChar.Manager; + +public class GroupsManager(OwnCharManager manager) { - public class GroupsManager(OwnCharManager manager) + public OwnCharManager Manager { get; } = manager; + + public UserProfile? GetOwner(Group? group) { - public OwnCharManager Manager { get; } = manager; + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataManager?.GetOwner(group); + } - public UserProfile? GetOwner(Group? group) - { - ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataManager?.GetOwner(group); - } - - public IEnumerable? GetMembers(Group? group) - { - ArgumentNullException.ThrowIfNull(group, nameof(group)); - return Manager.DataManager?.GetMembers(group); - } + public IEnumerable? GetMembers(Group? group) + { + ArgumentNullException.ThrowIfNull(group, nameof(group)); + return Manager.DataManager?.GetMembers(group); } } diff --git a/OwnChar/Manager/OwnCharManager.cs b/OwnChar/Manager/OwnCharManager.cs index 1918a81..405e34e 100644 --- a/OwnChar/Manager/OwnCharManager.cs +++ b/OwnChar/Manager/OwnCharManager.cs @@ -3,58 +3,57 @@ using OwnChar.Manager.Exceptions; using OwnChar.Model; using Pilz.Cryptography; -namespace OwnChar.Manager +namespace OwnChar.Manager; + +public class OwnCharManager { - public class OwnCharManager + // User + public bool IsLoggedIn => CurrentUser != null; + public UserAccount? CurrentUser { get; private set; } + + // Data Provider + public IDataManager? DataManager { get; set; } + + // Manager + public UserManager Users { get; } + public GroupsManager Groups { get; } + public CharacterManager Characters { get; } + + public OwnCharManager() { - // User - public bool IsLoggedIn => CurrentUser != null; - public UserAccount? CurrentUser { get; private set; } + Users = new(this); + Groups = new(this); + Characters = new(this); + } - // Data Provider - public IDataManager? DataManager { get; set; } + internal protected void CheckLogin() + { + if (!IsLoggedIn) + throw new LoginException("You are already logged in!"); + } - // Manager - public UserManager Users { get; } - public GroupsManager Groups { get; } - public CharacterManager Characters { get; } + /// + /// Tries to login on the given data provider. + /// + /// Returns if the login was successfull and if not. + public bool Login(IDataManager? proxy, string? username, SecureString? password) + { + ArgumentNullException.ThrowIfNull(proxy, nameof(proxy)); + ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); + ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); - public OwnCharManager() - { - Users = new(this); - Groups = new(this); - Characters = new(this); - } + username = username.Trim().ToLower(); + CurrentUser = proxy.Login(username, Utils.HashPassword(username, password)); - internal protected void CheckLogin() - { - if (!IsLoggedIn) - throw new LoginException("You are already logged in!"); - } + return IsLoggedIn; + } - /// - /// Tries to login on the given data provider. - /// - /// Returns if the login was successfull and if not. - public bool Login(IDataManager? proxy, string? username, SecureString? password) - { - ArgumentNullException.ThrowIfNull(proxy, nameof(proxy)); - ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); - ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); - - username = username.Trim().ToLower(); - CurrentUser = proxy.Login(username, Utils.HashPassword(username, password)); - - return IsLoggedIn; - } - - /// - /// Closes the session on the current data provider. - /// - /// - public bool Logout() - { - return DataManager?.Logout(CurrentUser) ?? true; - } + /// + /// Closes the session on the current data provider. + /// + /// + public bool Logout() + { + return DataManager?.Logout(CurrentUser) ?? true; } } diff --git a/OwnChar/Manager/UserManager.cs b/OwnChar/Manager/UserManager.cs index 7510f36..ccfec18 100644 --- a/OwnChar/Manager/UserManager.cs +++ b/OwnChar/Manager/UserManager.cs @@ -1,30 +1,29 @@ using OwnChar.Model; using Pilz.Cryptography; -namespace OwnChar.Manager +namespace OwnChar.Manager; + +public class UserManager(OwnCharManager manager) { - public class UserManager(OwnCharManager manager) + public OwnCharManager Manager { get; } = manager; + + public UserProfile? GetOwnUserProfile() { - public OwnCharManager Manager { get; } = manager; + Manager.CheckLogin(); + return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!); + } - public UserProfile? GetOwnUserProfile() - { - Manager.CheckLogin(); - return Manager.DataManager!.GetUserProfile(Manager.CurrentUser!); - } + public UserAccount? CreateUserAccount(string? username, SecureString? password) + { + ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); + ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); + username = username.Trim().ToLower(); + return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password)); + } - public UserAccount? CreateUserAccount(string? username, SecureString? password) - { - ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username)); - ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password)); - username = username.Trim().ToLower(); - return Manager.DataManager?.CreateUserAccount(username, Utils.HashPassword(username, password)); - } - - public bool DeleteUserAccount(UserAccount? account) - { - ArgumentNullException.ThrowIfNull(account, nameof(account)); - return Manager.DataManager?.DeleteUserAccount(account) ?? false; - } + public bool DeleteUserAccount(UserAccount? account) + { + ArgumentNullException.ThrowIfNull(account, nameof(account)); + return Manager.DataManager?.DeleteUserAccount(account) ?? false; } } diff --git a/OwnChar/Model/Character.cs b/OwnChar/Model/Character.cs index 3676e72..36d33ca 100644 --- a/OwnChar/Model/Character.cs +++ b/OwnChar/Model/Character.cs @@ -1,7 +1,6 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class Character : IOwnCharObject { - public abstract class Character : IOwnCharObject - { - public virtual string? Name { get; set; } - } + public virtual string? Name { get; set; } } diff --git a/OwnChar/Model/Group.cs b/OwnChar/Model/Group.cs index 0486a8e..c8757a1 100644 --- a/OwnChar/Model/Group.cs +++ b/OwnChar/Model/Group.cs @@ -1,8 +1,7 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class Group : IOwnCharObject { - public abstract class Group : IOwnCharObject - { - public virtual string? Name { get; set; } - public virtual bool IsInternal { 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 index 190e8ba..30078e9 100644 --- a/OwnChar/Model/IOwnCharObject.cs +++ b/OwnChar/Model/IOwnCharObject.cs @@ -1,6 +1,5 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public interface IOwnCharObject { - public interface IOwnCharObject - { - } } diff --git a/OwnChar/Model/Property.cs b/OwnChar/Model/Property.cs index 858997c..b16f31f 100644 --- a/OwnChar/Model/Property.cs +++ b/OwnChar/Model/Property.cs @@ -1,8 +1,7 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class Property : IOwnCharObject { - public abstract class Property : IOwnCharObject - { - public virtual string? Name { get; set; } - public virtual 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 ee91d58..c3ff4b9 100644 --- a/OwnChar/Model/PropertyCategory.cs +++ b/OwnChar/Model/PropertyCategory.cs @@ -1,7 +1,6 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class PropertyCategory : IOwnCharObject { - public abstract class PropertyCategory : IOwnCharObject - { - public virtual string? Name { get; set; } - } + public virtual string? Name { get; set; } } diff --git a/OwnChar/Model/UserAccount.cs b/OwnChar/Model/UserAccount.cs index 52d82df..8b38ec9 100644 --- a/OwnChar/Model/UserAccount.cs +++ b/OwnChar/Model/UserAccount.cs @@ -1,10 +1,9 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class UserAccount : IOwnCharObject { - public abstract class UserAccount : IOwnCharObject - { - public virtual string? Username { get; set; } - public virtual string? Password { get; set; } - public virtual string? Email { get; set; } - public virtual UserType Type { get; set; } - } + public virtual string? Username { get; set; } + public virtual string? Password { get; set; } + public virtual string? Email { get; set; } + public virtual UserType Type { get; set; } } diff --git a/OwnChar/Model/UserProfile.cs b/OwnChar/Model/UserProfile.cs index 91ad514..99ac413 100644 --- a/OwnChar/Model/UserProfile.cs +++ b/OwnChar/Model/UserProfile.cs @@ -1,7 +1,6 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public abstract class UserProfile : IOwnCharObject { - public abstract class UserProfile : IOwnCharObject - { - public virtual string? Name { get; set; } - } + public virtual string? Name { get; set; } } diff --git a/OwnChar/Model/UserType.cs b/OwnChar/Model/UserType.cs index c0fd354..4a9437d 100644 --- a/OwnChar/Model/UserType.cs +++ b/OwnChar/Model/UserType.cs @@ -1,9 +1,8 @@ -namespace OwnChar.Model +namespace OwnChar.Model; + +public enum UserType { - public enum UserType - { - Guest, - User, - Admin - } + Guest, + User, + Admin } diff --git a/OwnChar/Utils.cs b/OwnChar/Utils.cs index bfb072c..809dc6c 100644 --- a/OwnChar/Utils.cs +++ b/OwnChar/Utils.cs @@ -1,13 +1,12 @@ using Pilz.Cryptography; -namespace OwnChar +namespace OwnChar; + +public static class Utils { - public static class Utils + public static string HashPassword(string username, SecureString password) { - public static string HashPassword(string username, SecureString password) - { - // TODO: Implement a good hasing algorythmus (like MD5) BEFORE going productive! - return (username + ":" + password).GetHashCode().ToString(); - } + // TODO: Implement a good hasing algorythmus (like MD5) BEFORE going productive! + return (username + ":" + password).GetHashCode().ToString(); } }