"use server"; import { artemis } from "@/lib/prisma"; import { chuni_score_best } from "@/prisma/schemas/artemis/generated/artemis"; import type * as Prisma from "@prisma/client"; type ChuniScorePlaylog = Prisma.PrismaClient; type ChuniStaticMusic = Prisma.PrismaClient; export async function getSongsWithTitles(userId: number) { try { const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({ where: { user: userId, }, orderBy: { userPlayDate: "desc", }, select: { id: true, maxCombo: true, userPlayDate: true, isFullCombo: true, playerRating: true, isAllJustice: true, score: true, judgeHeaven: true, judgeGuilty: true, judgeJustice: true, judgeAttack: true, judgeCritical: true, isClear: true, skillId: true, skillEffect: true, skillLevel: true, isNewRecord: true, musicId: true, level: true, rateAir: true, rateHold: true, rateFlick: true, rateSlide: true, rateTap: true, romVersion: true, eventId: true, characterId: true, charaIllustId: true, track: true, isContinue: true, isFreeToPlay: true, playKind: true, playDate: true, orderId: true, sortNumber: true, user: true, placeId: true, ticketId: true, }, }); const chuniScorePlaylogMusicId = songs .map((song) => song.musicId) .filter((id): id is number => id !== null); const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({ where: { songId: { in: chuniScorePlaylogMusicId, }, }, select: { songId: true, title: true, artist: true, chartId: true, level: true, genre: true, worldsEndTag: true, jacketPath: true, }, }); const chuniScoreBest: chuni_score_best[] = await artemis.chuni_score_best.findMany({ where: { musicId: { in: chuniScorePlaylogMusicId, }, }, select: { id: true, user: true, musicId: true, level: true, playCount: true, scoreMax: true, resRequestCount: true, resAcceptCount: true, resSuccessCount: true, missCount: true, maxComboCount: true, isFullCombo: true, isAllJustice: true, isSuccess: true, fullChain: true, maxChain: true, scoreRank: true, isLock: true, ext1: true, theoryCount: true, }, }); const songsWithTitles = songs.map((song) => { const staticInfo = staticMusicInfo.find( (chuniStaticMusic) => chuniStaticMusic.songId === song.musicId && chuniStaticMusic.chartId === song.level, ); const bestScore = chuniScoreBest.find( (score) => score.musicId === song.musicId, ); return { ...song, title: staticInfo?.title || "Unknown Title", artist: staticInfo?.artist || "Unknown Artist", genre: staticInfo?.genre || "Unknown Genre", chartId: staticInfo?.chartId || "Unknown chartId", level: staticInfo?.level || "Unknown Level", chartlevel: song.level || "Unknown Level", playCount: bestScore?.playCount || 0, isSuccess: bestScore?.isSuccess || 0, jacketPath: staticInfo?.jacketPath || "", }; }); return songsWithTitles; } catch (error) { console.error("Error fetching songs with titles:", error); throw error; } } export async function searchSongWithTitle( userId: number, searchQuery: string = "", ) { try { const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({ where: { user: userId, }, orderBy: { userPlayDate: "desc", }, select: { id: true, }, }); const chuniScorePlaylogMusicId = songs .map((song) => song.musicId) .filter((id): id is number => id !== null); const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({ where: { songId: { in: chuniScorePlaylogMusicId, }, title: { contains: searchQuery, }, }, select: { songId: true, title: true, }, }); const chuniScoreBest: chuni_score_best[] = await artemis.chuni_score_best.findMany({ where: { musicId: { in: chuniScorePlaylogMusicId, }, }, select: { id: true, user: true, musicId: true, level: true, playCount: true, scoreMax: true, resRequestCount: true, resAcceptCount: true, resSuccessCount: true, missCount: true, maxComboCount: true, isFullCombo: true, isAllJustice: true, isSuccess: true, fullChain: true, maxChain: true, scoreRank: true, isLock: true, ext1: true, theoryCount: true, }, }); const songsWithTitles = songs.map((song) => { const staticInfo = staticMusicInfo.find( (chuniStaticMusic) => chuniStaticMusic.songId === song.musicId && chuniStaticMusic.chartId === song.level, ); const bestScore = chuniScoreBest.find( (score) => score.musicId === song.musicId, ); return { ...song, title: staticInfo?.title || "Unknown Title", playCount: bestScore?.playCount || 0, }; }); return songsWithTitles; } catch (error) { console.error("Error fetching songs with titles:", error); throw error; } }