list page pt. 2
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { desc, eq, like } from "drizzle-orm";
|
||||
import { desc, eq, inArray, like } from "drizzle-orm";
|
||||
import { anime, animeSongLinks, artists, groups, songs } from "$lib/db/schema";
|
||||
import type { ClientDb } from "./index";
|
||||
|
||||
@@ -124,6 +124,53 @@ export async function getAnimeWithSongsByAnnId(db: ClientDb, annId: number) {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch songs for a list of MAL anime ids.
|
||||
*
|
||||
* This joins `anime` -> `anime_song_links` -> `songs` and includes artist/group name.
|
||||
*
|
||||
* Intended usage: MAL animelist entries give you `node.id` (MAL id); pass those ids here.
|
||||
*/
|
||||
export async function getSongsForMalAnimeIds(
|
||||
db: ClientDb,
|
||||
malAnimeIds: number[],
|
||||
) {
|
||||
const ids = malAnimeIds.filter((n) => Number.isFinite(n));
|
||||
if (ids.length === 0) return [];
|
||||
|
||||
return db
|
||||
.select({
|
||||
annId: anime.annId,
|
||||
malId: anime.malId,
|
||||
animeName: anime.mainName,
|
||||
year: anime.year,
|
||||
seasonId: anime.seasonId,
|
||||
|
||||
annSongId: animeSongLinks.annSongId,
|
||||
type: animeSongLinks.type,
|
||||
number: animeSongLinks.number,
|
||||
|
||||
songName: songs.name,
|
||||
fileName: songs.fileName,
|
||||
|
||||
artistName: artists.name,
|
||||
groupName: groups.name,
|
||||
})
|
||||
.from(anime)
|
||||
.innerJoin(animeSongLinks, eq(animeSongLinks.annId, anime.annId))
|
||||
.innerJoin(songs, eq(songs.annSongId, animeSongLinks.annSongId))
|
||||
.leftJoin(artists, eq(artists.songArtistId, songs.songArtistId))
|
||||
.leftJoin(groups, eq(groups.songGroupId, songs.songGroupId))
|
||||
.where(inArray(anime.malId, ids))
|
||||
.orderBy(
|
||||
desc(anime.year),
|
||||
desc(anime.seasonId),
|
||||
desc(anime.annId),
|
||||
desc(animeSongLinks.type),
|
||||
desc(animeSongLinks.number),
|
||||
);
|
||||
}
|
||||
|
||||
function clampLimit(limit: number) {
|
||||
const n = Number(limit);
|
||||
if (!Number.isFinite(n)) return DEFAULT_LIST_LIMIT;
|
||||
|
||||
Reference in New Issue
Block a user