init
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/src/lib/prisma'
|
||||
import { isAuthorizedDelete } from '@/src/lib/utils'
|
||||
|
||||
export async function GET(_req: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
const rec = await prisma.recording.findUnique({
|
||||
where: { id: params.id },
|
||||
select: {
|
||||
id: true,
|
||||
timestamp: true,
|
||||
code_data: true,
|
||||
fit_a: true,
|
||||
fit_b: true,
|
||||
sample_count: true,
|
||||
duration: true,
|
||||
code_min: true,
|
||||
code_max: true,
|
||||
code_avg: true,
|
||||
force_min: true,
|
||||
force_max: true,
|
||||
force_avg: true,
|
||||
},
|
||||
})
|
||||
if (!rec) return Response.json({ error: 'not found' }, { status: 404 })
|
||||
|
||||
return Response.json({
|
||||
id: rec.id,
|
||||
timestamp: rec.timestamp.toISOString(),
|
||||
code: rec.code_data,
|
||||
fit: rec.fit_a != null ? { a: rec.fit_a, b: rec.fit_b } : undefined,
|
||||
sampleCount: rec.sample_count,
|
||||
duration: rec.duration,
|
||||
stats: {
|
||||
codeMin: rec.code_min,
|
||||
codeMax: rec.code_max,
|
||||
codeAvg: rec.code_avg,
|
||||
forceMin: rec.force_min ?? undefined,
|
||||
forceMax: rec.force_max ?? undefined,
|
||||
forceAvg: rec.force_avg ?? undefined,
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return Response.json({ error: 'internal error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
|
||||
export async function DELETE(req: NextRequest, { params }: { params: { id: string } }) {
|
||||
try {
|
||||
if (!isAuthorizedDelete(req)) {
|
||||
return Response.json({ error: 'unauthorized' }, { status: 401 })
|
||||
}
|
||||
const rec = await prisma.recording.delete({ where: { id: params.id } })
|
||||
return Response.json({ success: true, id: rec.id })
|
||||
} catch (err: any) {
|
||||
if (err?.code === 'P2025') {
|
||||
return Response.json({ error: 'not found' }, { status: 404 })
|
||||
}
|
||||
console.error(err)
|
||||
return Response.json({ error: 'internal error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user