mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-07-01 13:36:08 +00:00
Updated data handling
This commit is contained in:
parent
25f4acad64
commit
e8af308f76
@ -2,13 +2,13 @@ const std = @import("std");
|
|||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
/// This example demonstrates usage of Jetzig's background jobs.
|
/// This example demonstrates usage of Jetzig's background jobs.
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
// Prepare a job using `src/app/jobs/example.zig`.
|
// Prepare a job using `src/app/jobs/example.zig`.
|
||||||
var job = try request.job("example");
|
var job = try request.job("example");
|
||||||
|
|
||||||
// Add a param `foo` to the job.
|
// Add a param `foo` to the job.
|
||||||
try job.params.put("foo", data.string("bar"));
|
try job.params.put("foo", "bar");
|
||||||
try job.params.put("id", data.integer(std.crypto.random.int(u32)));
|
try job.params.put("id", std.crypto.random.int(u32));
|
||||||
|
|
||||||
// Schedule the job for background processing.
|
// Schedule the job for background processing.
|
||||||
try job.schedule();
|
try job.schedule();
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
_ = data;
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
try root.put("message", try request.cache.get("message"));
|
try root.put("message", try request.cache.get("message"));
|
||||||
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn post(request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
|
|
||||||
const params = try request.params();
|
const params = try request.params();
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ pub fn post(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
|||||||
try request.cache.put("message", message);
|
try request.cache.put("message", message);
|
||||||
try root.put("message", message);
|
try root.put("message", message);
|
||||||
} else {
|
} else {
|
||||||
try root.put("message", data.string("[no message param detected]"));
|
try root.put("message", "[no message param detected]");
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
|
@ -4,16 +4,14 @@ const jetzig = @import("jetzig");
|
|||||||
// Generic handler for all errors.
|
// Generic handler for all errors.
|
||||||
// Use `jetzig.http.status_codes.get(request.status_code)` to get a value that provides string
|
// Use `jetzig.http.status_codes.get(request.status_code)` to get a value that provides string
|
||||||
// versions of the error code and message for use in templates.
|
// versions of the error code and message for use in templates.
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
var error_info = try data.object();
|
var error_info = try root.put("error", .object);
|
||||||
|
|
||||||
const status = jetzig.http.status_codes.get(request.status_code);
|
const status = jetzig.http.status_codes.get(request.status_code);
|
||||||
|
|
||||||
try error_info.put("code", data.string(status.getCode()));
|
try error_info.put("code", status.getCode());
|
||||||
try error_info.put("message", data.string(status.getMessage()));
|
try error_info.put("message", status.getMessage());
|
||||||
|
|
||||||
try root.put("error", error_info);
|
|
||||||
|
|
||||||
// Render with the original error status code, or override if preferred.
|
// Render with the original error status code, or override if preferred.
|
||||||
return request.render(request.status_code);
|
return request.render(request.status_code);
|
||||||
|
@ -9,13 +9,11 @@ pub const formats: jetzig.Route.Formats = .{
|
|||||||
.get = &.{.html},
|
.get = &.{.html},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
_ = data;
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(id: []const u8, request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||||
_ = data;
|
|
||||||
_ = id;
|
_ = id;
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
@ -12,14 +12,14 @@ const jetzig = @import("jetzig");
|
|||||||
/// PUT /1234 => put(id, request, data)
|
/// PUT /1234 => put(id, request, data)
|
||||||
/// PATCH /1234 => patch(id, request, data)
|
/// PATCH /1234 => patch(id, request, data)
|
||||||
/// DELETE /1234 => delete(id, request, data)
|
/// DELETE /1234 => delete(id, request, data)
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
// The first call to `data.object()` or `data.array()` sets the root response data value.
|
// Sets the root response data value.
|
||||||
// JSON requests return a JSON string representation of the root data value.
|
// JSON requests return a JSON string representation of the root data value.
|
||||||
// Zmpl templates can access all values in the root data value.
|
// Zmpl templates can access all values in the root data value.
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
|
|
||||||
// Add a string to the root object.
|
// Add a string to the root object.
|
||||||
try root.put("welcome_message", data.string("Welcome to Jetzig!"));
|
try root.put("welcome_message", "Welcome to Jetzig!");
|
||||||
|
|
||||||
// Request params have the same type as a `data.object()` so they can be inserted them
|
// Request params have the same type as a `data.object()` so they can be inserted them
|
||||||
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
|
// directly into the response data. Fetch `http://localhost:8080/?message=hello` to set the
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
try root.put("message", data.string("Welcome to Jetzig!"));
|
try root.put("message", "Welcome to Jetzig!");
|
||||||
|
|
||||||
// Create a new mail using `src/app/mailers/welcome.zig`.
|
// Create a new mail using `src/app/mailers/welcome.zig`.
|
||||||
// HTML and text parts are rendered using Zmpl templates:
|
// HTML and text parts are rendered using Zmpl templates:
|
||||||
|
@ -3,8 +3,7 @@ const jetzig = @import("jetzig");
|
|||||||
|
|
||||||
pub const layout = "application";
|
pub const layout = "application";
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
_ = data;
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const jetzig = @import("jetzig");
|
const jetzig = @import("jetzig");
|
||||||
|
|
||||||
pub fn index(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try data.object();
|
var root = try request.data(.object);
|
||||||
|
|
||||||
const session = try request.session();
|
const session = try request.session();
|
||||||
|
|
||||||
if (session.get("message")) |message| {
|
if (session.get("message")) |message| {
|
||||||
try root.put("message", message);
|
try root.put("message", message);
|
||||||
} else {
|
} else {
|
||||||
try root.put("message", data.string("No message saved yet"));
|
try root.put("message", "No message saved yet");
|
||||||
}
|
}
|
||||||
|
|
||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
@ -20,8 +20,7 @@ pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||||||
return request.render(.ok);
|
return request.render(.ok);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn post(request: *jetzig.Request, data: *jetzig.Data) !jetzig.View {
|
pub fn post(request: *jetzig.Request) !jetzig.View {
|
||||||
_ = data;
|
|
||||||
const params = try request.params();
|
const params = try request.params();
|
||||||
var session = try request.session();
|
var session = try request.session();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user