Regulations support
This commit is contained in:
parent
62928e1f55
commit
8a64100252
@ -636,6 +636,62 @@ public static partial class APIHandler
|
|||||||
await runner.WriteJsonResponse(StatusCodes.Status201Created, "Proker evaluation accepted. Journal created successfully.");
|
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<Regulation> Regulations = await RegRd.ToListAsync<Regulation>(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<string, JsonElement> 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<string, JsonElement> 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.");
|
||||||
|
});
|
||||||
|
})
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,
|
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,
|
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);
|
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);
|
internal partial record User(string Username, string AgentID, string Password, byte Level, bool Active);
|
||||||
[JsonSerializable(typeof(Agent))]
|
[JsonSerializable(typeof(Agent))]
|
||||||
[JsonSerializable(typeof(ApiResponse))]
|
[JsonSerializable(typeof(ApiResponse))]
|
||||||
@ -31,6 +32,7 @@ internal partial record User(string Username, string AgentID, string Password, b
|
|||||||
[JsonSerializable(typeof(PasswdUser))]
|
[JsonSerializable(typeof(PasswdUser))]
|
||||||
[JsonSerializable(typeof(Proker))]
|
[JsonSerializable(typeof(Proker))]
|
||||||
[JsonSerializable(typeof(ProkerJournal))]
|
[JsonSerializable(typeof(ProkerJournal))]
|
||||||
|
[JsonSerializable(typeof(Regulation))]
|
||||||
[JsonSerializable(typeof(SafeUser))]
|
[JsonSerializable(typeof(SafeUser))]
|
||||||
[JsonSerializable(typeof(SimpleApiResponse))]
|
[JsonSerializable(typeof(SimpleApiResponse))]
|
||||||
[JsonSerializable(typeof(User))]
|
[JsonSerializable(typeof(User))]
|
||||||
@ -42,6 +44,7 @@ internal partial record User(string Username, string AgentID, string Password, b
|
|||||||
[JsonSerializable(typeof(List<Deployment>))]
|
[JsonSerializable(typeof(List<Deployment>))]
|
||||||
[JsonSerializable(typeof(List<Proker>))]
|
[JsonSerializable(typeof(List<Proker>))]
|
||||||
[JsonSerializable(typeof(List<ProkerJournal>))]
|
[JsonSerializable(typeof(List<ProkerJournal>))]
|
||||||
|
[JsonSerializable(typeof(List<Regulation>))]
|
||||||
[JsonSerializable(typeof(List<SafeUser>))]
|
[JsonSerializable(typeof(List<SafeUser>))]
|
||||||
[JsonSerializable(typeof(List<string>))]
|
[JsonSerializable(typeof(List<string>))]
|
||||||
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
|
[JsonSourceGenerationOptions(GenerationMode = JsonSourceGenerationMode.Default, PropertyNameCaseInsensitive = true, PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user