From cddde946e8790abc16b60d14e8e414f6a590aa31 Mon Sep 17 00:00:00 2001 From: MattLeo Date: Tue, 25 Nov 2025 09:11:29 -0600 Subject: [PATCH] Fixed a few API bugs --- server/db.js | 24 +++--------------------- 1 file changed, 3 insertions(+), 21 deletions(-) diff --git a/server/db.js b/server/db.js index 34d408a..02455b3 100644 --- a/server/db.js +++ b/server/db.js @@ -19,20 +19,6 @@ function getNextKANumber() { return 'KA001'; } -function getArtcileById(id) { - const stmt = db.prepare("SELECT * FROM articles WHERE id = ?"); - stmt.bind([id]); - - if (stmt.step()) { - const article = stmt.getAsObject(); - stmt.free(); - return article; - } - - stmt.free(); - return null; -} - async function initDb() { const SQL = await initSqlJs(); @@ -40,7 +26,7 @@ async function initDb() { // Loading Database if it already exists if (fs.existsSync(DB_PATH)) { - const buffer = fs.readFilySync(DB_PATH); + const buffer = fs.readFileSync(DB_PATH); db = new SQL.Database(buffer); console.log('Database Loaded') } else { @@ -116,7 +102,7 @@ function createArticle(title, content) { const ka_num = getNextKANumber(); db.run( - "INSERT INTO artciles (ka_number, title, content) VALUES (?, ?, ?)", + "INSERT INTO articles (ka_number, title, content) VALUES (?, ?, ?)", [ka_num, title, content] ); @@ -124,10 +110,7 @@ function createArticle(title, content) { const data = db.export(); fs.writeFileSync(DB_PATH, Buffer.from(data)); - // Getting id and returning the newly created artcle - const result = db.exec("SELECT last_insert_rowId()"); - const id = result[0].values[0][0]; - return getArtcileById(id); + return getArticle(ka_num); } function updateArticle(ka_num, title, content) { @@ -162,7 +145,6 @@ function searchArticles(query) { "SELECT * FROM articles WHERE title LIKE ? OR content LIKE ? or ka_number LIKE ? ORDER BY created_at DESC" ); stmt.bind([searchTerm, searchTerm, searchTerm]); - stmt.free(); const results = []; while (stmt.step()) {