From 12722f282d09798fc02c087321295754d7d29ec6 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Wed, 2 Jul 2025 23:05:57 -0400 Subject: [PATCH] Revert forced redirect after id in url and begin disambiguation page At first, this was a nightmare. Now, I think I have a good idea about how to do disambiguation pages --- src/app/views/songs.zig | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index 31876ba..4be4f43 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -21,29 +21,18 @@ pub fn index(request: *jetzig.Request) !jetzig.View { } pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { - const parse_err = blk: { - const rdr_id = std.fmt.parseInt(i64, id, 10) catch |err| break :blk err; - const song = try jetzig.database.Query(.Song).find(rdr_id).execute(request.repo); - if (song == null) break :blk error.InvalidCharacter; - var name = std.ArrayList(u8).init(request.allocator); - try name.appendSlice("http://127.0.0.1:8080/songs/"); - try name.appendSlice(song.?.name); - return request.redirect(try name.toOwnedSlice(), .found); - }; - - const id_int = switch (parse_err) { - error.Overflow => return request.fail(.not_found), - error.InvalidCharacter => blk: { - const rn = try decode(request.allocator, id); - std.log.debug("{s}", .{rn}); - const songs = try jetzig.database.Query(.Song).where(.{ .name = rn }).all(request.repo); - - if (songs.len == 0) return request.fail(.not_found); - if (songs.len > 1) return request.redirect("http://127.0.0.1:8080", .found); - break :blk songs[0].id; - }, - }; var root = try request.data(.object); + const id_int = blk: { + const rn = try decode(request.allocator, id); + const songs = try jetzig.database.Query(.Song).select(.{.id}).where(.{ .name = rn }).all(request.repo); + if (songs.len == 0) { + break :blk std.fmt.parseInt(i64, id, 10) catch return request.fail(.not_found); + } else if (songs.len == 1) { + break :blk songs[0].id; + } else { + return request.redirect("http://127.0.0.1:8080", .found); + } + }; const song = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .entity_info), .{id_int}); try root.put("song", song);