Refactor
This commit is contained in:
parent
0522a023b5
commit
3bc81e8c6f
4 changed files with 49 additions and 35 deletions
|
|
@ -12,17 +12,21 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
const albums = try request.repo.all(query);
|
||||
|
||||
for (albums) |album| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .album_id = album.id }).count().execute(request.repo);
|
||||
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 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).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo);
|
||||
if (artist_data) |ad| {
|
||||
try artist_info.put("name", ad.name);
|
||||
try artist_info.put("id", ad.id);
|
||||
}
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
|
||||
try album_view.put("name", album.name);
|
||||
|
|
@ -33,7 +37,10 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
}
|
||||
|
||||
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||
const album = try jetzig.database.Query(.Album).find(id).execute(request.repo);
|
||||
const album = try jetzig.database.Query(.Album)
|
||||
.find(id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
var root = try request.data(.object);
|
||||
try root.put("album", album.?.name);
|
||||
var songs_view = try root.put("songs", .array);
|
||||
|
|
@ -42,9 +49,13 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
.include(.song, .{ .select = .{ .name, .id } })
|
||||
.join(.inner, .album)
|
||||
.where(.{ .album = .{ .id = id } });
|
||||
|
||||
const songs = try request.repo.all(query);
|
||||
for (songs) |song| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.song.id }).count().execute(request.repo);
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
.where(.{ .song_id = song.song.id })
|
||||
.count()
|
||||
.execute(request.repo);
|
||||
var song_view = try songs_view.append(.object);
|
||||
try song_view.put("name", song.song.name);
|
||||
try song_view.put("url", song.song.id);
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
.orderBy(.{ .name = .asc });
|
||||
const artists = try request.repo.all(query);
|
||||
for (artists) |artist| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobbleartist).where(.{ .artist_id = artist.id }).count().execute(request.repo);
|
||||
const scrobbles = try jetzig.database.Query(.Scrobbleartist)
|
||||
.where(.{ .artist_id = artist.id })
|
||||
.count()
|
||||
.execute(request.repo);
|
||||
var artist_view = try artists_view.append(.object);
|
||||
//const output = try request.allocator.dupe(u8, artist.name);
|
||||
//std.mem.replaceScalar(u8, output, ' ', '_');
|
||||
try artist_view.put("name", artist.name);
|
||||
try artist_view.put("url", artist.id);
|
||||
try artist_view.put("scrobbles", scrobbles);
|
||||
|
|
@ -23,7 +24,10 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
}
|
||||
|
||||
pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
||||
const artist = try jetzig.database.Query(.Artist).find(id).execute(request.repo);
|
||||
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);
|
||||
|
|
@ -33,21 +37,16 @@ pub fn get(id: []const u8, request: *jetzig.Request) !jetzig.View {
|
|||
.join(.inner, .artist)
|
||||
.where(.{ .artist = .{ .id = id } });
|
||||
|
||||
//const query = jetzig.database.Query(.Albumartist)
|
||||
// .select(.{ .artist_id, jetquery.sql.count(.album_id) })
|
||||
// .groupBy(.{.artist_id})
|
||||
// .orderBy(.count__album_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);
|
||||
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("name", album.count__album_id);
|
||||
//try album_view.put("url", album.count__album_id);
|
||||
try album_view.put("scrobbles", scrobbles);
|
||||
//std.log.debug("{s}", .{album.album.name});
|
||||
}
|
||||
return request.render(.ok);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ const jetzig = @import("jetzig");
|
|||
pub fn index(request: *jetzig.Request) !jetzig.View {
|
||||
var root = try request.data(.object);
|
||||
var scrobbles_view = try root.put("scrobbles", .array);
|
||||
const query = jetzig.database.Query(.Scrobble).select(.{ .id, .date })
|
||||
const query = jetzig.database.Query(.Scrobble)
|
||||
.select(.{ .id, .date })
|
||||
.include(.song, .{ .select = .{ .id, .name } })
|
||||
.include(.album, .{ .select = .{ .id, .name } })
|
||||
.include(.scrobbleartists, .{ .select = .{.artist_id} })
|
||||
|
|
@ -16,17 +17,16 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
var artist_infos = try scrobble_view.put("artist_info", .array);
|
||||
for (scrobble.scrobbleartists) |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);
|
||||
}
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
|
||||
try scrobble_view.put("song_name", scrobble.song.name);
|
||||
try scrobble_view.put("song_id", scrobble.song.id);
|
||||
//try scrobble_view.put("artist_name", "placeholder");
|
||||
//try scrobble_view.put("artist_id", "placeholder");
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -11,17 +11,21 @@ pub fn index(request: *jetzig.Request) !jetzig.View {
|
|||
const songs = try request.repo.all(query);
|
||||
|
||||
for (songs) |song| {
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble).where(.{ .song_id = song.id }).count().execute(request.repo);
|
||||
const scrobbles = try jetzig.database.Query(.Scrobble)
|
||||
.where(.{ .song_id = song.id })
|
||||
.count()
|
||||
.execute(request.repo);
|
||||
var song_view = try songs_view.append(.object);
|
||||
|
||||
var artist_infos = try song_view.put("artist_info", .array);
|
||||
for (song.songartists) |artist| {
|
||||
var artist_info = try artist_infos.append(.object);
|
||||
const artist_data = try jetzig.database.Query(.Artist).find(artist.artist_id).select(.{ .id, .name }).execute(request.repo);
|
||||
if (artist_data) |ad| {
|
||||
try artist_info.put("name", ad.name);
|
||||
try artist_info.put("id", ad.id);
|
||||
}
|
||||
const artist_data = try jetzig.database.Query(.Artist)
|
||||
.find(artist.artist_id)
|
||||
.select(.{ .id, .name })
|
||||
.execute(request.repo);
|
||||
try artist_info.put("name", artist_data.?.name);
|
||||
try artist_info.put("id", artist_data.?.id);
|
||||
}
|
||||
try song_view.put("name", song.name);
|
||||
try song_view.put("url", song.id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue