This commit is contained in:
2022-04-19 00:05:16 +02:00
parent e7c2ae7b8d
commit d7f2348f0d
20 changed files with 924 additions and 49 deletions

77
APP/Utility/SaveBoy.cs Normal file
View File

@@ -0,0 +1,77 @@
using System.Text.Json;
using APP.DTO;
namespace APP.Utility;
internal static class SaveBoy
{
private static readonly string AuthFile = ".." + Path.DirectorySeparatorChar + "reg.json";
internal static async Task<Auth?> ReadAuth()
{
if (File.Exists(AuthFile))
{
return await ReadFromFile<Auth>(AuthFile);
}
return null;
}
internal static void DeleteAuth()
{
if (File.Exists(AuthFile))
{
File.Delete(AuthFile);
}
}
internal static async Task SaveAuth(Auth auth)
{
await WriteToFile(AuthFile, auth);
}
private static async Task WriteToFile<T>(string filePath, T objectToWrite)
{
try
{
await using var sw = new StreamWriter(filePath, false);
try
{
var res = JsonSerializer.Serialize(objectToWrite);
await sw.WriteAsync(res);
}
finally
{
sw.Close();
}
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync(ex.Message);
}
}
private static async Task<T?> ReadFromFile<T>(string filePath)
{
try
{
using var sr = new StreamReader(filePath);
try
{
var text = await sr.ReadToEndAsync();
var res = JsonSerializer.Deserialize<T>(text);
return res;
}
finally
{
sr.Close();
}
}
catch (Exception ex)
{
await Console.Error.WriteLineAsync(ex.Message);
}
return default;
}
}