Files
factorio-signal-exporter/web/app/api/alerts/[id]/route.ts
Caesar2011 20ed6ee9fb Initial web
2026-05-17 19:55:53 +02:00

32 lines
1.5 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 { item_key, item_key_is_regex, combinator, signal_type, condition, threshold, active } = body;
const result = await pool.query(
`UPDATE alerts SET
item_key = COALESCE($1, item_key),
item_key_is_regex = COALESCE($2, item_key_is_regex),
combinator = $3,
signal_type = COALESCE($4, signal_type),
condition = COALESCE($5, condition),
threshold = COALESCE($6, threshold),
active = COALESCE($7, active)
WHERE id = $8
RETURNING *`,
[item_key, item_key_is_regex, combinator, signal_type, condition, threshold, active, 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 alerts WHERE id = $1', [id]);
if (result.rowCount === 0) return NextResponse.json({ error: 'Not found' }, { status: 404 });
return NextResponse.json({ ok: true });
});