56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth } from '@/lib/auth'
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string; songId: string }> }
|
|
) {
|
|
try {
|
|
const user = await requireAuth()
|
|
const { id, songId } = await params
|
|
|
|
const playlist = await prisma.playlist.findUnique({
|
|
where: { id },
|
|
})
|
|
|
|
if (!playlist) {
|
|
return NextResponse.json({ error: 'Playlist not found' }, { status: 404 })
|
|
}
|
|
|
|
if (playlist.userId !== user.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
const playlistSong = await prisma.playlistSong.findUnique({
|
|
where: {
|
|
playlistId_songId: {
|
|
playlistId: id,
|
|
songId,
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!playlistSong) {
|
|
return NextResponse.json({ error: 'Song not in playlist' }, { status: 404 })
|
|
}
|
|
|
|
await prisma.playlistSong.delete({
|
|
where: {
|
|
playlistId_songId: {
|
|
playlistId: id,
|
|
songId,
|
|
},
|
|
},
|
|
})
|
|
|
|
return NextResponse.json({ message: 'Song removed from playlist successfully' })
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
console.error('Error removing song from playlist:', error)
|
|
return NextResponse.json({ error: 'Failed to remove song from playlist' }, { status: 500 })
|
|
}
|
|
}
|