Create disambiguation for songs

This was way easier than I expected, but I am rather unhappy with some things now. In particular, the GET page is pretty gross. I think there are some things I can do, but I'm not 100% confident. Maybe I'll bring some things up to bob once I have a better picture, but I really want to try to clean up my code
This commit is contained in:
mitteneer 2025-07-02 23:27:39 -04:00
parent 12722f282d
commit da9934ae1e
2 changed files with 22 additions and 6 deletions

View file

@ -24,13 +24,20 @@ 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) {
// Try to find the song by name
const queried_songs = try jetzig.database.Query(.Song).select(.{.id}).where(.{ .name = rn }).all(request.repo);
if (queried_songs.len == 0) {
// Either we've been given an id in the db, or the song doesn't exist
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 if (queried_songs.len == 1) {
// It can only be one song
break :blk queried_songs[0].id;
} else {
return request.redirect("http://127.0.0.1:8080", .found);
// It could be a variety of songs
const songs = try queries.entityQueryResult(request, queries.loadQuery(.song, .entities_by_name), .{rn});
try root.put("songs", songs);
try root.put("disambiguation", true);
return request.render(.ok);
}
};

View file

@ -1,7 +1,7 @@
@zig {
const ColumnChoices = []const enum{song, album, artist, artistlist, scrobbles, date};
const columns: ColumnChoices = &.{.song, .artistlist, .album, .date};
const reviews = try zmpl.coerceArray(".reviews");
const dis_columns: ColumnChoices = &.{.song, .album, .artistlist, .scrobbles};
}
<html>
@ -11,6 +11,14 @@
</head>
<body>
@partial partials/header
@if ($.disambiguation)
<h1>Songs</h1>
@partial partials/newtable(T: ColumnChoices, table_data: .songs, columns: dis_columns)
@else
@zig {
const reviews = try zmpl.coerceArray(".reviews");
}
<div style="text-align:center">
<h1>{{.song.song_name}}</h1>
</div>
@ -47,5 +55,6 @@
}
</div>
</div>
@end
</body>
</html>