Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | export const dynamic = 'force-dynamic' import {z} from 'zod' import {prisma} from '@/lib/db' import {requireOwner} from '@/lib/guard' import {slugify} from '@/lib/band-fields' import {logActivity} from '@/lib/songs' import {route} from '@/lib/route' /** Every band on this install, for its owner. */ export const GET = route(async () => { await requireOwner() const bands = await prisma.band.findMany({ orderBy: {name: 'asc'}, select: { id: true, name: true, appName: true, domains: {select: {host: true}, orderBy: {host: 'asc'}}, _count: {select: {memberships: true, songs: true}}, }, }) return Response.json(bands) }) const Body = z.object({name: z.string().trim().min(1).max(80)}) /** Start a new band. Whoever starts it is its first admin. */ export const POST = route(async (req: Request) => { const {user} = await requireOwner() const parsed = Body.safeParse(await req.json()) if (!parsed.success) return Response.json({error: 'Give the band a name.'}, {status: 400}) const base = slugify(parsed.data.name) let slug = base for (let n = 2; await prisma.band.findUnique({where: {slug}}); n++) slug = `${base}-${n}` const band = await prisma.$transaction(async (tx) => { const b = await tx.band.create({data: {name: parsed.data.name, slug}}) await tx.membership.create({ data: {userId: user.id, bandId: b.id, isAdmin: true}, }) await logActivity(tx, { bandId: b.id, userId: user.id, action: 'band.create', targetType: 'band', targetId: b.id, summary: `started ${b.name}`, }) return b }) return Response.json({id: band.id}, {status: 201}) }) |