Files
Shadow/Tools/GeneralUseHelperFunctions.cs

50 lines
1.4 KiB
C#

using Microsoft.EntityFrameworkCore;
using Shadow.Data;
using Shadow.Entities;
using System.Text;
namespace Shadow.Tools;
public class GeneralUseHelpers(ApplicationDbContext? db = null, IConfiguration? appsettings = null)
{
private readonly ApplicationDbContext? _db = db;
private readonly IConfiguration? _appsettings = appsettings;
//async public Task<User?> GetUserFromEmail(string email)
//{
// return await _db.Users.FirstOrDefaultAsync(e => e.Email == email);
//}
//public string HashWithSHA512(string s)
//{
// using (var sha512 = SHA512.Create())
// {
// byte[] bytes = Encoding.ASCII.GetBytes(s);
// byte[] hash = sha512.ComputeHash(bytes);
// string hashstring = BitConverter.ToString(hash).Replace("-", "").ToLower();
// return hashstring;
// }
//}
/// <summary>
/// Quick and dirty Dictionary&lt;string, string&gt; to JSON serializer
/// </summary>
/// <param name="dict">Dictionary with keypair of two strings</param>
/// <returns>Minified JSON</returns>
public static string DictAsJson(Dictionary<string, string> dict)
{
string resultJson = String.Empty;
foreach (string key in dict.Keys)
{
string cleanKey = key.Replace("\"", "\\\""); // "a"b" -> "a\"b"
string cleanValue = dict[key].Replace("\"", "\\\"");
resultJson += $"\"{cleanKey}\": \"{cleanValue}\", " // "key": "val",<space>
.Replace(@"\", @"\\"); // a\b -> a\\b
}
return "{" + resultJson[..^2] + "}";
}
}