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 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { NextRequest } from 'next/server'
|
||||
import { prisma } from '@/src/lib/prisma'
|
||||
|
||||
export async function GET(req: NextRequest) {
|
||||
try {
|
||||
const { searchParams } = new URL(req.url)
|
||||
const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10) || 1)
|
||||
const pageSizeRaw = parseInt(searchParams.get('pageSize') || '20', 10) || 20
|
||||
const pageSize = Math.min(100, Math.max(1, pageSizeRaw))
|
||||
const startDate = searchParams.get('startDate')
|
||||
const endDate = searchParams.get('endDate')
|
||||
const hasFit = ((): boolean | undefined => {
|
||||
const v = searchParams.get('hasFit')
|
||||
return v === null ? undefined : v === 'true'
|
||||
})()
|
||||
const sortBy = searchParams.get('sortBy') || 'timestamp' // timestamp | duration | maxValue
|
||||
const sortOrder = (searchParams.get('sortOrder') === 'asc' ? 'asc' : 'desc') as 'asc' | 'desc'
|
||||
|
||||
const where: any = {}
|
||||
if (startDate || endDate) {
|
||||
where.timestamp = {}
|
||||
if (startDate) where.timestamp.gte = new Date(startDate)
|
||||
if (endDate) where.timestamp.lte = new Date(endDate)
|
||||
}
|
||||
if (hasFit === true) where.NOT = { fit_a: null }
|
||||
if (hasFit === false) where.fit_a = null
|
||||
|
||||
let orderBy: any
|
||||
if (sortBy === 'duration') orderBy = { duration: sortOrder }
|
||||
else if (sortBy === 'maxValue') {
|
||||
if (hasFit === true) orderBy = { force_max: sortOrder }
|
||||
else orderBy = { code_max: sortOrder }
|
||||
} else {
|
||||
orderBy = { timestamp: sortOrder }
|
||||
}
|
||||
|
||||
const [total, rows] = await Promise.all([
|
||||
prisma.recording.count({ where }),
|
||||
prisma.recording.findMany({
|
||||
where,
|
||||
orderBy,
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
select: {
|
||||
id: true,
|
||||
timestamp: true,
|
||||
sample_count: true,
|
||||
duration: true,
|
||||
fit_a: true,
|
||||
fit_b: true,
|
||||
code_min: true,
|
||||
code_max: true,
|
||||
code_avg: true,
|
||||
force_min: true,
|
||||
force_max: true,
|
||||
force_avg: true,
|
||||
},
|
||||
}),
|
||||
])
|
||||
|
||||
const data = rows.map((r) => ({
|
||||
id: r.id,
|
||||
timestamp: r.timestamp.toISOString(),
|
||||
sampleCount: r.sample_count,
|
||||
duration: r.duration,
|
||||
hasFit: r.fit_a !== null && r.fit_a !== undefined,
|
||||
fit: r.fit_a !== null && r.fit_a !== undefined ? { a: r.fit_a!, b: r.fit_b! } : undefined,
|
||||
stats: {
|
||||
codeMin: r.code_min,
|
||||
codeMax: r.code_max,
|
||||
codeAvg: r.code_avg,
|
||||
forceMin: r.force_min ?? undefined,
|
||||
forceMax: r.force_max ?? undefined,
|
||||
forceAvg: r.force_avg ?? undefined,
|
||||
},
|
||||
}))
|
||||
|
||||
return Response.json({
|
||||
data,
|
||||
pagination: {
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
totalPages: Math.max(1, Math.ceil(total / pageSize)),
|
||||
},
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
return Response.json({ error: 'internal error' }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user