From 93da50652a5487f83e85c412e4724f2ee8aecfac Mon Sep 17 00:00:00 2001 From: mitteneer Date: Mon, 23 Jun 2025 10:14:25 -0400 Subject: [PATCH 1/3] Remove unnecessary else --- src/app/views/artists.zig | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/views/artists.zig b/src/app/views/artists.zig index 12be28c..505c3d1 100644 --- a/src/app/views/artists.zig +++ b/src/app/views/artists.zig @@ -38,7 +38,6 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View { if (artists.len > 1) return request.redirect("http://127.0.0.1:8080", .found); break :blk artists[0].id; }, - else => unreachable, }; var root = try request.data(.object); From f292368947279e6d208505848602931378668cd8 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Mon, 23 Jun 2025 10:15:17 -0400 Subject: [PATCH 2/3] Song name in url string --- src/app/views/songs.zig | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/app/views/songs.zig b/src/app/views/songs.zig index b634534..00ec085 100644 --- a/src/app/views/songs.zig +++ b/src/app/views/songs.zig @@ -1,6 +1,7 @@ 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); @@ -20,21 +21,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 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 song = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_info), .{id}); + const song = try queries.entityQueryResult(request, queries.loadQuery(.song, .entity_info), .{id_int}); try root.put("song", song); - const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_scrobbles), .{id}); + const scrobbles = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_scrobbles), .{id_int}); try root.put("scrobbles", scrobbles); - const albums = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_albums), .{id}); + const albums = try queries.entityQueryResult(request, queries.loadQuery(.song, .get_albums), .{id_int}); try root.put("albums", albums); - const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.song, .firstlast), .{id}); + const firstlast = try queries.entityQueryResult(request, queries.loadQuery(.song, .firstlast), .{id_int}); try root.put("firstlast", firstlast); - const timescale = try queries.entityQueryResult(request, queries.loadQuery(.song, .timescale), .{id}); + const timescale = try queries.entityQueryResult(request, queries.loadQuery(.song, .timescale), .{id_int}); try root.put("yearly", timescale); return request.render(.ok); } From b7e625dd9844e455496b729ddb9433a4560dcdc3 Mon Sep 17 00:00:00 2001 From: mitteneer Date: Mon, 23 Jun 2025 10:18:17 -0400 Subject: [PATCH 3/3] Start ratings This is actually fantastic, I'm really happy with how this has worked so far. My only concern for the future is how posting reviews from the `/ratings` path might work, since it's currently designed around posting reviews from the song page itself, but I think some HTMX and/or JS wil alleviate any problems I run into --- src/app/views/ratings/songs.zig | 12 ++++++++++++ src/app/views/ratings/songs/post.zmpl | 2 ++ src/app/views/songs/get.zmpl | 9 +++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 src/app/views/ratings/songs.zig create mode 100644 src/app/views/ratings/songs/post.zmpl diff --git a/src/app/views/ratings/songs.zig b/src/app/views/ratings/songs.zig new file mode 100644 index 0000000..3145fb4 --- /dev/null +++ b/src/app/views/ratings/songs.zig @@ -0,0 +1,12 @@ +const std = @import("std"); +const jetzig = @import("jetzig"); +pub fn post(request: *jetzig.Request) !jetzig.View { + var root = try request.data(.object); + const params = try request.params(); + const id = params.getT(.integer, "song_id"); + const review = params.getT(.string, "review"); + try root.put("song_id", id); + try root.put("review", review); + + return request.render(.created); +} diff --git a/src/app/views/ratings/songs/post.zmpl b/src/app/views/ratings/songs/post.zmpl new file mode 100644 index 0000000..3366a0c --- /dev/null +++ b/src/app/views/ratings/songs/post.zmpl @@ -0,0 +1,2 @@ +{{.song_id}} +{{.review}} \ No newline at end of file diff --git a/src/app/views/songs/get.zmpl b/src/app/views/songs/get.zmpl index 1ceddef..33e6886 100644 --- a/src/app/views/songs/get.zmpl +++ b/src/app/views/songs/get.zmpl @@ -5,6 +5,7 @@ + @@ -24,9 +25,13 @@

Rating

- - +
+ + + +
+
No reviews
\ No newline at end of file