Kemudian buatlah sebuah api/to-do-list/route.js
jangan lupa untuk import connection databasenya dari folder lib/db
import connection from "@/lib/db";
export async function GET(req) {
const rows = await connection.query("SELECT * FROM todo");
return new Response(JSON.stringify(rows[0]));
}
export async function POST(req) {
const todo = Object.values(await req.json());
const sql =
todo.length < 3
? "INSERT INTO todo (to_list, do_list) VALUES (?,?)"
: "UPDATE todo SET to_list=?, do_list=? WHERE id=?";
try {
const result = await connection.query(sql, todo);
return new Response({ status: 200 });
} catch (error) {
return new Response({ status: 400 });
}
}
export async function DELETE(req) {
try {
const { id } = await req.json();
const result = await connection.query("DELETE FROM todo WHERE id=?", [id]);
return new Response({ status: 200 });
} catch (error) {
return new Response({ status: 400 });
}
}