agper-perubahan/Commons.cs

343 lines
15 KiB
C#

global using Microsoft.AspNetCore.Builder;
global using Microsoft.AspNetCore.Hosting;
global using Microsoft.AspNetCore.Http;
global using Microsoft.Data.SqlClient;
global using System.Text;
global using System.Text.Json.Nodes;
global using static perubahan.Commons;
global using static perubahan.Logging;
global using static perubahan.Middlewares;
global using System.Collections.Concurrent;
global using System.Security.Cryptography;
using System.Text.Json;
using System.Runtime.InteropServices;
namespace perubahan;
internal static class Commons
{
internal readonly static string VerNum = "0.1.250509.2301";
internal static ConcurrentDictionary<string,User> UserAccounts = [];
internal static List<Deployment> Deployments = [];
internal static List<Agent> Agents = [];
internal static readonly string SecretKey = "userandomlaterlikethecommented"; //RandomNumberGenerator.GetHexString(32);
internal static string CS = string.Empty ;
internal static JsonNode Settings = JsonNode.Parse("{}")!;
internal static readonly CookieOptions HttpOnly;
internal static readonly CookieOptions Delete;
// internal static EventStore EventsMarker = new (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X"),DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X"));
internal static readonly CancellationTokenSource CTS = new();
static Commons()
{
Delete = new ()
{
Domain = "",
Path = "/",
Expires = DateTime.Parse("2023-06-14")
};
HttpOnly = new ()
{
Domain = "",
Path = "/",
HttpOnly = true,
IsEssential = true
};
Console.CancelKeyPress += (sender, e) =>
{
e.Cancel = true; // Prevents immediate termination
CTS.Cancel();
};
AppDomain.CurrentDomain.ProcessExit += (sender, e) =>
{
CTS.Cancel();
};
}
internal static async Task WriteJsonResponse(HttpContext Context, int Status, string Message, object Data)
{
Context.Response.StatusCode = Status;
await Context.Response.WriteAsJsonAsync(new ApiResponse(Status, Message, Data), SGContext.Default.ApiResponse,cancellationToken: CTS.Token);
}
internal static async Task WriteJsonResponse(HttpContext Context, int Status, string Message)
{
Context.Response.StatusCode = Status;
await Context.Response.WriteAsJsonAsync(new SimpleApiResponse(Status, Message), SGContext.Default.SimpleApiResponse,cancellationToken: CTS.Token);
}
internal static async Task<bool> RequestValidated(HttpContext Context, int RequiredLevel = 0, string ValidMethod = "GET", bool CheckJson = false)
{
if (!ValidMethod.Equals(Context.Request.Method,StringComparison.OrdinalIgnoreCase) ||
(CheckJson && !Context.Request.HasJsonContentType()))
{
await WriteJsonResponse(Context, StatusCodes.Status405MethodNotAllowed, "Method Not Allowed.");
return false;
}
if (!Auth.IsAuthorized(Context, RequiredLevel))
{
await WriteJsonResponse(Context, StatusCodes.Status401Unauthorized, "Unauthorized.");
return false;
}
return true;
}
internal static async Task<int> RunNonQueryAsync(string ConnectionString, string SQL, Action<SqlCommand>? Configure = null, CancellationToken Token = default)
{
await using SqlConnection Conn = new(ConnectionString);
await Conn.OpenAsync(Token);
await using SqlCommand Cmd = new(SQL, Conn);
Configure?.Invoke(Cmd);
return await Cmd.ExecuteNonQueryAsync(Token);
}
internal static async Task<SqlDataReader> RunReaderAsync(string ConnectionString, string SQL, Action<SqlCommand>? Configure = null, CancellationToken Token = default)
{
SqlConnection Conn = new(ConnectionString);
await Conn.OpenAsync(Token);
SqlCommand Cmd = new(SQL, Conn);
Configure?.Invoke(Cmd);
return await Cmd.ExecuteReaderAsync(System.Data.CommandBehavior.CloseConnection, Token);
}
internal static async Task<object> RunScalarAsync(string ConnectionString, string SQL, Action<SqlCommand>? Configure = null, CancellationToken Token = default)
{
await using SqlConnection Conn = new(ConnectionString);
await Conn.OpenAsync(Token);
await using SqlCommand Cmd = new(SQL, Conn);
Configure?.Invoke(Cmd);
return await Cmd.ExecuteScalarAsync(Token);
}
public static async Task RunTransactionAsync(string ConnStr, Func<SqlConnection, SqlTransaction, Task> Logic, CancellationToken Token)
{
using SqlConnection Conn = new(ConnStr);
await Conn.OpenAsync(Token);
using SqlTransaction Tran = Conn.BeginTransaction();
try
{
await Logic(Conn, Tran);
await Tran.CommitAsync(Token);
}
catch (SqlException)
{
await Tran.RollbackAsync(Token);
// if (ex.State != 255) throw; //state = 255 is custom sql error, designed just for this purpose of not rethrowing.
}
catch
{
throw;
}
}
internal static string GenerateUuidV7()
{
Span<byte> uuidBytes = stackalloc byte[16];
long time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
RandomNumberGenerator.Fill(uuidBytes[7..]);
if (BitConverter.IsLittleEndian)
{
MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref time, 1))[..6].CopyTo(uuidBytes[..6]);
uuidBytes[..6].Reverse();
}
else
{
MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref time, 1))[2..8].CopyTo(uuidBytes[..6]);
}
uuidBytes[6] = (byte)((uuidBytes[6] & 0x0F) | 0x70); // Set version to 7
uuidBytes[8] = (byte)((uuidBytes[8] & 0x3F) | 0x80); // Set variant to 10xx
return Convert.ToHexString(uuidBytes).ToLower().Insert(8, "-").Insert(13, "-").Insert(18, "-").Insert(23, "-");
}
internal static async Task<Dictionary<string, JsonElement>?> TryGetBodyJsonAsync(HttpContext Context, string[] Keys, CancellationToken Token)
{
using JsonDocument BodyDoc = await JsonDocument.ParseAsync(Context.Request.Body,cancellationToken: Token);
JsonElement Element = BodyDoc.RootElement;
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
foreach (string Key in Keys)
{
if (!Element.TryGetProperty(Key, out JsonElement Value))
{
await WriteJsonResponse(Context, StatusCodes.Status400BadRequest,
"Bad Request. One or more required properties were not received.");
return null;
}
Result[Key] = Value.Clone();
}
return Result;
}
internal static Dictionary<string, JsonElement>? JsonElementToDict(JsonElement Element, string[] Keys)
{
Dictionary<string, JsonElement>? Result = new(StringComparer.OrdinalIgnoreCase);
foreach (string Key in Keys)
{
if (!Element.TryGetProperty(Key, out JsonElement Value))
{
return null;
}
Result[Key] = Value.Clone();
}
return Result;
}
internal static async Task<int> UpdateCache()
{
Console.WriteLine("Updating app cache.");
Console.Write(" Caching User Accounts... ");
await using SqlConnection FConn = new(CS);
await FConn.OpenAsync().ConfigureAwait(false);
using SqlCommand FComm = new("SELECT * FROM users",FConn);
await using (SqlDataReader URead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
{
while(await URead.ReadAsync(CTS.Token).ConfigureAwait(false))
{
_ = UserAccounts.TryAdd((string)URead["uname"], new User((string)URead["uname"],(string)URead["name"],(string)URead["pass"],(byte)URead["level"],(bool)URead["active"]));
}
}
FComm.CommandText = "SELECT * FROM deployment";
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
{
Deployments = await SRead.ToListAsync<Deployment>(r=>new(
(short)r["deplid"],
(string)r["unitkerja"]
),CTS.Token);
}
FComm.CommandText = "SELECT * FROM agents";
await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
{
Agents = await SRead.ToListAsync<Agent>(r=>new(
(string)r["agentid"],
(string)r["name"],
(string)r["jabatan"],
(short)r["deplid"],
(string)r["skangkat"],
DateOnly.FromDateTime((DateTime)r["tmt"]),
r["skperubahan"] == DBNull.Value ? null : (string)r["skperubahan"],
r["tgperubahan"] == DBNull.Value ? null : DateOnly.FromDateTime((DateTime)r["tgperubahan"]),
r["vision"] == DBNull.Value ? null : (string)r["vision"],
r["mission"] == DBNull.Value ? null : (string)r["mission"],
r["photourl"] == DBNull.Value ? null : (string)r["photourl"]
),CTS.Token);
}
// FComm.CommandText = "SELECT shift_sched.*, shifts.name FROM shifts LEFT JOIN shift_sched ON shifts.shiftid = shift_sched.shiftid ORDER BY shifts.shiftid, [day]";
// await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
// {
// ShiftSched = await SRead.ToListAsync<ShiftSchedEntry>(r=>new(
// (byte)r["shiftid"],
// (string)r["name"],
// (byte)r["schedid"],
// (byte)r["day"],
// TimeOnly.FromTimeSpan((TimeSpan)r["start"]),
// TimeOnly.FromTimeSpan((TimeSpan)r["end"])
// ),CTS.Token);
// }
Console.WriteLine("Done.");
Console.WriteLine("App cache updated.");
return 0;
// FComm.CommandText = "SELECT shift_sched.*, shifts.name FROM shifts LEFT JOIN shift_sched ON shifts.shiftid = shift_sched.shiftid ORDER BY shifts.shiftid, [day]";
// await using (SqlDataReader SRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
// {
// ShiftSched = await SRead.ToListAsync<ShiftSchedEntry>(r=>new(
// (byte)r["shiftid"],
// (string)r["name"],
// (byte)r["schedid"],
// (byte)r["day"],
// TimeOnly.FromTimeSpan((TimeSpan)r["start"]),
// TimeOnly.FromTimeSpan((TimeSpan)r["end"])
// ),CTS.Token);
// }
// FComm.CommandText = "SELECT tariffs.*, tariff_sched.schedid, tariff_sched.amount FROM tariffs LEFT JOIN tariff_sched ON tariffs.tariffid = tariff_sched.tariffid ORDER BY tariffs.tariffid";
// await using (SqlDataReader TRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
// {
// TariffSched = await TRead.ToListAsync<TariffSchedEntry>(r=>new(
// (byte)r["tariffid"],
// (string)r["name"],
// (bool)r["active"],
// r["schedid"] == DBNull.Value ? null : (byte)r["schedid"],
// r["amount"] == DBNull.Value ? null : (int)r["amount"]
// ),CTS.Token);
// }
// FComm.CommandText = "SELECT * FROM charges";
// await using (SqlDataReader CRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
// {
// Charges = await CRead.ToListAsync<Charge>(r=>new(
// (byte)r["chargid"],
// (string)r["name"],
// r["percentage"] == DBNull.Value ? null : (byte)r["percentage"],
// r["amount"] == DBNull.Value ? null : (int)r["amount"]
// ),CTS.Token);
// }
// FComm.CommandText = "SELECT * FROM packview";
// await using (SqlDataReader PRead = await FComm.ExecuteReaderAsync(CTS.Token).ConfigureAwait(false))
// {
// Packages = await PRead.ToListAsync<PackItem>(r=>new(
// (byte)r["packid"],
// (string)r["name"],
// (short)r["durinmin"],
// (int)r["price"],
// (byte)r["shiftid"],
// (string)r["shift"],
// (byte)r["schedid"],
// (byte)r["day"],
// (bool)r["active"]
// ), CTS.Token);
// }
// EventsMarker.CacheUpdates = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X");
}
}
internal static class Logging
{
private static readonly object Lock = new();
internal static async void WriteLog(Exception ex, string location = "")
{
await Task.Run(()=>{
string Time = $"{DateTime.Now:yyy-MM-dd HH:mm:ss}";
string msg = $"{Time} [{"Error".PadRight(7)[0..7]}] {ex.Message}{(location.Length > 0 ? $" {location}" : "")}{Environment.NewLine}";
Console.Write(msg);
try
{
byte[] msgbytes = Encoding.UTF8.GetBytes(msg);
lock (Lock)
{
using FileStream LogWriter = new($"{AppContext.BaseDirectory}/{DateTime.Now:yyyyMMdd}.log", FileMode.Append);
LogWriter.Write(msgbytes, 0, msgbytes.Length);
}
}
finally
{
}
});
}
internal static async void WriteLog(string exm, string location = "")
{
await Task.Run(()=>{
string Time = $"{DateTime.Now:yyy-MM-dd HH:mm:ss}";
string msg = $"{Time} [{"Info".PadRight(7)[0..7]}] {exm}{(location.Length > 0 ? $" {location}" : "")}{Environment.NewLine}";
Console.Write(msg);
try
{
byte[] msgbytes = Encoding.UTF8.GetBytes(msg);
lock (Lock)
{
using FileStream LogWriter = new($"{AppContext.BaseDirectory}/{DateTime.Now:yyyyMMdd}.log", FileMode.Append);
LogWriter.Write(msgbytes, 0, msgbytes.Length);
}
}
finally
{
}
});
}
}
internal static class DataReaderExtensions
{
internal static async Task<List<T>> ToListAsync<T>(
this SqlDataReader reader,
Func<SqlDataReader, T> map,
CancellationToken cancellationToken = default)
{
var list = new List<T>();
while (await reader.ReadAsync(cancellationToken))
{
list.Add(map(reader));
}
return list;
}
}