yea boy, now I got it!

This commit is contained in:
2024-05-28 15:18:27 +02:00
parent b08041204d
commit 991987bb6d
28 changed files with 396 additions and 312 deletions

View File

@@ -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<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.UserAccounts.Count != 0;
}
bool IDataProvider.Delete<T>(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<UserProfile> GetGroupMembers(Group group)
{
throw new NotImplementedException();
}
public UserProfile? GetOwner(Group group)
{
throw new NotImplementedException();
}
public UserProfile? GetOwner(Character character)
{
throw new NotImplementedException();
}
}
}