Add new to view generator

This commit is contained in:
Bob Farrell 2024-11-14 21:36:45 +00:00
parent 3ca7ade6a6
commit 5b285df07b

View File

@ -43,7 +43,7 @@ pub fn run(allocator: std.mem.Allocator, cwd: std.fs.Dir, args: [][]const u8, he
const action_args = if (args.len > 1) const action_args = if (args.len > 1)
args[1..] args[1..]
else else
&[_][]const u8{ "index", "get", "post", "put", "patch", "delete" }; &[_][]const u8{ "index", "get", "new", "post", "put", "patch", "delete" };
var actions = std.ArrayList(Action).init(allocator); var actions = std.ArrayList(Action).init(allocator);
defer actions.deinit(); defer actions.deinit();
@ -92,7 +92,7 @@ pub fn run(allocator: std.mem.Allocator, cwd: std.fs.Dir, args: [][]const u8, he
std.debug.print("Generated view: {s}\n", .{realpath}); std.debug.print("Generated view: {s}\n", .{realpath});
} }
const Method = enum { index, get, post, put, patch, delete }; const Method = enum { index, get, new, post, put, patch, delete };
const Action = struct { const Action = struct {
method: Method, method: Method,
static: bool, static: bool,
@ -126,16 +126,16 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
.{ .{
@tagName(action.method), @tagName(action.method),
switch (action.method) { switch (action.method) {
.index, .post => "", .index, .post, .new => "",
.get, .put, .patch, .delete => "id: []const u8, ", .get, .put, .patch, .delete => "id: []const u8, ",
}, },
if (action.static) "StaticRequest" else "Request", if (action.static) "StaticRequest" else "Request",
switch (action.method) { switch (action.method) {
.index, .post => "", .index, .post, .new => "",
.get, .put, .patch, .delete => "\n _ = id;", .get, .put, .patch, .delete => "\n _ = id;",
}, },
switch (action.method) { switch (action.method) {
.index, .get => ".ok", .index, .get, .new => ".ok",
.post => ".created", .post => ".created",
.put, .patch, .delete => ".ok", .put, .patch, .delete => ".ok",
}, },
@ -165,16 +165,17 @@ fn writeTest(allocator: std.mem.Allocator, writer: anytype, name: []const u8, ac
.{ .{
@tagName(action.method), @tagName(action.method),
switch (action.method) { switch (action.method) {
.index, .get => "GET", .index, .get, .new => "GET",
.put, .patch, .delete, .post => action_upper, .put, .patch, .delete, .post => action_upper,
}, },
name, name,
switch (action.method) { switch (action.method) {
.index, .post => "", .index, .post => "",
.new => "/new",
.get, .put, .patch, .delete => "/example-id", .get, .put, .patch, .delete => "/example-id",
}, },
switch (action.method) { switch (action.method) {
.index, .get => ".ok", .index, .get, .new => ".ok",
.post => ".created", .post => ".created",
.put, .patch, .delete => ".ok", .put, .patch, .delete => ".ok",
}, },
@ -195,7 +196,7 @@ fn writeStaticParams(allocator: std.mem.Allocator, actions: []Action, writer: an
for (actions) |action| { for (actions) |action| {
switch (action.method) { switch (action.method) {
.index, .post => { .index, .post, .new => {
const output = try std.fmt.allocPrint( const output = try std.fmt.allocPrint(
allocator, allocator,
\\ .{s} = .{{ \\ .{s} = .{{