diff --git a/src/app/views/albums.zig b/src/app/views/albums.zig index 55db929..5f3e0e4 100644 --- a/src/app/views/albums.zig +++ b/src/app/views/albums.zig @@ -16,46 +16,43 @@ 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 album = try jetzig.database.Query(.Album).find(rdr_id).execute(request.repo); - if (album == null) break :blk error.InvalidCharacter; - var name = std.ArrayList(u8).init(request.allocator); - try name.appendSlice("http://127.0.0.1:8080/albums/"); - try name.appendSlice(album.?.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(.Album).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 album = try queries.entityQueryResult(request, queries.loadQuery(.album, .entity_info), .{id_int}); + const id_int = blk: { + const rn = try decode(request.allocator, id); + // Try to find the song by name + const queried_albums = try jetzig.database.Query(.Album).select(.{.id}).where(.{ .name = rn }).all(request.repo); + if (queried_albums.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 (queried_albums.len == 1) { + // It can only be one song + break :blk queried_albums[0].id; + } else { + // It could be a variety of songs + const albums = try queries.entityQueryResult(request, comptime queries.loadQuery(.album, .entities_by_name), .{rn}); + try root.put("name", rn); + try root.put("albums", albums); + try root.put("disambiguation", true); + return request.render(.ok); + } + }; + const album = try queries.entityQueryResult(request, comptime queries.loadQuery(.album, .entity_info), .{id_int}); try root.put("album", album); - const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_scrobbles), .{id_int}); + const scrobbles = try queries.entityQueryResult(request, comptime queries.loadQuery(.song, .get_scrobbles), .{id_int}); try root.put("scrobbles", scrobbles); - const songs = try queries.entityQueryResult(request, queries.loadQuery(.album, .get_songs), .{id_int}); + const songs = try queries.entityQueryResult(request, comptime queries.loadQuery(.album, .get_songs), .{id_int}); try root.put("songs", songs); - const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.album, .firstlast), .{id_int}); + const firstlast = try queries.entityQueryResult(request, comptime queries.loadQuery(.album, .firstlast), .{id_int}); try root.put("firstlast", firstlast); - const timescale = try queries.entityQueryResult(request, queries.loadQuery(.album, .timescale), .{id_int}); + const timescale = try queries.entityQueryResult(request, comptime queries.loadQuery(.album, .timescale), .{id_int}); try root.put("yearly", timescale); - const ratings = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_ratings), .{id_int}); + 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(.album, .peak), .{id_int}); diff --git a/src/app/views/albums/get.zmpl b/src/app/views/albums/get.zmpl index e4e2196..d62af1c 100644 --- a/src/app/views/albums/get.zmpl +++ b/src/app/views/albums/get.zmpl @@ -1,7 +1,7 @@ @zig { const ColumnChoices = []const enum{song, album, artist, artistlist, scrobbles, date}; const columns: ColumnChoices = &.{.song, .scrobbles}; - const reviews = try zmpl.coerceArray(".reviews"); + const dis_columns: ColumnChoices = &.{.album, .artistlist, .scrobbles}; } @@ -11,6 +11,14 @@
@partial partials/header +@if ($.disambiguation) +