some work

This commit is contained in:
2024-05-27 22:07:25 +02:00
parent b2f0a80872
commit b08041204d
12 changed files with 196 additions and 91 deletions

View File

@@ -0,0 +1,30 @@
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Data.ClientServer
{
public class ClientServerDataProvider : IDataProvider
{
public UserAccount? CurrentUserAccount => throw new NotImplementedException();
public ClientServerDataProvider(string serverAddress)
{
throw new NotImplementedException();
}
public UserProfile? GetUserProfile(string username)
{
throw new NotImplementedException();
}
public UserAccount? Login(string username, SecureString password)
{
throw new NotImplementedException();
}
public bool Logout()
{
throw new NotImplementedException();
}
}
}

View File

@@ -1,52 +0,0 @@
using OwnChar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Data
{
public class ClientServerDataProvider : IDataProvider
{
public bool Save(Character character)
{
}
public bool Save(UserProfile profile)
{
}
public bool Save(UserAccount account)
{
}
public bool Save(Group group)
{
}
public bool Save(PropertyCategory category)
{
}
public bool Delete(Character character)
{
}
public bool Delete(UserProfile profile)
{
}
public bool Delete(UserAccount account)
{
}
public bool Delete(Group group)
{
}
public bool Delete(PropertyCategory category)
{
}
}
}

View File

@@ -1,12 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OwnChar.Model;
using Pilz.Cryptography;
namespace OwnChar.Data
{
public interface IDataProvider
{
// Properties
public bool IsLoggedIn => CurrentUserAccount != null;
public UserProfile? CurrentUserProfile => CurrentUserAccount?.Profile;
// Properties (abstract)
abstract UserAccount? CurrentUserAccount { get; }
// Methods (abstract)
abstract UserAccount? Initialize(bool force);
abstract UserProfile? GetUserProfile(string username);
abstract UserAccount? Login(string username, string password);
abstract bool Logout();
abstract UserAccount? CreateUser(string username, string password);
}
}

View File

@@ -0,0 +1,14 @@
using OwnChar.Model;
namespace OwnChar.Data.JsonFile
{
public class JsonFile
{
public List<UserAccount> UserAccounts { get; } = [];
public List<UserProfile> UserProfiles { get; } = [];
public List<Character> Characters { get; } = [];
public List<Group> Groups { get; } = [];
public List<Property> Properties { get; } = [];
public List<PropertyCategory> PropertyCategories { get; } = [];
}
}

View File

@@ -0,0 +1,87 @@
using Newtonsoft.Json;
using OwnChar.Model;
namespace OwnChar.Data.JsonFile
{
public class JsonFileDataProvider : IDataProvider
{
public JsonFile JsonFile { get; protected set; }
public string JsonFilePath { get; protected set; }
public UserAccount? CurrentUserAccount { 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.");
}
public UserAccount? Initialize(bool force)
{
if (force || JsonFile.UserAccounts.Count == 0)
CreateUser("admin", "admin");
return CurrentUserAccount;
}
protected void LoadFile()
{
if (JsonConvert.DeserializeObject<JsonFile>(File.ReadAllText(JsonFilePath), CreateJsonSerializerSettings()) is JsonFile jsonFile)
JsonFile = jsonFile;
JsonFile ??= new();
CurrentUserAccount = Initialize(false);
}
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 UserAccount? Login(string username, string password)
{
return CurrentUserAccount = JsonFile.UserAccounts.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Username) && !string.IsNullOrWhiteSpace(n.Password) && n.Username == username && n.Password == password);
}
public bool Logout()
{
CurrentUserAccount = null;
return true;
}
public UserAccount? CreateUser(string username, string password)
{
var account = new UserAccount
{
Username = username,
Password = password,
Profile = new()
{
Name = username,
}
};
JsonFile.UserAccounts.Add(account);
JsonFile.UserProfiles.Add(account.Profile);
return account;
}
public UserProfile? GetUserProfile(string username)
{
return JsonFile.UserProfiles.FirstOrDefault(n => !string.IsNullOrWhiteSpace(n.Account?.Username) && n.Account.Username == username);
}
}
}

View File

@@ -1,12 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OwnChar.Data
{
public class JsonFileDataProvider : IDataProvider
{
}
}