project-standalo-sonic-cloud/app/api/artists/route.ts

82 lines
1.8 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { prisma } from '@/lib/prisma'
import { getCurrentUser, slugify } from '@/lib/auth'
export async function POST(request: NextRequest) {
try {
const currentUser = await getCurrentUser()
if (!currentUser) {
return NextResponse.json(
{ error: 'Unauthorized' },
{ status: 401 }
)
}
if (currentUser.artist) {
return NextResponse.json(
{ error: 'Artist profile already exists' },
{ status: 409 }
)
}
const body = await request.json()
const { name, bio, website, twitter, instagram, spotify } = body
if (!name) {
return NextResponse.json(
{ error: 'Artist name is required' },
{ status: 400 }
)
}
const slug = slugify(name)
const existingSlug = await prisma.artist.findUnique({
where: { slug },
})
if (existingSlug) {
return NextResponse.json(
{ error: 'An artist with this name already exists' },
{ status: 409 }
)
}
const artist = await prisma.artist.create({
data: {
userId: currentUser.id,
name,
slug,
bio,
website,
twitter,
instagram,
spotify,
},
include: {
user: {
select: {
displayName: true,
avatarUrl: true,
},
},
},
})
// Update user role to artist
await prisma.user.update({
where: { id: currentUser.id },
data: { role: 'artist' },
})
return NextResponse.json({ artist }, { status: 201 })
} catch (error) {
console.error('Create artist error:', error)
return NextResponse.json(
{ error: 'Failed to create artist profile' },
{ status: 500 }
)
}
}