Switch to using newtable partial for all tables
Will be renamed eventually, don't care right now. Also cleans up a lot of code I wasn't particularly happy about
This commit is contained in:
parent
153ea869e0
commit
365b9dbf11
10 changed files with 128 additions and 166 deletions
|
|
@ -1,6 +1,8 @@
|
|||
const std = @import("std");
|
||||
const jetzig = @import("jetzig");
|
||||
const zeit = @import("zeit");
|
||||
const TableRow = @import("../../types.zig").TableRows;
|
||||
const HyperlinkData = @import("../../types.zig").HyperlinkData;
|
||||
|
||||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
|
|
@ -23,33 +25,30 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
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 prev_artist_infos: ?*jetzig.zmpl.Data.Value = 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) {
|
||||
var artist_info = try prev_artist_infos.?.append(.object);
|
||||
try artist_info.put("name", scrobble.artist_name);
|
||||
try artist_info.put("url", scrobble.artist_id);
|
||||
try artistlist.append(.{ .name = scrobble.artist_name, .id = scrobble.artist_id });
|
||||
continue :blk;
|
||||
} else {
|
||||
var date = std.ArrayList(u8).init(request.allocator);
|
||||
try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(scrobble.date, 1_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
|
||||
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.items,
|
||||
};
|
||||
|
||||
try scrobbles_view.append(row);
|
||||
}
|
||||
// Not appending the scrobble directly because we don't want the unix timestamp or scrobble id
|
||||
var scrobble_view = try scrobbles_view.append(.object);
|
||||
|
||||
var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
try artist_info.put("name", scrobble.artist_name);
|
||||
try artist_info.put("url", scrobble.artist_id);
|
||||
|
||||
try scrobble_view.put("song_name", scrobble.song_name);
|
||||
try scrobble_view.put("song_id", scrobble.song_id);
|
||||
try scrobble_view.put("album_name", scrobble.album_name);
|
||||
try scrobble_view.put("album_id", scrobble.album_id);
|
||||
var date = std.ArrayList(u8).init(request.allocator);
|
||||
try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(scrobble.date, 1_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
try scrobble_view.put("date", date.items);
|
||||
|
||||
prev_artist_infos = artist_infos;
|
||||
prev_s_id = scrobble.s_id;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue