project-standalo-sonic-cloud/app/api/labels/[id]/stats/route.ts

62 lines
1.5 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
// Verify label exists
const label = await prisma.label.findUnique({
where: { id },
select: { id: true },
})
if (!label) {
return NextResponse.json({ error: 'Label not found' }, { status: 404 })
}
// Get artist count
const artistCount = await prisma.artist.count({
where: { labelId: id },
})
// Get artists' song and album counts and total plays
const artists = await prisma.artist.findMany({
where: { labelId: id },
select: { id: true },
})
const artistIds = artists.map(a => a.id)
const songCount = await prisma.song.count({
where: { artistId: { in: artistIds } },
})
const albumCount = await prisma.album.count({
where: { artistId: { in: artistIds } },
})
const songs = await prisma.song.findMany({
where: { artistId: { in: artistIds } },
select: { playCount: true },
})
const totalPlays = songs.reduce((sum, song) => sum + song.playCount, 0)
const stats = {
artistCount,
songCount,
albumCount,
totalPlays,
}
return NextResponse.json(stats)
} catch (error) {
console.error('Error fetching label stats:', error)
return NextResponse.json({ error: 'Failed to fetch label stats' }, { status: 500 })
}
}