36 lines
1.4 KiB
TypeScript
36 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import pool from '@/lib/db';
|
|
import { withAuth } from '@/lib/apiHelpers';
|
|
|
|
export const GET = withAuth(async () => {
|
|
const result = await pool.query('SELECT * FROM charts ORDER BY pos_y ASC, pos_x ASC');
|
|
return NextResponse.json(result.rows);
|
|
});
|
|
|
|
export const POST = withAuth(async (req: NextRequest) => {
|
|
const body = await req.json();
|
|
const {
|
|
title,
|
|
pos_x = 0, pos_y = 0, width = 2, height = 4,
|
|
signal_type = 'both', chart_type = 'signals', viz_type = 'line',
|
|
filter_combinators = null, filter_items = null,
|
|
filter_items_exclude = null, filter_items_regex = false,
|
|
y_min = null, y_max = null, y_scale = 'linear',
|
|
series_limit = 20, order_by = 'time',
|
|
} = body;
|
|
|
|
if (!title) return NextResponse.json({ error: 'title required' }, { status: 400 });
|
|
|
|
const result = await pool.query(
|
|
`INSERT INTO charts
|
|
(title,pos_x,pos_y,width,height,signal_type,chart_type,viz_type,
|
|
filter_combinators,filter_items,filter_items_exclude,filter_items_regex,
|
|
y_min,y_max,y_scale,series_limit,order_by)
|
|
VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17)
|
|
RETURNING *`,
|
|
[title,pos_x,pos_y,width,height,signal_type,chart_type,viz_type,
|
|
filter_combinators,filter_items,filter_items_exclude,filter_items_regex,
|
|
y_min,y_max,y_scale,series_limit,order_by],
|
|
);
|
|
return NextResponse.json(result.rows[0], { status: 201 });
|
|
}); |