WIP. A little custom music server
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix: update getAlbumList

+13 -79
+13 -79
backend/src/api.ts
··· 31 31 ApiService, 32 32 { 33 33 readonly getAlbumList: () => Effect.Effect<AlbumWithArtist[], SqlError.SqlError, DatabaseLive>; 34 - readonly getAlbum2: (id: string) => Effect.Effect<any, SqlError.SqlError | AlbumNotFoundError, DatabaseLive>; 35 34 readonly getAlbum: ( 36 35 id: string, 37 36 ) => Effect.Effect<GetAlbum, SqlError.SqlError | AlbumNotFoundError, DatabaseLive>; ··· 91 90 songs, 92 91 }; 93 92 }), 94 - getAlbum2: (id: string) => 95 - Effect.gen(function* () { 96 - const rows = yield* db 97 - .select({ 98 - album: albumTable, 99 - song: { 100 - id: songTable.id, 101 - fileId: songTable.fileId, 102 - title: songTable.title, 103 - }, 104 - artist: artistTable, 105 - }) 106 - .from(albumTable) 107 - .innerJoin(songTable, eq(songTable.albumId, albumTable.id)) 108 - .innerJoin(songToArtistTable, eq(songToArtistTable.songId, songTable.id)) 109 - .innerJoin(artistTable, eq(songToArtistTable.artistId, artistTable.id)) 110 - .where(eq(albumTable.id, id)); 111 - 112 - yield* Console.table(rows); 113 - 114 - const firstRow = rows.at(0); 115 - if (!firstRow) { 116 - return yield* new AlbumNotFoundError({ 117 - message: "Can't extract album from selected rows", 118 - cause: rows, 119 - }); 120 - } 121 - 122 - const songsMap = rows.reduce<Record<string, Omit<SongWithArtists, "albumId">>>((acc, row) => { 123 - const { song, artist } = row; 124 - 125 - if (!acc[song.id]) { 126 - acc[song.id] = { 127 - id: song.id, 128 - fileId: song.fileId, 129 - title: song.title, 130 - artists: [] as Artist[], 131 - }; 132 - } 133 - 134 - if (artist) { 135 - acc[song.id].artists.push(artist); 136 - } 137 - 138 - return acc; 139 - }, {}); 140 - 141 - const album = { 142 - id: firstRow.album.id, 143 - title: firstRow.album.title, 144 - songs: Object.values(songsMap), 145 - }; 146 - 147 - return album; 148 - }), 149 93 getAlbumList: () => 150 94 Effect.gen(function* () { 151 - const rows = yield* db 152 - .select({ 153 - album: albumTable, 154 - artist: artistTable, 155 - }) 156 - .from(albumTable) 157 - .innerJoin(artistToAlbumTable, eq(albumTable.id, artistToAlbumTable.albumId)) 158 - .innerJoin(artistTable, eq(artistTable.id, artistToAlbumTable.artistId)); 159 - 160 - const result = rows.reduce<Record<string, AlbumWithArtist>>((acc, cur) => { 161 - const albumId = cur.album.id; 162 - 163 - if (!acc[albumId]) { 164 - acc[albumId] = { 165 - ...cur.album, 166 - artists: [], 167 - }; 168 - } 95 + const rows = yield* db.query.albumTable.findMany({ 96 + with: { 97 + artists: { 98 + with: { 99 + artist: true, 100 + }, 101 + }, 102 + }, 103 + }); 169 104 170 - acc[albumId]?.artists.push(cur.artist); 171 - return acc; 172 - }, {}); 173 - 174 - return Object.values(result); 105 + return rows.map((r) => ({ 106 + ...r, 107 + artists: r.artists.map((a) => a.artist), 108 + })); 175 109 }), 176 110 }; 177 111 }),