266 lines
7.4 KiB
C#
266 lines
7.4 KiB
C#
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 (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);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public bool IsInitialized()
|
|
{
|
|
return JsonFile.IsInitialized;
|
|
}
|
|
|
|
public void SetInitialized()
|
|
{
|
|
JsonFile.IsInitialized = true;
|
|
}
|
|
|
|
public bool Delete<T>(T obj) where T : class, IOwnCharObject
|
|
{
|
|
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 true;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
public bool SetParent(Character character, Group parent)
|
|
{
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
if (group is JsonGroup jgroup && owner is JsonUserProfile jprofile)
|
|
{
|
|
jgroup.Owner = jprofile;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool SetOwner(Character character, UserProfile owner)
|
|
{
|
|
if (character is JsonCharacter jcharacter && owner is JsonUserProfile jprofile)
|
|
{
|
|
jcharacter.Owner = jprofile;
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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 UserProfile? GetUserProfile(string username)
|
|
{
|
|
return JsonFile.UserAccounts.FirstOrDefault(n => n.Username == username)?.Profile;
|
|
}
|
|
|
|
public IEnumerable<UserProfile>? GetMembers(Group group)
|
|
{
|
|
if (group is JsonGroup jgroup)
|
|
return jgroup.Members;
|
|
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;
|
|
}
|
|
|
|
public IEnumerable<Character>? GetCharacters(Group group)
|
|
{
|
|
if (group is JsonGroup jgroup)
|
|
return jgroup.Characters;
|
|
return null;
|
|
}
|
|
|
|
public IEnumerable<Character>? GetCharacters(UserProfile profile)
|
|
{
|
|
if (profile is UserProfile jprofile)
|
|
return JsonFile.Characters.Where(n => n.Owner == profile);
|
|
return null;
|
|
}
|
|
|
|
public bool AddMember(Group group, UserProfile user)
|
|
{
|
|
if (group is JsonGroup jgroup && user is JsonUserProfile jprofile)
|
|
{
|
|
if (!jgroup.Members.Contains(jprofile))
|
|
jgroup.Members.Add(jprofile);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool RemoveMember(Group group, UserProfile user)
|
|
{
|
|
if (group is JsonGroup jgroup && user is JsonUserProfile jprofile)
|
|
{
|
|
jgroup.Members.Remove(jprofile);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public bool SaveDatabase()
|
|
{
|
|
SaveFile();
|
|
return true;
|
|
}
|
|
}
|