114 lines
2.8 KiB
TypeScript
114 lines
2.8 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/prisma'
|
|
import { requireAuth, slugify } from '@/lib/auth'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const { id } = await params
|
|
|
|
const label = await prisma.label.findUnique({
|
|
where: { id },
|
|
include: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
username: true,
|
|
displayName: true,
|
|
},
|
|
},
|
|
artists: {
|
|
select: {
|
|
id: true,
|
|
name: true,
|
|
slug: true,
|
|
avatarUrl: true,
|
|
verified: true,
|
|
},
|
|
},
|
|
_count: {
|
|
select: {
|
|
artists: true,
|
|
invitations: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!label) {
|
|
return NextResponse.json({ error: 'Label not found' }, { status: 404 })
|
|
}
|
|
|
|
return NextResponse.json(label)
|
|
} catch (error) {
|
|
console.error('Error fetching label:', error)
|
|
return NextResponse.json({ error: 'Failed to fetch label' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function PUT(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const user = await requireAuth()
|
|
const { id } = await params
|
|
|
|
// Check if user owns this label
|
|
const label = await prisma.label.findUnique({
|
|
where: { id },
|
|
select: { userId: true },
|
|
})
|
|
|
|
if (!label) {
|
|
return NextResponse.json({ error: 'Label not found' }, { status: 404 })
|
|
}
|
|
|
|
if (label.userId !== user.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 403 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { name, description, logoUrl, website } = body
|
|
|
|
const updateData: any = {}
|
|
if (name !== undefined) {
|
|
updateData.name = name
|
|
updateData.slug = slugify(name)
|
|
}
|
|
if (description !== undefined) updateData.description = description
|
|
if (logoUrl !== undefined) updateData.logoUrl = logoUrl
|
|
if (website !== undefined) updateData.website = website
|
|
|
|
const updatedLabel = await prisma.label.update({
|
|
where: { id },
|
|
data: updateData,
|
|
include: {
|
|
user: {
|
|
select: {
|
|
email: true,
|
|
username: true,
|
|
displayName: true,
|
|
},
|
|
},
|
|
_count: {
|
|
select: {
|
|
artists: true,
|
|
invitations: true,
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(updatedLabel)
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'Unauthorized') {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
console.error('Error updating label:', error)
|
|
return NextResponse.json({ error: 'Failed to update label' }, { status: 500 })
|
|
}
|
|
}
|