89 lines
2.1 KiB
TypeScript
89 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireArtist, slugify } from '@/lib/auth'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const { artist } = await requireArtist()
|
|
|
|
const body = await request.json()
|
|
const {
|
|
title,
|
|
description,
|
|
audioUrl,
|
|
coverUrl,
|
|
duration,
|
|
waveformUrl,
|
|
albumId,
|
|
genreIds,
|
|
isPublic = true,
|
|
} = body
|
|
|
|
if (!title || !audioUrl || !duration) {
|
|
return NextResponse.json(
|
|
{ error: 'Title, audioUrl, and duration are required' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
const slug = slugify(title)
|
|
|
|
const song = await prisma.song.create({
|
|
data: {
|
|
artistId: artist.id,
|
|
title,
|
|
slug,
|
|
description,
|
|
audioUrl,
|
|
coverUrl,
|
|
duration,
|
|
waveformUrl,
|
|
albumId,
|
|
isPublic,
|
|
genres: genreIds
|
|
? {
|
|
create: genreIds.map((genreId: string) => ({
|
|
genreId,
|
|
})),
|
|
}
|
|
: undefined,
|
|
},
|
|
include: {
|
|
artist: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
avatarUrl: true,
|
|
verified: true,
|
|
},
|
|
},
|
|
album: {
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
slug: true,
|
|
coverUrl: true,
|
|
},
|
|
},
|
|
genres: {
|
|
include: {
|
|
genre: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(song, { status: 201 })
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
if (error instanceof Error && error.message === 'Artist profile required') {
|
|
return NextResponse.json({ error: 'Artist profile required' }, { status: 403 })
|
|
}
|
|
console.error('Error uploading song:', error)
|
|
return NextResponse.json({ error: 'Failed to upload song' }, { status: 500 })
|
|
}
|
|
}
|