From 7c294a5ecbe10e167e1d445c7e18f8127fd4d9f0 Mon Sep 17 00:00:00 2001 From: yuzu Date: Fri, 9 May 2025 13:26:53 -0500 Subject: [PATCH] add edit blog feature --- src/app/views/blogs.zig | 60 ++++++++++++++++++++++++++++ src/app/views/blogs/edit.zmpl | 46 ++++++++++++++++++++++ src/app/views/blogs/index.zmpl | 72 +++++++++++++++++----------------- src/main.zig | 2 + 4 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 src/app/views/blogs/edit.zmpl diff --git a/src/app/views/blogs.zig b/src/app/views/blogs.zig index 4d260a4..19a5cc0 100644 --- a/src/app/views/blogs.zig +++ b/src/app/views/blogs.zig @@ -223,6 +223,66 @@ pub fn delete(id: []const u8, request: *jetzig.Request) !jetzig.View { } } +// blogs/:id/edit +pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View { + // check "session" cookie for authentication + const cookies = try request.cookies(); + const session = cookies.get("session") orelse { + return request.fail(.not_found); + }; + + const session_query = jetzig.database.Query(.Session) + .findBy(.{ .session_id = session.value }); + + _ = request.repo.execute(session_query) catch { + return request.fail(.not_found); + }; + + switch (request.method) { + .GET => { + // get blog + const blog_query = jetzig.database.Query(.Blog) + .find(try std.fmt.parseInt(i32, id, 10)); + + const blog = request.repo.execute(blog_query) catch { + return request.fail(.not_found); + }; + + const root = try request.data(.object); + try root.put("allowed", true); + try root.put("blog", blog); + + return request.render(.ok); + }, + .POST => { + // make two queries for now + const params = try request.params(); + + const blog_query_original = jetzig.database.Query(.Blog) + .find(try std.fmt.parseInt(i32, id, 10)); + + if (try request.repo.execute(blog_query_original)) |blog| { + const title = params.getT(.string, "title") orelse blog.title; + const content = params.getT(.string, "content") orelse blog.content; + const preview = params.getT(.string, "preview") orelse blog.blob; + + // query the blogpost first + const blog_query = jetzig.database.Query(.Blog) + .update(.{ .title = title, .blob = preview, .content = content }) + .where(.{ .id = try std.fmt.parseInt(i32, id, 10) }); + + // update the blogpost + try request.repo.execute(blog_query); + + return request.redirect("/blogs", .moved_permanently); + } else { + return request.fail(.not_found); + } + }, + else => return request.fail(.not_found), + } +} + test "index" { var app = try jetzig.testing.app(std.testing.allocator, @import("routes")); defer app.deinit(); diff --git a/src/app/views/blogs/edit.zmpl b/src/app/views/blogs/edit.zmpl new file mode 100644 index 0000000..874642e --- /dev/null +++ b/src/app/views/blogs/edit.zmpl @@ -0,0 +1,46 @@ +
+
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+
+ diff --git a/src/app/views/blogs/index.zmpl b/src/app/views/blogs/index.zmpl index b64d9d6..91b86c8 100644 --- a/src/app/views/blogs/index.zmpl +++ b/src/app/views/blogs/index.zmpl @@ -3,25 +3,27 @@
@for (.blogs) |blog| {
-
- - {{blog.title}} - +
+ {{blog.title}} - @if ($.allowed) - - @end -
- -

- {{zmpl.fmt.datetime(blog.get("created_at"), "%Y-%m-%d %H:%M")}} -

-
+ @if ($.allowed) +
+ + Edit + + +
+ @end +
+

{{zmpl.fmt.datetime(blog.get("created_at"), "%Y-%m-%d %H:%M")}}

+
} @if ($.allowed) @@ -35,22 +37,22 @@ @if ($.allowed) - + @end diff --git a/src/main.zig b/src/main.zig index f2063b6..355dc33 100644 --- a/src/main.zig +++ b/src/main.zig @@ -105,6 +105,8 @@ pub const jetzig_options = struct { pub fn init(app: *jetzig.App) !void { app.route(.GET, "/rss.xml", @import("app/views/rss.zig"), .index); + app.route(.GET, "/blogs/:id/edit" , @import("app/views/blogs.zig"), .edit); + app.route(.POST, "/blogs/:id/edit" , @import("app/views/blogs.zig"), .edit); } pub fn main() !void {