diff --git a/api/main.ts b/api/main.ts index cfc7b2a..b3a7409 100644 --- a/api/main.ts +++ b/api/main.ts @@ -12,6 +12,12 @@ import { Router, } from "https://deno.land/x/oak@v17.1.2/mod.ts"; import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts"; +import { + dirname, + fromFileUrl, + join, +} from "https://deno.land/std@0.224.0/path/mod.ts"; + import * as db_utils from "../database/utils.ts"; import * as helper_utils from "./helpers.ts"; @@ -48,6 +54,9 @@ type ApiResponse = { body: unknown; }; +// database creation if missing, runs here because this is the main file executed by the API. +db_utils.ensureDatabaseExists(); + // +++ ROUTER ------------------------------------------------------- // // Creates the routes for the API server. // Example: localhost:8000/api will show "testAPIPoint" diff --git a/database/create_db.ts b/database/create_db.ts index 8577703..c3981dc 100644 --- a/database/create_db.ts +++ b/database/create_db.ts @@ -55,7 +55,7 @@ export function createDatabase(): void { date_created_at TEXT, text TEXT, likes INTEGER - ); -- Added semicolon here + ); CREATE TABLE IF NOT EXISTS messages ( message_id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -105,6 +105,3 @@ export function insertSampleData(): void { `, ); } - -createDatabase(); -insertSampleData(); diff --git a/database/utils.ts b/database/utils.ts index b3e2ce4..c6a26a0 100644 --- a/database/utils.ts +++ b/database/utils.ts @@ -16,41 +16,37 @@ import * as db_create from "./create_db.ts"; // Import all internal utilities with renamed imports to avoid naming conflicts import { - // --- Account Functions --- // - getAllUsersFromDB as getAllUsersFromDBInternal, - getUserByUsername as getUserByUsernameInternal, - // getAllUserInfoByID as getAllUserInfoByIDInternal, - // Accidentally deleted function... - registerUser as registerUserInternal, - - // --- Post Functions --- // - getPostsFromDB as getPostsFromDBInternal, - getPostById as getPostByIdInternal, - createPost as createPostInternal, - updatePost as updatePostInternal, - deletePost as deletePostInternal, - likePost as likePostInternal, - filterPosts, - - // --- Comment Functions --- // - getCommentsFromDB as getCommentsFromDBInternal, + addMessageToChat as addMessageToChatInternal, + createChat as createChatInternal, // getCommentsForComments as getCommentsForCommentsInternal, // Accidentally deleted function... createComment as createCommentInternal, - updateComment as updateCommentInternal, + createPost as createPostInternal, + deleteChat as deleteChatInternal, deleteComment as deleteCommentInternal, - likeComment as likeCommentInternal, - - // --- Chat Functions --- // - getUserChats as getUserChatsInternal, + deletePost as deletePostInternal, + filterPosts, + // --- Account Functions --- // + getAllUsersFromDB as getAllUsersFromDBInternal, getChatById as getChatByIdInternal, getChatMessages as getChatMessagesInternal, - createChat as createChatInternal, - addMessageToChat as addMessageToChatInternal, - deleteChat as deleteChatInternal, - + // --- Comment Functions --- // + getCommentsFromDB as getCommentsFromDBInternal, + getPostById as getPostByIdInternal, + // --- Post Functions --- // + getPostsFromDB as getPostsFromDBInternal, + getUserByUsername as getUserByUsernameInternal, + // --- Chat Functions --- // + getUserChats as getUserChatsInternal, + likeComment as likeCommentInternal, + likePost as likePostInternal, // --- Mapper Functions --- // queryDatabase as queryDatabaseInternal, + // getAllUserInfoByID as getAllUserInfoByIDInternal, + // Accidentally deleted function... + registerUser as registerUserInternal, + updateComment as updateCommentInternal, + updatePost as updatePostInternal, } from "./helpers/utils/mod.ts"; // +++ VARIABLES ---------------------------------------------------- // @@ -64,11 +60,11 @@ const db = new DB(dbPath); // +++ INTERFACES --------------------------------------------------- // // Only re-export interfaces that are needed by external code export type { - Post, Accounts, - Comments, Chat, + Comments, Message, + Post, } from "./helpers/interfaces.ts"; // +++ HELPER FUNCTIONS --------------------------------------------- // export function queryDatabase( @@ -80,6 +76,42 @@ export function queryDatabase( } // +++ DATABASE INITIALIZATION -------------------------------------- // +export async function ensureDatabaseExists(): Promise { + try { + // Check if the database directory exists, create it if not + const dbDir = dirname(dbPath); + try { + await Deno.stat(dbDir); + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + // Create the database directory + await Deno.mkdir(dbDir, { recursive: true }); + console.log(`Created database directory: ${dbDir}`); + } else { + throw error; + } + } + + // Check if the database file exists + try { + await Deno.stat(dbPath); + console.log("Database file already exists"); + } catch (error) { + if (error instanceof Deno.errors.NotFound) { + // Nothing, file will be created below + } else { + throw error; + } + } + + createDatabaseIfNotExist(); + insertSamples(); + } catch (error) { + console.error("Error ensuring database exists:", error); + throw error; + } +} + export function createDatabaseIfNotExist(): void { db_create.createDatabase(); }