implement some missing method content
This commit is contained in:
@@ -11,9 +11,10 @@ namespace OwnChar.Data
|
||||
// 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);
|
||||
abstract IEnumerable<UserProfile>? GetMembers(Group group);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace OwnChar.Data
|
||||
// Gets
|
||||
abstract UserAccount? GetUserAccount(string username, string password);
|
||||
abstract UserProfile? GetUserProfile(string username);
|
||||
abstract IEnumerable<UserProfile> GetGroupMembers(Group group);
|
||||
abstract IEnumerable<UserProfile>? GetGroupMembers(Group group);
|
||||
abstract UserProfile? GetOwner(Group group);
|
||||
abstract UserProfile? GetOwner(Character character);
|
||||
}
|
||||
|
||||
@@ -2,42 +2,14 @@
|
||||
|
||||
namespace OwnChar.Data.Managers
|
||||
{
|
||||
public class DefaultDataManager : IDataManager
|
||||
public class DefaultDataManager(IDataProvider dataProvider) : IDataManager
|
||||
{
|
||||
public IDataProvider DataProvider { get; }
|
||||
private const string defaultUsername = "admin";
|
||||
private const string defaultPassword = "admin";
|
||||
|
||||
public DefaultDataManager(IDataProvider dataProvider)
|
||||
{
|
||||
DataProvider = dataProvider;
|
||||
}
|
||||
public IDataProvider DataProvider { get; } = 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)
|
||||
public IEnumerable<UserProfile>? GetMembers(Group group)
|
||||
{
|
||||
return DataProvider.GetGroupMembers(group);
|
||||
}
|
||||
@@ -66,7 +38,41 @@ namespace OwnChar.Data.Managers
|
||||
public bool Initialize(bool force)
|
||||
{
|
||||
if (force || !DataProvider.IsInitialized())
|
||||
return CreateUserAccount("admin", "admin") != null;
|
||||
return CreateUserAccount(defaultUsername, Utils.HashPassword(defaultUsername, defaultPassword)) != null;
|
||||
return true;
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using Newtonsoft.Json;
|
||||
using OwnChar.Data.Providers.JsonFile.Model;
|
||||
using OwnChar.Model;
|
||||
using System.Net.NetworkInformation;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile
|
||||
{
|
||||
@@ -90,59 +91,131 @@ namespace OwnChar.Data.Providers.JsonFile
|
||||
return JsonFile.UserAccounts.Count != 0;
|
||||
}
|
||||
|
||||
bool IDataProvider.Delete<T>(T obj)
|
||||
public bool Delete<T>(T obj) where T : class, IOwnCharObject
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (obj is JsonCharacter character)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SetParent(UserProfile profile, UserAccount parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (parent is JsonUserAccount jaccount && profile is JsonUserProfile jprofile)
|
||||
{
|
||||
jaccount.Profile = jprofile;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SetParent(Character character, Group parent)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (character is JsonCharacter jcharacter && parent is JsonGroup jgroup)
|
||||
{
|
||||
if (!jgroup.Characters.Contains(jcharacter))
|
||||
jgroup.Characters.Add(jcharacter);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SetParent(Property property, Character character)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (property is JsonProp jprop && character is JsonCharacter jcharacter)
|
||||
{
|
||||
if (!jcharacter.Properties.Contains(jprop))
|
||||
jcharacter.Properties.Add(jprop);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SetOwner(Group group, UserProfile owner)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
|
||||
{
|
||||
jgroup.Owner = jprofile;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool SetOwner(Character group, UserProfile owner)
|
||||
public bool SetOwner(Character character, UserProfile owner)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
|
||||
{
|
||||
jcharacter.Owner = jprofile;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public UserAccount? GetUserAccount(string username, string password)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(username, nameof(username));
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(password, nameof(password));
|
||||
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username && n.Password == password);
|
||||
}
|
||||
|
||||
public UserProfile? GetUserProfile(string username)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
|
||||
}
|
||||
|
||||
public IEnumerable<UserProfile> GetGroupMembers(Group group)
|
||||
public IEnumerable<UserProfile>? GetGroupMembers(Group group)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (group is JsonGroup jgroup)
|
||||
return jgroup.Members;
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserProfile? GetOwner(Group group)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (group is JsonGroup jgroup)
|
||||
return jgroup.Owner;
|
||||
return null;
|
||||
}
|
||||
|
||||
public UserProfile? GetOwner(Character character)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
if (character is JsonCharacter jcharacter)
|
||||
return jcharacter.Owner;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,8 @@ namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonCharacter : Character
|
||||
{
|
||||
public List<JsonProp> Properties { get; } = [];
|
||||
public List<JsonPropCat> PropertyCategories { get; } = [];
|
||||
public virtual JsonUserProfile? Owner { get; set; }
|
||||
public virtual List<JsonProp> Properties { get; } = [];
|
||||
public virtual List<JsonPropCat> PropertyCategories { get; } = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ 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; } = [];
|
||||
public virtual JsonUserProfile? Owner { get; set; }
|
||||
public virtual List<JsonUserProfile> Members { get; } = [];
|
||||
public virtual List<JsonCharacter> Characters { get; } = [];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,6 @@ namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonProp : Property
|
||||
{
|
||||
public JsonPropCat? Category { get; set; } = null;
|
||||
public virtual JsonPropCat? Category { get; set; } = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
using OwnChar.Model;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Converters;
|
||||
using OwnChar.Model;
|
||||
|
||||
namespace OwnChar.Data.Providers.JsonFile.Model
|
||||
{
|
||||
public class JsonUserAccount : UserAccount
|
||||
{
|
||||
public JsonUserProfile? Profile { get; set; }
|
||||
public virtual JsonUserProfile? Profile { get; set; }
|
||||
|
||||
[JsonConverter(typeof(StringEnumConverter))]
|
||||
public override UserType Type { get => base.Type; set => base.Type = value; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user