Initial web
This commit is contained in:
64
web/app/api/charts/[id]/route.ts
Normal file
64
web/app/api/charts/[id]/route.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
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 });
|
||||
});
|
||||
36
web/app/api/charts/route.ts
Normal file
36
web/app/api/charts/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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 });
|
||||
});
|
||||
Reference in New Issue
Block a user