64 lines
3.0 KiB
TypeScript
64 lines
3.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import pool from '@/lib/db';
|
|
import { withAuth } from '@/lib/apiHelpers';
|
|
|
|
export const PUT = withAuth(async (req: NextRequest, { params }) => {
|
|
const { id } = await params;
|
|
const body = await req.json();
|
|
const {
|
|
title, pos_x, pos_y, width, height,
|
|
signal_type, chart_type, viz_type,
|
|
filter_items_regex, y_scale,
|
|
series_limit, order_by,
|
|
} = body;
|
|
|
|
const hasFilterCombinators = 'filter_combinators' in body;
|
|
const hasFilterItems = 'filter_items' in body;
|
|
const hasFilterItemsExclude = 'filter_items_exclude' in body;
|
|
const hasYMin = 'y_min' in body;
|
|
const hasYMax = 'y_max' in body;
|
|
|
|
const result = await pool.query(
|
|
`UPDATE charts SET
|
|
title = COALESCE($1, title),
|
|
pos_x = COALESCE($2, pos_x),
|
|
pos_y = COALESCE($3, pos_y),
|
|
width = COALESCE($4, width),
|
|
height = COALESCE($5, height),
|
|
signal_type = COALESCE($6, signal_type),
|
|
chart_type = COALESCE($7, chart_type),
|
|
viz_type = COALESCE($8, viz_type),
|
|
filter_combinators = CASE WHEN $9::boolean THEN $10::text[] ELSE filter_combinators END,
|
|
filter_items = CASE WHEN $11::boolean THEN $12::text[] ELSE filter_items END,
|
|
filter_items_exclude = CASE WHEN $13::boolean THEN $14::text[] ELSE filter_items_exclude END,
|
|
filter_items_regex = COALESCE($15, filter_items_regex),
|
|
y_min = CASE WHEN $16::boolean THEN $17::double precision ELSE y_min END,
|
|
y_max = CASE WHEN $18::boolean THEN $19::double precision ELSE y_max END,
|
|
y_scale = COALESCE($20, y_scale),
|
|
series_limit = COALESCE($21, series_limit),
|
|
order_by = COALESCE($22, order_by)
|
|
WHERE id = $23
|
|
RETURNING *`,
|
|
[
|
|
title, pos_x, pos_y, width, height, signal_type, chart_type, viz_type,
|
|
hasFilterCombinators, body.filter_combinators ?? null,
|
|
hasFilterItems, body.filter_items ?? null,
|
|
hasFilterItemsExclude, body.filter_items_exclude ?? null,
|
|
filter_items_regex,
|
|
hasYMin, body.y_min ?? null,
|
|
hasYMax, body.y_max ?? null,
|
|
y_scale,
|
|
series_limit, order_by,
|
|
id,
|
|
],
|
|
);
|
|
if (result.rowCount === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
|
return NextResponse.json(result.rows[0]);
|
|
});
|
|
|
|
export const DELETE = withAuth(async (_req: NextRequest, { params }) => {
|
|
const { id } = await params;
|
|
const result = await pool.query('DELETE FROM charts WHERE id = $1', [id]);
|
|
if (result.rowCount === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
|
return NextResponse.json({ ok: true });
|
|
}); |