aethex.live/app/api/streams/route.ts

77 lines
1.7 KiB
TypeScript

import { PrismaClient } from '@prisma/client'
import { NextRequest, NextResponse } from 'next/server'
const prisma = new PrismaClient()
// GET /api/streams - List current live streams
export async function GET(req: NextRequest) {
try {
const limit = 20
const page = parseInt(req.nextUrl.searchParams.get('page') || '1')
const skip = (page - 1) * limit
const streams = await prisma.stream.findMany({
where: {
status: 'live',
channel: {
isSuspended: false,
},
},
select: {
id: true,
title: true,
description: true,
hlsUrl: true,
thumbnailUrl: true,
viewerCount: true,
peakViewers: true,
startedAt: true,
channel: {
select: {
id: true,
name: true,
slug: true,
category: true,
user: {
select: {
displayName: true,
avatarUrl: true,
},
},
},
},
},
orderBy: { viewerCount: 'desc' },
skip,
take: limit,
})
const total = await prisma.stream.count({
where: {
status: 'live',
channel: {
isSuspended: false,
},
},
})
return NextResponse.json(
{
streams,
pagination: {
page,
limit,
total,
pages: Math.ceil(total / limit),
},
},
{ status: 200 }
)
} catch (error) {
console.error('Error fetching streams:', error)
return NextResponse.json(
{ error: 'Failed to fetch streams' },
{ status: 500 }
)
}
}