diff --git a/APIHandler.cs b/APIHandler.cs index bad2d87..ca7c39e 100644 --- a/APIHandler.cs +++ b/APIHandler.cs @@ -484,7 +484,7 @@ public static partial class APIHandler { proker.Run(async runner => { - if (!await runner.RequestValidated(2, "POST", true) || !Auth.TryGetUser(runner, out SafeUser CurrUser) || + if (!await runner.RequestValidated(2, "POST", true) || !Auth.TryGetUser(runner, out SafeUser CurrUser) || await runner.TryGetBodyJsonAsync(["agentid", "year", "newstatus", "notes"], CTS.Token) is not Dictionary InElement) return; if (CurrUser.Level != 2) { @@ -501,7 +501,7 @@ public static partial class APIHandler await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Required property is of invalid format."); return; } - await RunTransactionAsync(CS, async(Conn, Trans) => + await RunTransactionAsync(CS, async (Conn, Trans) => { List PKIDList = []; using (SqlCommand PKIDFetch = Conn.CreateCommand()) @@ -536,7 +536,7 @@ public static partial class APIHandler proker.Run(async runner => { if (!await runner.RequestValidated(3, "POST", true) || !Auth.TryGetUser(runner, out SafeUser CurrUser) || - await runner.TryGetBodyJsonAsync(["agentid","year"], CTS.Token) is not Dictionary InElement) return; + await runner.TryGetBodyJsonAsync(["agentid", "year"], CTS.Token) is not Dictionary InElement) return; if (CurrUser.Level != 3) { await runner.WriteJsonResponse(StatusCodes.Status401Unauthorized, "Only Level 3 Users may submit their own Prokers."); @@ -551,7 +551,7 @@ public static partial class APIHandler return; } List PKIDList = []; - await RunTransactionAsync(CS, async(Conn, Trans) => + await RunTransactionAsync(CS, async (Conn, Trans) => { using (SqlCommand PKIDFetch = Conn.CreateCommand()) { @@ -587,7 +587,7 @@ public static partial class APIHandler if ( InElement["prokerid"].GetString() is not string ProkerID || ProkerID.Equals(string.Empty) || InElement["notes"].GetString() is not string Notes || Notes.Equals(string.Empty) || - InElement["evidence"].GetString() is not string Evidence || Evidence.Equals(string.Empty) + InElement["evidence"].GetString() is not string Evidence || Evidence.Equals(string.Empty) ) { await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "Required property values is invalid or out of allowed range."); @@ -595,7 +595,7 @@ public static partial class APIHandler } byte[] PDFBytes = Convert.FromBase64String(Evidence); string FileName = $"{Crc32.Compute(PDFBytes):X8}.pdf"; - string FilePath = Path.Combine(AppContext.BaseDirectory , "wwwroot/uploads/dokumen" , FileName); + string FilePath = Path.Combine(AppContext.BaseDirectory, "wwwroot/uploads/dokumen", FileName); if (!File.Exists(FilePath)) await File.WriteAllBytesAsync(FilePath, PDFBytes, CTS.Token); await RunNonQueryAsync(CS, "INSERT INTO [proker_journal] VALUES (@pjid, @pkid, @owid, 4, @nots, @docs, @tstp)", Query => { @@ -610,7 +610,7 @@ public static partial class APIHandler await runner.WriteJsonResponse(StatusCodes.Status201Created, "Proker evidence accepted. Journal created successfully."); }); }) - .Map("/eval", proker=> + .Map("/eval", proker => { proker.Run(async runner => { @@ -636,6 +636,62 @@ public static partial class APIHandler await runner.WriteJsonResponse(StatusCodes.Status201Created, "Proker evaluation accepted. Journal created successfully."); }); }) + .Map("/regs", regs => + { + regs.Run(async runner => + { + if (!await runner.RequestValidated(3)) return; + using SqlDataReader RegRd = await RunReaderAsync(CS, "SELECT * FROM [regulations]", null, CTS.Token); + List Regulations = await RegRd.ToListAsync(R => new( + (string)RegRd["id"], + (string)RegRd["judul"], + RegRd["abstrak"] == DBNull.Value ? "" : (string)RegRd["abstrak"], + (string)RegRd["agentid"], + (DateTime)RegRd["timestamp"] + )); + await runner.WriteJsonResponse(StatusCodes.Status200OK, "Regulation List fetched.", Regulations); + }); + }) + .Map("/addreg", reg => + { + reg.Run(async runner => + { + if (!await runner.RequestValidated(3,"POST",true) || !Auth.TryGetUser(runner, out SafeUser CurrUser) || await runner.TryGetBodyJsonAsync(["judul", "abstrak", "pdf"], CTS.Token) is not Dictionary InElement) return; + if ( + InElement["judul"].GetString() is not string Judul || + InElement["abstrak"].GetString() is not string Abstrak || + InElement["pdf"].GetString() is not string InPDF + ) + { + await runner.WriteJsonResponse(StatusCodes.Status400BadRequest, "One or more required propertyies are not provided or of invalid format/value."); + return; + } + byte[] PDFBytes = Convert.FromBase64String(InPDF); + string FileName = $"{Crc32.Compute(PDFBytes):X8}.pdf"; + string FilePath = Path.Combine(AppContext.BaseDirectory, "wwwroot/uploads/regulasi", FileName); + if (!File.Exists(FilePath)) await File.WriteAllBytesAsync(FilePath, PDFBytes, CTS.Token); + await RunNonQueryAsync(CS, "INSERT INTO [regulations] VALUES(@id, @jd, @ab, @ag, SYSDATETIME())", Query => + { + Query.Parameters.AddWithValue("@id", FileName); + Query.Parameters.AddWithValue("@jd", Judul); + Query.Parameters.AddWithValue("@ab", Abstrak.Length > 1 ? Abstrak : DBNull.Value); + Query.Parameters.AddWithValue("@ag", CurrUser.AgentID); + }, CTS.Token); + await runner.WriteJsonResponse(StatusCodes.Status201Created, "New regulation entry created."); + }); + }) + .Map("/remreg", reg => + { + reg.Run(async runner => + { + if (!await runner.RequestValidated(3,"POST",true) || await runner.TryGetBodyJsonAsync(["id"], CTS.Token) is not Dictionary InElement || InElement["id"].GetString() is not string ID || ID.Length < 12) return; + await RunNonQueryAsync(CS, "DELETE FROM [regulations] WHERE id = @id", Query => + { + Query.Parameters.AddWithValue("@id", ID); + }, CTS.Token); + await runner.WriteJsonResponse(StatusCodes.Status200OK, "Regulation entry removed."); + }); + }) ; } diff --git a/Partials.cs b/Partials.cs index 93467eb..f642aa4 100644 --- a/Partials.cs +++ b/Partials.cs @@ -21,6 +21,7 @@ internal partial record Proker(string ProkerID, string AgentID, byte Kegiatan, s internal partial record ProkerJournal(string ProkerID, string OwnerID, byte Kegiatan, string Sasaran, byte? StartDay, byte StartMonth, short Year, byte TimeTarget, bool IsInMonth, string EntityTarget, string Indicators, string Actions, string JournalID, string SubmitterID, string Notes, byte Status, string? Document, DateTime TimeStamp); +internal partial record Regulation(string ID, string Judul, string Abstrak, string AgentID, DateTime TimeStamp); internal partial record User(string Username, string AgentID, string Password, byte Level, bool Active); [JsonSerializable(typeof(Agent))] [JsonSerializable(typeof(ApiResponse))] @@ -31,6 +32,7 @@ internal partial record User(string Username, string AgentID, string Password, b [JsonSerializable(typeof(PasswdUser))] [JsonSerializable(typeof(Proker))] [JsonSerializable(typeof(ProkerJournal))] +[JsonSerializable(typeof(Regulation))] [JsonSerializable(typeof(SafeUser))] [JsonSerializable(typeof(SimpleApiResponse))] [JsonSerializable(typeof(User))] @@ -42,6 +44,7 @@ internal partial record User(string Username, string AgentID, string Password, b [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] +[JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [JsonSerializable(typeof(List))] [JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]