zuletzt/src/app/views/songs.zig
mitteneer 12722f282d 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
2025-07-02 23:05:57 -04:00

59 lines
2.4 KiB
Zig

const std = @import("std");
const jetzig = @import("jetzig");
const queries = @import("../../queries.zig");
const decode = @import("../../date_fmt.zig").urlDecode;
pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object);
const htmx_query = (try request.queryParams()).getT(.string, "s");
try root.put("htmx", htmx_query != null);
const songs = if (htmx_query) |name|
try queries.entityQueryResult(request, queries.loadQuery(.song, .entities_by_name), .{name})
else
try queries.entityQueryResult(request, queries.loadQuery(.song, .entities), .{});
try root.put("songs", songs);
return request.render(.ok);
}
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
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);
const scrobbles = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .get_scrobbles), .{id_int});
try root.put("scrobbles", scrobbles);
const albums = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .get_albums), .{id_int});
try root.put("albums", albums);
const firstlast = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .firstlast), .{id_int});
try root.put("firstlast", firstlast);
const timescale = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .timescale), .{id_int});
try root.put("yearly", timescale);
const ratings = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .get_ratings), .{id_int});
try root.put("reviews", ratings);
const peak = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .peak), .{id_int});
try root.put("peak", peak);
return request.render(.ok);
}