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:
parent
12722f282d
commit
da9934ae1e
2 changed files with 22 additions and 6 deletions
|
|
@ -24,13 +24,20 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||||
var root = try request.data(.object);
|
var root = try request.data(.object);
|
||||||
const id_int = blk: {
|
const id_int = blk: {
|
||||||
const rn = try decode(request.allocator, id);
|
const rn = try decode(request.allocator, id);
|
||||||
const songs = try jetzig.database.Query(.Song).select(.{.id}).where(.{ .name = rn }).all(request.repo);
|
// Try to find the song by name
|
||||||
if (songs.len == 0) {
|
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);
|
break :blk std.fmt.parseInt(i64, id, 10) catch return request.fail(.not_found);
|
||||||
} else if (songs.len == 1) {
|
} else if (queried_songs.len == 1) {
|
||||||
break :blk songs[0].id;
|
// It can only be one song
|
||||||
|
break :blk queried_songs[0].id;
|
||||||
} else {
|
} 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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
@zig {
|
@zig {
|
||||||
const ColumnChoices = []const enum{song, album, artist, artistlist, scrobbles, date};
|
const ColumnChoices = []const enum{song, album, artist, artistlist, scrobbles, date};
|
||||||
const columns: ColumnChoices = &.{.song, .artistlist, .album, .date};
|
const columns: ColumnChoices = &.{.song, .artistlist, .album, .date};
|
||||||
const reviews = try zmpl.coerceArray(".reviews");
|
const dis_columns: ColumnChoices = &.{.song, .album, .artistlist, .scrobbles};
|
||||||
}
|
}
|
||||||
|
|
||||||
<html>
|
<html>
|
||||||
|
|
@ -11,6 +11,14 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@partial partials/header
|
@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">
|
<div style="text-align:center">
|
||||||
<h1>{{.song.song_name}}</h1>
|
<h1>{{.song.song_name}}</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -47,5 +55,6 @@
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
@end
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Add table
Add a link
Reference in a new issue