Add artists column to albums view

This commit is contained in:
mitteneer 2025-03-26 21:38:34 -04:00
parent 11ddf40131
commit a77e1dcaad
12 changed files with 169 additions and 4 deletions

View file

@ -5,12 +5,27 @@ const jetquery = @import("jetzig").jetquery;
pub fn index(request: *jetzig.Request) !jetzig.View { pub fn index(request: *jetzig.Request) !jetzig.View {
var root = try request.data(.object); var root = try request.data(.object);
var albums_view = try root.put("albums", .array); var albums_view = try root.put("albums", .array);
const query = jetzig.database.Query(.Album).select(.{}).orderBy(.{ .name = .asc }); const query = jetzig.database.Query(.Album).select(.{})
.include(.albumartists, .{ .select = .{.artist_id} })
.orderBy(.{ .name = .asc });
const albums = try request.repo.all(query); const albums = try request.repo.all(query);
for (albums) |album| { for (albums) |album| {
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .album_id = album.id }).count().execute(request.repo);
var album_view = try albums_view.append(.object); var album_view = try albums_view.append(.object);
var artist_infos = try album_view.put("artist_info", .array);
for (album.albumartists) |artist| {
var artist_info = try artist_infos.append(.object);
const artist_data = try jetzig.database.Query(.Artist).where(.{ .id = artist.artist_id }).all(request.repo);
for (artist_data) |ad| {
try artist_info.put("name", ad.name);
try artist_info.put("id", ad.id);
}
}
try album_view.put("name", album.name); try album_view.put("name", album.name);
try album_view.put("url", album.id); try album_view.put("url", album.id);
try album_view.put("scrobbles", scrobbles);
} }
return request.render(.ok); return request.render(.ok);
} }

View file

@ -1,19 +1,41 @@
<html> <html>
<head> <head>
<link href="https://cdn.jsdelivr.net/npm/simple-datatables@latest/dist/style.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="styles.css"> <link rel="stylesheet" href="styles.css">
<meta charset="UTF-8"> <meta charset="UTF-8">
</head> </head>
<body> <body>
@partial partials/header @partial partials/header
<h1>Albums</h1> <h1>Albums</h1>
<table> <table id="myTable">
<thead>
<tr> <tr>
<th>Name</th> <th>Name</th>
<th>Artist(s)</th>
<th>Scrobbles</th>
</tr>
</thead>
</tbody>
@for (.albums) |album| { @for (.albums) |album| {
<tr> <tr>
<td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td> <td class=cell><a href="/albums/{{album.url}}">{{album.name}}</a></td>
<td class=cell>
@for (album.get("artist_info").?) |ai| {
<a href="/artists/{{ai.id}}">{{ai.name}}</a>
}
</td>
<td class=cell>{{album.scrobbles}}</td>
</tr> </tr>
} }
</tbody>
</table> </table>
<script src="https://cdn.jsdelivr.net/npm/simple-datatables@latest" type="text/javascript"></script>
<script>
const dataTable = new simpleDatatables.DataTable("#myTable", {
searchable: false,
perPage: 50,
perPageSelect: [25,50,100],
});
</script>
</body> </body>
</html> </html>

104
src/app/views/rules.zig Normal file
View file

@ -0,0 +1,104 @@
const std = @import("std");
const jetzig = @import("jetzig");
pub fn index(request: *jetzig.Request) !jetzig.View {
return request.render(.ok);
}
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
pub fn new(request: *jetzig.Request) !jetzig.View {
return request.render(.ok);
}
pub fn edit(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
pub fn post(request: *jetzig.Request) !jetzig.View {
return request.render(.created);
}
pub fn put(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
pub fn patch(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
pub fn delete(id: []const u8, request: *jetzig.Request) !jetzig.View {
_ = id;
return request.render(.ok);
}
test "index" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/rules", .{});
try response.expectStatus(.ok);
}
test "get" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/rules/example-id", .{});
try response.expectStatus(.ok);
}
test "new" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/rules/new", .{});
try response.expectStatus(.ok);
}
test "edit" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.GET, "/rules/example-id/edit", .{});
try response.expectStatus(.ok);
}
test "post" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.POST, "/rules", .{});
try response.expectStatus(.created);
}
test "put" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.PUT, "/rules/example-id", .{});
try response.expectStatus(.ok);
}
test "patch" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.PATCH, "/rules/example-id", .{});
try response.expectStatus(.ok);
}
test "delete" {
var app = try jetzig.testing.app(std.testing.allocator, @import("routes"));
defer app.deinit();
const response = try app.request(.DELETE, "/rules/example-id", .{});
try response.expectStatus(.ok);
}

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -0,0 +1,3 @@
<div>
<span>Content goes here</span>
</div>

View file

@ -25,8 +25,8 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
try scrobble_view.put("song_name", scrobble.song.name); try scrobble_view.put("song_name", scrobble.song.name);
try scrobble_view.put("song_id", scrobble.song.id); try scrobble_view.put("song_id", scrobble.song.id);
try scrobble_view.put("artist_name", "placeholder"); //try scrobble_view.put("artist_name", "placeholder");
try scrobble_view.put("artist_id", "placeholder"); //try scrobble_view.put("artist_id", "placeholder");
try scrobble_view.put("album_name", scrobble.album.name); try scrobble_view.put("album_name", scrobble.album.name);
try scrobble_view.put("album_id", scrobble.album.id); try scrobble_view.put("album_id", scrobble.album.id);
try scrobble_view.put("date", scrobble.date); try scrobble_view.put("date", scrobble.date);