This commit is contained in:
Pilzinsel64
2025-10-31 09:29:28 +01:00
parent e2a337a7aa
commit 6ae5cd51b0
8 changed files with 90 additions and 3 deletions

View File

@@ -0,0 +1,45 @@
using Pilz.Jobs;
using Pilz.Net.Api.Entities;
using Pilz.Net.Extensions;
namespace Pilz.Net.Api.Server;
public class JobsHandler(IApiServer server)
{
[ApiMessageHandler("/jobs", "GET")]
public virtual ApiResult GetAll()
{
return server.Jobs.Jobs.Select(ToClient).ToItemsResult();
}
[ApiMessageHandler("/jobs/{name}", "GET")]
public virtual ApiResult Get(string name)
{
if (server.Jobs.Jobs.FirstOrDefault(n => n.Name == name) is not Job job)
return ApiResult.BadRequest();
return ToClient(job).ToItemResult();
}
[ApiMessageHandler("/jobs/{name}", "POST")]
public virtual ApiResult Execute(string name)
{
if (server.Jobs.Jobs.FirstOrDefault(n => n.Name == name) is not Job job)
return ApiResult.BadRequest();
var now = DateTime.Now;
job.Execute(new(server.Jobs));
job.LastExecution = now;
return ApiResult.Ok();
}
protected virtual JobInfo ToClient(Job job)
{
return new JobInfo
{
Name = job.Name,
Interval = job.Interval,
LastExecution = job.LastExecution,
};
}
}