code cleanup
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user