Add more information to artists view, songs view, and format dates correctly in scrobbles view
This commit is contained in:
parent
01fe10f045
commit
65136a44d6
9 changed files with 82 additions and 34 deletions
|
|
@ -33,6 +33,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
continue :blk;
|
||||
}
|
||||
var album_view = try albums_view.append(.object);
|
||||
|
||||
var artist_infos = try album_view.put("artist_info", .array);
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
try artist_info.put("name", album.artist_name);
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var albums_view = try root.put("albums", .array);
|
||||
var appears = try root.put("appears", .array);
|
||||
const artist_name = try jetzig.database.Query(.Artist).find(id).select(.{ .id, .name }).execute(request.repo);
|
||||
_ = try root.put("artist", artist_name.?.name);
|
||||
const query =
|
||||
|
|
@ -48,6 +49,18 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
\\ORDER BY scrobbles DESC
|
||||
;
|
||||
|
||||
const appears_query =
|
||||
\\SELECT albums.name, albums.id, COUNT(scrobbles) AS scrobbles
|
||||
\\FROM artistalbums
|
||||
\\INNER JOIN albums ON albums.id = artistalbums.album_id
|
||||
\\INNER JOIN albumsongs ON albumsongs.album_id = albums.id
|
||||
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
|
||||
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
|
||||
\\WHERE albumsongsartists.artist_id = $1 AND artistalbums.artist_id != $1
|
||||
\\GROUP BY albums.id
|
||||
\\ORDER BY scrobbles DESC;
|
||||
;
|
||||
|
||||
var albums_jq_result = try request.repo.executeSql(query, .{id});
|
||||
defer albums_jq_result.deinit();
|
||||
|
||||
|
|
@ -60,30 +73,18 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
//const artist = try jetzig.database.Query(.Artist)
|
||||
// .find(id)
|
||||
// .select(.{ .id, .name })
|
||||
// .execute(request.repo);
|
||||
//var root = try request.data(.object);
|
||||
//try root.put("artist", artist.?.name);
|
||||
//var albums_view = try root.put("albums", .array);
|
||||
//const query = jetzig.database.Query(.Albumartist)
|
||||
// .select(.{.id})
|
||||
// .include(.album, .{ .select = .{ .name, .id } })
|
||||
// .join(.inner, .artist)
|
||||
// .where(.{ .artist = .{ .id = id } });
|
||||
|
||||
//const albums = try request.repo.all(query);
|
||||
//for (albums) |album| {
|
||||
// const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
// .where(.{ .album_id = album.album.id })
|
||||
// .count()
|
||||
// .execute(request.repo);
|
||||
// var album_view = try albums_view.append(.object);
|
||||
// try album_view.put("name", album.album.name);
|
||||
// try album_view.put("url", album.album.id);
|
||||
// try album_view.put("scrobbles", scrobbles);
|
||||
//}
|
||||
var appears_jq_result = try request.repo.executeSql(appears_query, .{id});
|
||||
defer appears_jq_result.deinit();
|
||||
|
||||
while (try appears_jq_result.postgresql.result.next()) |album_row| {
|
||||
const album = try album_row.to(Album, .{ .dupe = true, .allocator = request.allocator });
|
||||
var album_view = try appears.append(.object);
|
||||
try album_view.put("name", album.name);
|
||||
try album_view.put("url", album.id);
|
||||
try album_view.put("scrobbles", album.scrobbles);
|
||||
}
|
||||
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
<body>
|
||||
@partial partials/header
|
||||
<h1>{{.artist}}</h1>
|
||||
<h2>Albums</h2>
|
||||
<table id="myTable">
|
||||
<thead>
|
||||
<tr>
|
||||
|
|
@ -21,6 +22,22 @@
|
|||
}
|
||||
</tbody>
|
||||
</table>
|
||||
<h2>Albums Featured On</h2>
|
||||
<table id="myTable">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th><th>Scrobbles</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@for (.appears) |album| {
|
||||
<tr>
|
||||
<td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td>
|
||||
<td class=cell>{{album.scrobbles}}</td>
|
||||
</tr>
|
||||
}
|
||||
</tbody>
|
||||
<table>
|
||||
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
|
||||
<script>
|
||||
const dataTable = new simpleDatatables.DataTable("#myTable", {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
const std = @import("std");
|
||||
const jetzig = @import("jetzig");
|
||||
const zeit = @import("zeit");
|
||||
|
||||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
|
|
@ -33,7 +34,9 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
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);
|
||||
try scrobble_view.put("date", scrobble.date);
|
||||
var date = std.ArrayList(u8).init(request.allocator);
|
||||
try (try zeit.instant(.{ .source = .{ .unix_timestamp = @divFloor(scrobble.date, 1_000_000) } })).time().strftime(date.writer(), "%d %b %Y, %H:%M");
|
||||
try scrobble_view.put("date", date.items);
|
||||
}
|
||||
|
||||
//for (scrobbles) |scrobble| {
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@
|
|||
<tr>
|
||||
<th>Song</th>
|
||||
<th>Album</th>
|
||||
<th>Date</th>
|
||||
<th data-type="date" data-format="DD MMM YYYY, HH:mm">Date</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
|
|
|
|||
|
|
@ -12,25 +12,45 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
// .all(request.repo);
|
||||
|
||||
const query =
|
||||
\\SELECT songs.name, songs.id, COUNT(scrobbles) AS scrobbles
|
||||
\\SELECT songs.name, songs.id, artists.name, artists.id, COUNT(scrobbles) AS scrobbles
|
||||
\\FROM albumsongs
|
||||
\\INNER JOIN songs ON albumsongs.song_id = songs.id
|
||||
\\INNER JOIN scrobbles ON scrobbles.albumsong = albumsongs.id
|
||||
\\GROUP BY songs.id
|
||||
\\ORDER BY scrobbles DESC
|
||||
\\INNER JOIN albumsongsartists ON albumsongsartists.albumsong_id = albumsongs.id
|
||||
\\INNER JOIN artists ON artists.id = albumsongsartists.artist_id
|
||||
\\GROUP BY songs.id, artists.id
|
||||
\\ORDER BY scrobbles DESC, songs.name ASC
|
||||
;
|
||||
|
||||
var songs_js_result = try request.repo.executeSql(query, .{});
|
||||
defer songs_js_result.deinit();
|
||||
|
||||
const Song = struct { name: []const u8, id: i32, scrobbles: i64 };
|
||||
const Song = struct { name: []const u8, id: i32, artist_name: []const u8, artist_id: i32, scrobbles: i64 };
|
||||
|
||||
while (try songs_js_result.postgresql.result.next()) |song_row| {
|
||||
var prev_song_id: ?i32 = null;
|
||||
var prev_artist_infos: ?*jetzig.zmpl.Data.Value = null;
|
||||
|
||||
blk: while (try songs_js_result.postgresql.result.next()) |song_row| {
|
||||
const song = try song_row.to(Song, .{ .dupe = true, .allocator = request.allocator });
|
||||
if (song.id == prev_song_id) {
|
||||
var artist_info = try prev_artist_infos.?.append(.object);
|
||||
try artist_info.put("name", song.artist_name);
|
||||
try artist_info.put("url", song.artist_id);
|
||||
continue :blk;
|
||||
}
|
||||
var song_view = try songs_view.append(.object);
|
||||
|
||||
var artist_infos = try song_view.put("artist_info", .array);
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
try artist_info.put("name", song.artist_name);
|
||||
try artist_info.put("url", song.artist_id);
|
||||
|
||||
try song_view.put("name", song.name);
|
||||
try song_view.put("url", song.id);
|
||||
try song_view.put("scrobbles", song.scrobbles);
|
||||
|
||||
prev_artist_infos = artist_infos;
|
||||
prev_song_id = song.id;
|
||||
}
|
||||
|
||||
//for (songs) |song| {
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@
|
|||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Artist(s)</th>
|
||||
<th>Scrobbles</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
|
@ -17,6 +18,11 @@
|
|||
@for (.songs) |song| {
|
||||
<tr>
|
||||
<td class=cell><a href="/songs/{{song.url}}">{{song.name}}</a></td>
|
||||
<td class=cell>
|
||||
@for (song.get("artist_info").?) |ai| {
|
||||
<a href="/artists/{{ai.url}}">{{ai.name}}</a>
|
||||
}
|
||||
</td>
|
||||
<td class=cell>{{song.scrobbles}}</td>
|
||||
</tr>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -54,9 +54,6 @@ pub fn applyScrobbleRule(allocator: std.mem.Allocator, scrobble: Data.ImportedSc
|
|||
const list = try al.toOwnedSlice();
|
||||
@field(output_scrobble, @tagName(on)) = list;
|
||||
},
|
||||
//else => {
|
||||
// std.log.debug("Adding artists doesn't work yet", .{});
|
||||
//},
|
||||
}
|
||||
},
|
||||
.replace => switch (act.action_on) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue