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

163 lines
4.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { requireAuth, slugify, getCurrentUser } from '@/lib/auth'
export async function GET(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { id } = await params
const user = await getCurrentUser()
const playlist = await prisma.playlist.findUnique({
where: { id },
include: {
user: {
select: {
id: true,
username: true,
displayName: true,
avatarUrl: true,
},
},
songs: {
include: {
song: {
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,
},
},
},
},
},
orderBy: {
position: 'asc',
},
},
},
})
if (!playlist) {
return NextResponse.json({ error: 'Playlist not found' }, { status: 404 })
}
// Only allow private playlists to be viewed by the owner
if (!playlist.isPublic && (!user || user.id !== playlist.userId)) {
return NextResponse.json({ error: 'Playlist not found' }, { status: 404 })
}
return NextResponse.json(playlist)
} catch (error) {
console.error('Error fetching playlist:', error)
return NextResponse.json({ error: 'Failed to fetch playlist' }, { status: 500 })
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const user = await requireAuth()
const { id } = await params
const existingPlaylist = await prisma.playlist.findUnique({
where: { id },
})
if (!existingPlaylist) {
return NextResponse.json({ error: 'Playlist not found' }, { status: 404 })
}
if (existingPlaylist.userId !== user.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
}
const body = await request.json()
const { title, description, coverUrl, isPublic } = body
const updateData: any = {}
if (title !== undefined) {
updateData.title = title
updateData.slug = slugify(title)
}
if (description !== undefined) updateData.description = description
if (coverUrl !== undefined) updateData.coverUrl = coverUrl
if (isPublic !== undefined) updateData.isPublic = isPublic
const updatedPlaylist = await prisma.playlist.update({
where: { id },
data: updateData,
include: {
_count: {
select: {
songs: true,
},
},
},
})
return NextResponse.json(updatedPlaylist)
} catch (error) {
if (error instanceof Error && error.message === 'Unauthorized') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
console.error('Error updating playlist:', error)
return NextResponse.json({ error: 'Failed to update playlist' }, { status: 500 })
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const user = await requireAuth()
const { id } = await params
const existingPlaylist = await prisma.playlist.findUnique({
where: { id },
})
if (!existingPlaylist) {
return NextResponse.json({ error: 'Playlist not found' }, { status: 404 })
}
if (existingPlaylist.userId !== user.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
}
await prisma.playlist.delete({
where: { id },
})
return NextResponse.json({ message: 'Playlist deleted successfully' })
} catch (error) {
if (error instanceof Error && error.message === 'Unauthorized') {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
}
console.error('Error deleting playlist:', error)
return NextResponse.json({ error: 'Failed to delete playlist' }, { status: 500 })
}
}