76 lines
3.6 KiB
C#
76 lines
3.6 KiB
C#
using System.Text.Json;
|
|
|
|
namespace perubahan;
|
|
|
|
public static class APIHandler
|
|
{
|
|
public static void Handle(IApplicationBuilder App)
|
|
{
|
|
App
|
|
.Map("/getunits", units =>{
|
|
units.Run(async runner=>{
|
|
if (!await RequestValidated(runner,2)) return;
|
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Success",Deployments);
|
|
});
|
|
})
|
|
.Map("/chunit", unit =>{
|
|
unit.Run(async runner=>{
|
|
if (!await RequestValidated(runner, 2, "POST", true)) return;
|
|
if(await TryGetBodyJsonAsync(runner, ["deplid", "unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
|
{
|
|
Deployment CorrectDeployment = new(
|
|
InElement["deplid"].GetInt16(),
|
|
InElement["unitkerja"].GetString() ?? ""
|
|
);
|
|
if (CorrectDeployment.UnitKerja.Length < 1)
|
|
{
|
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
|
return;
|
|
}
|
|
int i = Deployments.FindIndex(depl=>depl.DeplID == CorrectDeployment.DeplID);
|
|
if(i<0)
|
|
{
|
|
await WriteJsonResponse(runner,StatusCodes.Status404NotFound,"Deployment ID not found.");
|
|
return;
|
|
}
|
|
_ = await RunNonQueryAsync(CS,"UPDATE deployment SET unitkerja = @uk WHERE deplid = @id",Comm=>{
|
|
Comm.Parameters.AddWithValue("@id", CorrectDeployment.DeplID);
|
|
Comm.Parameters.AddWithValue("@uk", CorrectDeployment.UnitKerja);
|
|
},CTS.Token);
|
|
Deployments[i] = CorrectDeployment;
|
|
await WriteJsonResponse(runner,StatusCodes.Status202Accepted,"Data updated.",Deployments[i]);
|
|
}
|
|
});
|
|
})
|
|
.Map("/addunit", unit =>{
|
|
unit.Run(async runner=>{
|
|
if (!await RequestValidated(runner, 2, "POST", true)) return;
|
|
if(await TryGetBodyJsonAsync(runner, ["unitkerja"], CTS.Token) is Dictionary<string, JsonElement> InElement)
|
|
{
|
|
string UnitKerja = InElement["unitkerja"].GetString() ?? "";
|
|
if (UnitKerja.Length < 1)
|
|
{
|
|
await WriteJsonResponse(runner,StatusCodes.Status400BadRequest, "Unit Kerja can't be empty string.");
|
|
return;
|
|
}
|
|
short DeplID = (short)await RunScalarAsync(CS,"INSERT INTO deployment OUTPUT INSERTED.deplid VALUES (@uk)",Comm=>{
|
|
Comm.Parameters.AddWithValue("@uk", UnitKerja);
|
|
},CTS.Token);
|
|
Deployment Inserted = new(DeplID,UnitKerja);
|
|
Deployments.Add(Inserted);
|
|
// EventsMarker.CacheUpdates = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString("X");
|
|
await WriteJsonResponse(runner,StatusCodes.Status201Created,"Data Created.",Inserted);
|
|
}
|
|
});
|
|
})
|
|
|
|
.Map("/getagents", agents=>{
|
|
agents.Run(async runner=>{
|
|
if (!await RequestValidated(runner,2)) return;
|
|
await WriteJsonResponse(runner,StatusCodes.Status200OK,"Success",Agents);
|
|
});
|
|
})
|
|
;
|
|
}
|
|
}
|