Edit process_scrobbles to conform to new schema

This commit is contained in:
mitteneer 2025-03-28 17:26:31 -04:00
parent 9760ca7fbc
commit 58e232009b

View file

@ -78,31 +78,38 @@ pub fn run(allocator: std.mem.Allocator, params: *jetzig.data.Value, env: jetzig
// but song_check/album is not. Also yes, the order of these // but song_check/album is not. Also yes, the order of these
// checks is weird, I didn't put a lot of thought into it // checks is weird, I didn't put a lot of thought into it
var associative_table_flags: [3]bool = [3]bool{ true, true, true }; var associative_table_flags: [3]bool = [3]bool{ true, true, true };
var associative_table_ids: [3][]const u8 = [3][]const u8{ null, null, null };
if (album_check == null) { if (album_check == null) {
try env.repo.execute(album_insert); try env.repo.execute(album_insert);
try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).execute(env.repo); associative_table_ids[0] = try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo);
associative_table_flags[0] = false; associative_table_flags[0] = false;
try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).execute(env.repo); associative_table_ids[1] = try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).returning(.{.id}).execute(env.repo);
associative_table_flags[1] = false; associative_table_flags[1] = false;
} }
if (artist_check == null) { if (artist_check == null) {
try env.repo.execute(artist_insert); try env.repo.execute(artist_insert);
if (associative_table_flags[0]) try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).execute(env.repo); if (associative_table_flags[0]) associative_table_ids[0] = try jetzig.database.Query(.Albumartist).insert(.{ .album_id = album_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo);
try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).execute(env.repo); associative_table_ids[2] = try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo);
associative_table_flags[2] = false; associative_table_flags[2] = false;
} }
if (song_check == null) { if (song_check == null) {
try env.repo.execute(song_insert); try env.repo.execute(song_insert);
if (associative_table_flags[1]) try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).execute(env.repo); if (associative_table_flags[1]) associative_table_ids[1] = try jetzig.database.Query(.Albumsong).insert(.{ .album_id = album_id, .song_id = song_id }).returning(.{.id}).execute(env.repo);
if (associative_table_flags[2]) try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).execute(env.repo); if (associative_table_flags[2]) associative_table_ids[2] = try jetzig.database.Query(.Songartist).insert(.{ .song_id = song_id, .artist_id = artist_id }).returning(.{.id}).execute(env.repo);
} }
const scr_id = try jetzig.database.Query(.Scrobble).insert(.{ .song_id = song_id, .album_id = album_id, .date = scrobble.date }).returning(.{.id}).execute(env.repo); defer {
defer env.repo.free(scr_id); for (0..3) |i| {
try jetzig.database.Query(.Scrobbleartist).insert(.{ .scrobble_id = scr_id.?.id, .artist_id = artist_id }).execute(env.repo); if (associative_table_ids[i]) env.repo.free(associative_table_ids[i]);
}
}
try jetzig.database.Query(.Scrobble).insert(.{ .albumartists_id = associative_table_ids[0], .albumsong_id = associative_table_ids[1], .songartists_id = associative_table_ids[2], .date = scrobble.date }).execute(env.repo);
//defer env.repo.free(scr_id);
//try jetzig.database.Query(.Scrobbleartist).insert(.{ .scrobble_id = scr_id.?.id, .artist_id = artist_id }).execute(env.repo);
} }
} }