added data to sharing page
This commit is contained in:
162
lib/api.ts
162
lib/api.ts
@ -2,91 +2,111 @@
|
||||
|
||||
import { getAuth } from "@/auth/queries/getauth";
|
||||
import { artemis, daphnis } from "@/lib/prisma";
|
||||
import type * as Prisma from '@prisma/client';
|
||||
import type * as Prisma from "@prisma/client";
|
||||
|
||||
type ChuniScorePlaylog = Prisma.PrismaClient;
|
||||
type ChuniStaticMusic = Prisma.PrismaClient;
|
||||
|
||||
type LinkSharingToken = {
|
||||
playlogId: number;
|
||||
};
|
||||
|
||||
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,
|
||||
},
|
||||
});
|
||||
// Fetch songs played by the user
|
||||
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,
|
||||
},
|
||||
});
|
||||
|
||||
// Extract unique musicIds from the fetched songs
|
||||
const musicIds = songs
|
||||
.map((song) => song.musicId)
|
||||
.filter((id) => id !== null) as number[];
|
||||
|
||||
const staticMusicInfo: ChuniStaticMusic[] = await artemis.chuni_static_music.findMany({
|
||||
where: {
|
||||
songId: {
|
||||
in: musicIds,
|
||||
// Fetch static music information for the extrcted musicIds
|
||||
const staticMusicInfo: ChuniStaticMusic[] =
|
||||
await artemis.chuni_static_music.findMany({
|
||||
where: {
|
||||
songId: {
|
||||
in: musicIds,
|
||||
},
|
||||
},
|
||||
},
|
||||
select: {
|
||||
songId: true,
|
||||
title: true,
|
||||
artist: true,
|
||||
},
|
||||
});
|
||||
select: {
|
||||
songId: true,
|
||||
title: true,
|
||||
artist: true,
|
||||
chartId: true,
|
||||
level: true,
|
||||
genre: true,
|
||||
worldsEndTag: true,
|
||||
},
|
||||
});
|
||||
|
||||
const songsWithTitles = songs.map((song) => ({
|
||||
...song,
|
||||
title:
|
||||
staticMusicInfo.find((title) => title.songId === song.musicId)?.title ||
|
||||
"Unknown Title",
|
||||
artist:
|
||||
staticMusicInfo.find((artist) => artist.songId === song.musicId)
|
||||
?.artist || "Unknown Artist",
|
||||
}));
|
||||
// Map each song with its corresponding static music information
|
||||
const songsWithTitles = songs.map((song) => {
|
||||
// Find static info corresponding to the song's musicId and level (chartId)
|
||||
const staticInfo = staticMusicInfo.find(
|
||||
(chuniStatiMusic) =>
|
||||
chuniStatiMusic.songId === song.musicId &&
|
||||
chuniStatiMusic.chartId === song.level
|
||||
);
|
||||
|
||||
// Return mapped song with title, artist, genre, and correct level
|
||||
return {
|
||||
...song,
|
||||
title: staticInfo?.title || "Unknown Title",
|
||||
artist: staticInfo?.artist || "Unknown Artist",
|
||||
genre: staticInfo?.genre || "Unknown Genre",
|
||||
level: staticInfo?.level || "Unknown Level",
|
||||
chartlevel: song.level || "unkownlevel",
|
||||
};
|
||||
});
|
||||
|
||||
return songsWithTitles;
|
||||
} catch (error) {
|
||||
@ -97,14 +117,14 @@ export async function getSongsWithTitles(userId: number) {
|
||||
|
||||
export async function generatePlaylogId(playlogid: number) {
|
||||
try {
|
||||
const tokens = await daphnis.linkSharingToken.findMany({
|
||||
const tokens = (await daphnis.linkSharingToken.findMany({
|
||||
where: {
|
||||
playlogId: playlogid,
|
||||
},
|
||||
select: {
|
||||
playlogId: true,
|
||||
},
|
||||
}) as LinkSharingToken[];
|
||||
})) as LinkSharingToken[];
|
||||
|
||||
const playlogIds: number[] = tokens.map((token) => token.playlogId);
|
||||
|
||||
|
Reference in New Issue
Block a user