Fixed a few API bugs

This commit is contained in:
MattLeo 2025-11-25 09:11:29 -06:00
parent ecbb6f8f30
commit cddde946e8

View File

@ -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()) {