Fix Error in create_db

This commit is contained in:
Esad Mustafoski
2025-03-21 08:32:28 +01:00
parent 8b594990ee
commit 029fc10de7
3 changed files with 60 additions and 41 deletions

View File

@@ -16,29 +16,26 @@ import * as db_utils from "../database/utils.ts";
import * as helper_utils from "./helpers.ts"; import * as helper_utils from "./helpers.ts";
import { import {
// --- Post --- //
api_getPostById,
api_createPost,
api_updatePost,
api_deletePost,
api_likePost,
// --- Comment --- //
api_getPostComments,
api_createComment,
api_updateComment,
api_deleteComment,
api_likeComment,
// --- User --- //
api_getAllUsers,
// --- Chat --- // // --- Chat --- //
api_createChat, api_createChat,
api_createComment,
api_createPost,
api_deleteChat, api_deleteChat,
api_deleteComment,
api_deletePost,
// --- User --- //
api_getAllUsers,
api_getChatById, api_getChatById,
api_getChats, api_getChats,
// --- Post --- //
api_getPostById,
// --- Comment --- //
api_getPostComments,
api_likeComment,
api_likePost,
api_sendMessage, api_sendMessage,
api_updateComment,
api_updatePost,
} from "./helpers/mod.ts"; } from "./helpers/mod.ts";
// +++ VARIABLES / TYPES --------------------------------------------- // // +++ VARIABLES / TYPES --------------------------------------------- //
@@ -86,8 +83,8 @@ router
// -- User routes -- // // -- User routes -- //
router router
.get("/api/users", api_getAllUsers) .get("/api/users", api_getAllUsers);
// .get("/api/user/:id/info", api_user_getInfo); // .get("/api/user/:id/info", api_user_getInfo);
// -- Chat routes -- // // -- Chat routes -- //
router router
@@ -114,7 +111,6 @@ router
.delete("/api/comment/:id", api_deleteComment) .delete("/api/comment/:id", api_deleteComment)
.post("/api/comment/:id/like", api_likeComment); .post("/api/comment/:id/like", api_likeComment);
// +++ FUNCTIONS ----------------------------------------------------- // // +++ FUNCTIONS ----------------------------------------------------- //
// ABANDONED FUNCTIONS // // ABANDONED FUNCTIONS //
@@ -196,7 +192,7 @@ async function api_register(ctx: Context): Promise<void> {
// Then hash the salted password // Then hash the salted password
const hash = await helper_utils.hashPassword(saltedPassword); const hash = await helper_utils.hashPassword(saltedPassword);
const userId = await db_utils.registerUser( const userId = db_utils.registerUser(
username, username,
hash, hash,
salt, salt,

View File

@@ -6,8 +6,12 @@
*/ */
// +++ IMPORTS ------------------------------------------------------ // // +++ IMPORTS ------------------------------------------------------ //
import { DB } from "https://deno.land/x/sqlite/mod.ts"; import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
import { dirname, fromFileUrl, join } from "https://deno.land/std/path/mod.ts"; import {
dirname,
fromFileUrl,
join,
} from "https://deno.land/std@0.224.0/path/mod.ts";
// +++ VARIABLES ---------------------------------------------------- // // +++ VARIABLES ---------------------------------------------------- //
const _dirname: string = dirname(fromFileUrl(import.meta.url)); const _dirname: string = dirname(fromFileUrl(import.meta.url));
@@ -51,7 +55,7 @@ export function createDatabase(): void {
date_created_at TEXT, date_created_at TEXT,
text TEXT, text TEXT,
likes INTEGER likes INTEGER
) ); -- Added semicolon here
CREATE TABLE IF NOT EXISTS messages ( CREATE TABLE IF NOT EXISTS messages (
message_id INTEGER PRIMARY KEY AUTOINCREMENT, message_id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -61,7 +65,7 @@ export function createDatabase(): void {
timestamp TEXT, timestamp TEXT,
FOREIGN KEY (chat_id) REFERENCES chats (chat_id), FOREIGN KEY (chat_id) REFERENCES chats (chat_id),
FOREIGN KEY (sender_id) REFERENCES accounts (user_id) FOREIGN KEY (sender_id) REFERENCES accounts (user_id)
) );
CREATE TABLE IF NOT EXISTS chats ( CREATE TABLE IF NOT EXISTS chats (
chat_id INTEGER PRIMARY KEY AUTOINCREMENT, chat_id INTEGER PRIMARY KEY AUTOINCREMENT,

View File

@@ -10,7 +10,10 @@ import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
import { Comments } from "../interfaces.ts"; import { Comments } from "../interfaces.ts";
import { mapCommentRow, queryDatabase } from "./mod.ts"; import { mapCommentRow, queryDatabase } from "./mod.ts";
async function getCommentsFromDB(db: DB, post_id?: number): Promise<Comments[]> { async function getCommentsFromDB(
db: DB,
post_id?: number,
): Promise<Comments[]> {
const query = post_id const query = post_id
? `SELECT * FROM comments WHERE post_id = ?` ? `SELECT * FROM comments WHERE post_id = ?`
: `SELECT * FROM comments`; : `SELECT * FROM comments`;
@@ -18,20 +21,31 @@ async function getCommentsFromDB(db: DB, post_id?: number): Promise<Comments[]>
return await queryDatabase<Comments>(db, query, params, mapCommentRow); return await queryDatabase<Comments>(db, query, params, mapCommentRow);
} }
async function createComment(db: DB, postId: string, userId: string, createdAt: string, text: string): Promise<string> { function createComment(
db: DB,
postId: string,
userId: string,
createdAt: string,
text: string,
): string {
const query = ` const query = `
INSERT INTO comments (post_id, author_user_id, date_created_at, text, likes) INSERT INTO comments (post_id, author_user_id, date_created_at, text, likes)
VALUES (?, ?, ?, ?, 0) VALUES (?, ?, ?, ?, 0)
`; `;
db.query(query, [postId, userId, createdAt, text]); db.query(query, [postId, userId, createdAt, text]);
const updatePostQuery = `UPDATE posts SET comments = comments + 1 WHERE posts_uuid = ?`; const updatePostQuery =
`UPDATE posts SET comments = comments + 1 WHERE posts_uuid = ?`;
db.query(updatePostQuery, [postId]); db.query(updatePostQuery, [postId]);
return db.lastInsertRowId.toString(); return db.lastInsertRowId.toString();
} }
async function updateComment(db: DB, commentId: string, text: string): Promise<void> { async function updateComment(
db: DB,
commentId: string,
text: string,
): Promise<void> {
const query = `UPDATE comments SET text = ? WHERE comment_id = ?`; const query = `UPDATE comments SET text = ? WHERE comment_id = ?`;
db.query(query, [text, commentId]); db.query(query, [text, commentId]);
} }
@@ -44,19 +58,24 @@ async function deleteComment(db: DB, commentId: string): Promise<void> {
const deleteCommentQuery = `DELETE FROM comments WHERE comment_id = ?`; const deleteCommentQuery = `DELETE FROM comments WHERE comment_id = ?`;
db.query(deleteCommentQuery, [commentId]); db.query(deleteCommentQuery, [commentId]);
const updatePostQuery = `UPDATE posts SET comments = comments - 1 WHERE posts_uuid = ?`; const updatePostQuery =
`UPDATE posts SET comments = comments - 1 WHERE posts_uuid = ?`;
db.query(updatePostQuery, [postId]); db.query(updatePostQuery, [postId]);
} }
async function likeComment(db: DB, commentId: string, userId: string): Promise<void> { async function likeComment(
db: DB,
commentId: string,
userId: string,
): Promise<void> {
const query = `UPDATE comments SET likes = likes + 1 WHERE comment_id = ?`; const query = `UPDATE comments SET likes = likes + 1 WHERE comment_id = ?`;
db.query(query, [commentId]); db.query(query, [commentId]);
} }
export { export {
createComment, createComment,
updateComment,
deleteComment, deleteComment,
likeComment,
getCommentsFromDB, getCommentsFromDB,
} likeComment,
updateComment,
};