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

190 lines
5.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { requireArtist, 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 song = await prisma.song.findUnique({
where: { id },
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,
},
},
},
})
if (!song) {
return NextResponse.json({ error: 'Song not found' }, { status: 404 })
}
// Only allow private songs to be viewed by the artist
if (!song.isPublic && (!user || !user.artist || user.artist.id !== song.artistId)) {
return NextResponse.json({ error: 'Song not found' }, { status: 404 })
}
return NextResponse.json(song)
} catch (error) {
console.error('Error fetching song:', error)
return NextResponse.json({ error: 'Failed to fetch song' }, { status: 500 })
}
}
export async function PUT(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { artist } = await requireArtist()
const { id } = await params
const existingSong = await prisma.song.findUnique({
where: { id },
})
if (!existingSong) {
return NextResponse.json({ error: 'Song not found' }, { status: 404 })
}
if (existingSong.artistId !== artist.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
}
const body = await request.json()
const {
title,
description,
coverUrl,
albumId,
genreIds,
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 (albumId !== undefined) updateData.albumId = albumId
if (isPublic !== undefined) updateData.isPublic = isPublic
// Handle genre updates separately
if (genreIds !== undefined) {
await prisma.songGenre.deleteMany({
where: { songId: id },
})
if (genreIds.length > 0) {
await prisma.songGenre.createMany({
data: genreIds.map((genreId: string) => ({
songId: id,
genreId,
})),
})
}
}
const updatedSong = await prisma.song.update({
where: { id },
data: updateData,
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(updatedSong)
} 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 updating song:', error)
return NextResponse.json({ error: 'Failed to update song' }, { status: 500 })
}
}
export async function DELETE(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
try {
const { artist } = await requireArtist()
const { id } = await params
const existingSong = await prisma.song.findUnique({
where: { id },
})
if (!existingSong) {
return NextResponse.json({ error: 'Song not found' }, { status: 404 })
}
if (existingSong.artistId !== artist.id) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
}
await prisma.song.delete({
where: { id },
})
return NextResponse.json({ message: 'Song deleted successfully' })
} 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 deleting song:', error)
return NextResponse.json({ error: 'Failed to delete song' }, { status: 500 })
}
}