diff --git a/src/app/jobs/process_rule.zig b/src/app/jobs/process_rule.zig new file mode 100644 index 0000000..fb0d78b --- /dev/null +++ b/src/app/jobs/process_rule.zig @@ -0,0 +1,32 @@ +const std = @import("std"); +const jetzig = @import("jetzig"); + +pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig.jobs.JobEnv) !void { + _ = allocator; + _ = env; + //_ = params; + + const rule = try params.toJson(); + + //const rule = struct { + // name: []const u8 = params, + // conditions: []struct { + // match_on: []const u8, + // match_cond: []const u8, + // match_text: []const u8, + // }, + // actions: []struct { + // action: []const u8, + // action_cond: []const u8, + // action_text: []const u8, + // }, + //}; + + //var file = try std.fs.cwd().openFile("rules.json", .{}); + + //_ = try file.write(rule); + try std.fs.cwd().writeFile(.{ "rules.json", rule, .{} }); + + // Job execution code goes here. Add any code that you would like to run in the background. + //try env.logger.INFO("Running a job.", .{}); +} diff --git a/src/app/views/rules.zig b/src/app/views/rules.zig index a2a222e..bb8ea4b 100644 --- a/src/app/views/rules.zig +++ b/src/app/views/rules.zig @@ -20,6 +20,26 @@ pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View { } pub fn post(request: *jetzig.Request) !jetzig.View { + const params = try request.params(); + + var job = try request.job("process_rule"); + + _ = try job.params.put("name", params.get("rule-title")); + + var conditionals = try job.params.put("conditionals", .array); + var cond0 = try conditionals.append(.object); + try cond0.put("match_on", params.get("match-on")); + try cond0.put("match_cond", params.get("match-cond")); + try cond0.put("match_txt", params.get("match-txt")); + + var actions = try job.params.put("actions", .array); + var act0 = try actions.append(.object); + try act0.put("action", params.get("action")); + try act0.put("action_on", params.get("action-on")); + try act0.put("action_txt", params.get("action-txt")); + + try job.schedule(); + return request.render(.created); } @@ -38,7 +58,6 @@ pub fn delete(id: []const u8, request: *jetzig.Request) !jetzig.View { return request.render(.ok); } - test "index" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit(); diff --git a/src/app/views/rules/index.zmpl b/src/app/views/rules/index.zmpl index 76457d0..4d15188 100644 --- a/src/app/views/rules/index.zmpl +++ b/src/app/views/rules/index.zmpl @@ -1,3 +1,99 @@ -