Use queries.zig in scrobbles view
This commit is contained in:
parent
f59eec79a8
commit
3ff973e193
2 changed files with 23 additions and 64 deletions
|
|
@ -1,56 +1,12 @@
|
|||
const std = @import("std");
|
||||
const jetzig = @import("jetzig");
|
||||
const zeit = @import("zeit");
|
||||
const TableRow = @import("../../types.zig").TableRow;
|
||||
const HyperlinkData = @import("../../types.zig").HyperlinkData;
|
||||
const Utils = @import("../../date_fmt.zig");
|
||||
const queries = @import("../../queries.zig");
|
||||
|
||||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var scrobbles_view = try root.put("scrobbles", .array);
|
||||
|
||||
const query =
|
||||
\\SELECT songs.name, songs.id, albums.name, albums.id, artists.name, artists.id, scrobbles.id, scrobbles.datetime
|
||||
\\FROM albumsongs
|
||||
\\INNER JOIN songs ON songs.id = albumsongs.song_id
|
||||
\\INNER JOIN albums ON albums.id = albumsongs.album_id
|
||||
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
|
||||
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
|
||||
\\INNER JOIN artists ON artists.id = albumsongsartists.artist_id
|
||||
\\ORDER BY scrobbles.datetime ASC
|
||||
;
|
||||
|
||||
var scrobbles_jq_result = try request.repo.executeSql(query, .{});
|
||||
defer scrobbles_jq_result.deinit();
|
||||
|
||||
const Scrobble = struct { song_name: []const u8, song_id: i32, album_name: []const u8, album_id: i32, artist_name: []const u8, artist_id: i32, s_id: i32, date: i64 };
|
||||
|
||||
var prev_s_id: ?i32 = null;
|
||||
|
||||
var row: ?TableRow = null;
|
||||
var artistlist = std.ArrayList(HyperlinkData).init(request.allocator);
|
||||
|
||||
blk: while (try scrobbles_jq_result.postgresql.result.next()) |scrobble_row| {
|
||||
const scrobble = try scrobble_row.to(Scrobble, .{ .dupe = true, .allocator = request.allocator });
|
||||
if (scrobble.s_id == prev_s_id) {
|
||||
try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id });
|
||||
continue :blk;
|
||||
} else {
|
||||
const date = try Utils.dateFmt(request.allocator, scrobble.date);
|
||||
|
||||
try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id });
|
||||
|
||||
row = TableRow{
|
||||
.song = .{ .name = scrobble.song_name, .id = scrobble.song_id },
|
||||
.album = .{ .name = scrobble.album_name, .id = scrobble.album_id },
|
||||
.artistlist = try artistlist.toOwnedSlice(),
|
||||
.date = date,
|
||||
};
|
||||
|
||||
try scrobbles_view.append(row);
|
||||
}
|
||||
prev_s_id = scrobble.s_id;
|
||||
}
|
||||
const scrobbles = try queries.entityQueryResult(request, queries.generateQuery(.scrobble, .entities), .{}, .array);
|
||||
try root.put("scrobbles", scrobbles);
|
||||
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue