Web app ready
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import db from '@/lib/db';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
type Params = { params: Promise<{ id: string }> };
|
||||
|
||||
export async function POST(_req: Request, { params }: Params) {
|
||||
const { id } = await params;
|
||||
const tx = db.transaction(() => {
|
||||
const row = db.prepare('UPDATE counters SET value = value - 1 WHERE id = ? AND value > 0 RETURNING *').get(Number(id));
|
||||
if (row) db.prepare('INSERT INTO events (counter_id, delta, created_at) VALUES (?, -1, ?)').run(Number(id), Date.now());
|
||||
return row;
|
||||
});
|
||||
const result = tx();
|
||||
if (!result) return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
||||
return NextResponse.json(result);
|
||||
}
|
||||
Reference in New Issue
Block a user