added fuzzy search for table

This commit is contained in:
Polaris
2024-08-21 13:34:23 -04:00
parent 7fe79979b5
commit 007b23783a
5 changed files with 226 additions and 96 deletions

View File

@ -1,21 +1,19 @@
"use server";
import { artemis, daphnis } from "@/lib/prisma";
import type * as Prisma from "@prisma/client";
type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient;
type ChuniScorePlaylog = Prisma.PrismaClient;
type ChuniStaticMusic = Prisma.PrismaClient;
type LinkSharingToken = {
playlogId: number;
};
playlogId: number;
};
export async function getSongsWithTitles(userId: number) {
try {
const songs: ChuniScorePlaylog[] = await artemis.chuni_score_playlog.findMany({
try {
const songs: ChuniScorePlaylog[] =
await artemis.chuni_score_playlog.findMany({
where: {
user: userId,
},
@ -63,12 +61,13 @@ export async function getSongsWithTitles(userId: number) {
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({
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,
@ -82,74 +81,76 @@ export async function getSongsWithTitles(userId: number) {
level: true,
genre: true,
worldsEndTag: true,
jacketPath: true,
jacketPath: true,
},
});
const playCounts = await artemis.chuni_score_playlog.groupBy({
by: ['musicId'],
_count: {
musicId: true,
const playCounts = await artemis.chuni_score_playlog.groupBy({
by: ["musicId"],
_count: {
musicId: true,
},
where: {
user: userId,
musicId: {
in: chuniScorePlaylogMusicId,
},
where: {
user: userId,
musicId: {
in: chuniScorePlaylogMusicId,
},
},
});
const playCountMap = playCounts.reduce((map, item) => {
},
});
const playCountMap = playCounts.reduce(
(map, item) => {
if (item.musicId !== null) {
map[item.musicId] = item._count.musicId;
}
return map;
}, {} as Record<number, number>);
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level
);
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: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0,
jacketPath: staticInfo?.jacketPath || "",
};
});
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}
},
{} as Record<number, number>,
);
const songsWithTitles = songs.map((song) => {
const staticInfo = staticMusicInfo.find(
(chuniStaticMusic) =>
chuniStaticMusic.songId === song.musicId &&
chuniStaticMusic.chartId === song.level,
);
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: song.musicId !== null ? playCountMap[song.musicId] || 0 : 0,
jacketPath: staticInfo?.jacketPath || "",
};
});
return songsWithTitles;
} catch (error) {
console.error("Error fetching songs with titles:", error);
throw error;
}
}
export async function generatePlaylogId(playlogid: number) {
try {
const tokens = (await daphnis.linkSharingToken.findMany({
where: {
playlogId: playlogid,
},
select: {
playlogId: true,
},
})) as LinkSharingToken[];
const playlogIds: number[] = tokens.map((token) => token.playlogId);
return playlogIds;
} catch (error) {
console.error("Error fetching playlogIds:", error);
throw error;
}
}
try {
const tokens = (await daphnis.linkSharingToken.findMany({
where: {
playlogId: playlogid,
},
select: {
playlogId: true,
},
})) as LinkSharingToken[];
const playlogIds: number[] = tokens.map((token) => token.playlogId);
return playlogIds;
} catch (error) {
console.error("Error fetching playlogIds:", error);
throw error;
}
}