Initial web
This commit is contained in:
30
web/app/api/alerts/route.ts
Normal file
30
web/app/api/alerts/route.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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 alerts ORDER BY created_at DESC');
|
||||
return NextResponse.json(result.rows);
|
||||
});
|
||||
|
||||
export const POST = withAuth(async (req: NextRequest) => {
|
||||
const body = await req.json();
|
||||
const {
|
||||
item_key, item_key_is_regex = false,
|
||||
combinator = null, signal_type = 'green', condition, threshold,
|
||||
} = body;
|
||||
|
||||
if (!item_key || !condition || threshold === undefined) {
|
||||
return NextResponse.json(
|
||||
{ error: 'item_key, condition, threshold required' },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
|
||||
const result = await pool.query(
|
||||
`INSERT INTO alerts (item_key, item_key_is_regex, combinator, signal_type, condition, threshold)
|
||||
VALUES ($1,$2,$3,$4,$5,$6) RETURNING *`,
|
||||
[item_key, item_key_is_regex, combinator, signal_type, condition, threshold],
|
||||
);
|
||||
return NextResponse.json(result.rows[0], { status: 201 });
|
||||
});
|
||||
Reference in New Issue
Block a user