From 9e7039c72ccb549a651bd86997610d4adde1c787 Mon Sep 17 00:00:00 2001 From: Lynixenn Date: Mon, 24 Mar 2025 21:21:45 +0100 Subject: [PATCH] Now returns userId upon Register --- api/helpers/post_api.ts | 9 ++-- database/helpers/utils/post_utils.ts | 40 ++++++++++------ database/helpers/utils/user_utils.ts | 17 +++++-- database/utils.ts | 71 ++++++++++++++++------------ 4 files changed, 82 insertions(+), 55 deletions(-) diff --git a/api/helpers/post_api.ts b/api/helpers/post_api.ts index 48a251a..7154b15 100644 --- a/api/helpers/post_api.ts +++ b/api/helpers/post_api.ts @@ -46,9 +46,10 @@ async function api_createPost(ctx: Context): Promise { } // Create timestamp in the format expected by the database - const createdAt = `${Math.floor(Date.now() / 1000)}-${ - new Date().toLocaleDateString("en-GB").split("/").join("-") - }`; + const createdAt = `${Math.floor(Date.now() / 1000)}-${new Date() + .toLocaleDateString("en-GB") + .split("/") + .join("-")}`; const postId = await db_utils.createPost( userId, @@ -143,4 +144,4 @@ export { api_getPostById, api_likePost, api_updatePost, -}; \ No newline at end of file +}; diff --git a/database/helpers/utils/post_utils.ts b/database/helpers/utils/post_utils.ts index 7c88413..dadb760 100644 --- a/database/helpers/utils/post_utils.ts +++ b/database/helpers/utils/post_utils.ts @@ -6,7 +6,6 @@ * @file postUtil.ts */ - // +++ IMPORTS ------------------------------------------------------ // import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts"; import { Post } from "../interfaces.ts"; @@ -20,30 +19,41 @@ async function getPostsFromDB(db: DB, user_uuid?: string): Promise { return await queryDatabase(db, query, params, mapPostRow); } -async function getPostById(db: DB, postId: string): Promise { +async function getPostById(db: DB, postId: string): Promise { const query = `SELECT * FROM posts WHERE posts_uuid = ?`; const posts = await queryDatabase(db, query, [postId], mapPostRow); return posts.length > 0 ? posts[0] : null; } -async function createPost(db: DB, userId: string, createdAt: string, postText: string, postType: string): Promise { +async function createPost( + db: DB, + userId: string, + createdAt: string, + postText: string, + postType: string, +): Promise { const query = ` INSERT INTO posts (user_id, created_at, post_text, post_type, likes, comments) VALUES (?, ?, ?, ?, 0, 0) `; - + db.query(query, [userId, createdAt, postText, postType]); return db.lastInsertRowId.toString(); } -async function updatePost(db: DB, postId: string, postText?: string, postType?: string): Promise { +async function updatePost( + db: DB, + postId: string, + postText?: string, + postType?: string, +): Promise { let query = `UPDATE posts SET `; const params: any[] = []; - + if (postText) { query += `post_text = ?`; params.push(postText); - + if (postType) { query += `, post_type = ?`; params.push(postType); @@ -52,26 +62,26 @@ async function updatePost(db: DB, postId: string, postText?: string, postType?: query += `post_type = ?`; params.push(postType); } - + query += ` WHERE posts_uuid = ?`; params.push(postId); - + db.query(query, params); } +// This function deletes the comments on the post first, then +// deletes the post to avoid errors async function deletePost(db: DB, postId: string): Promise { - // First delete all comments on the post const deleteCommentsQuery = `DELETE FROM comments WHERE post_id = ?`; db.query(deleteCommentsQuery, [postId]); - - // Then delete the post itself + const deletePostQuery = `DELETE FROM posts WHERE posts_uuid = ?`; db.query(deletePostQuery, [postId]); } +// This is simplified and doesn't work as it would in a real application +// or website like twitter, this only exists as a test async function likePost(db: DB, postId: string, userId: string): Promise { - // In a real application, you would check if the user has already liked the post - // and store like relationships in a separate table. This is a simplified version. const query = `UPDATE posts SET likes = likes + 1 WHERE posts_uuid = ?`; db.query(query, [postId]); } @@ -115,4 +125,4 @@ export { deletePost, likePost, filterPosts, -}; \ No newline at end of file +}; diff --git a/database/helpers/utils/user_utils.ts b/database/helpers/utils/user_utils.ts index e1d6ed9..c638649 100644 --- a/database/helpers/utils/user_utils.ts +++ b/database/helpers/utils/user_utils.ts @@ -29,8 +29,7 @@ function registerUser( surname: string, account_created: string, ): string { - const query_user_exists = - `SELECT * FROM accounts WHERE user_username = '${user}'`; + const query_user_exists = `SELECT * FROM accounts WHERE user_username = '${user}'`; if (!query_user_exists) { return "noUser"; } @@ -68,9 +67,12 @@ function registerUser( '[]' )`; db.query(query_add_user); + let userId = db.query( + `SELECT user_id FROM accounts WHERE user_username = '${user}'`, + ); console.log(`New user: ${user}`); - return "newUser"; + return userId; } /** @@ -88,8 +90,13 @@ async function getAllUsersFromDB(db: DB): Promise { async function getUserByUsername(db: DB, username: string): Promise { const query = `SELECT * FROM accounts WHERE username = '${username}'`; const params: string[] = []; - const result = await queryDatabase(db, query, params, mapAccountRow); + const result = await queryDatabase( + db, + query, + params, + mapAccountRow, + ); return result[0]; } -export { registerUser, getAllUsersFromDB, getUserByUsername }; \ No newline at end of file +export { registerUser, getAllUsersFromDB, getUserByUsername }; diff --git a/database/utils.ts b/database/utils.ts index 52e12ab..b3e2ce4 100644 --- a/database/utils.ts +++ b/database/utils.ts @@ -1,8 +1,9 @@ +/// + /** * @author Esad Mustafoski * @description This file is responsible for creating Functions to easily access the Database, Intended for use in the API */ -/// // +++ IMPORTS ------------------------------------------------------ // import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts"; @@ -21,7 +22,7 @@ import { // getAllUserInfoByID as getAllUserInfoByIDInternal, // Accidentally deleted function... registerUser as registerUserInternal, - + // --- Post Functions --- // getPostsFromDB as getPostsFromDBInternal, getPostById as getPostByIdInternal, @@ -30,7 +31,7 @@ import { deletePost as deletePostInternal, likePost as likePostInternal, filterPosts, - + // --- Comment Functions --- // getCommentsFromDB as getCommentsFromDBInternal, // getCommentsForComments as getCommentsForCommentsInternal, @@ -39,7 +40,7 @@ import { updateComment as updateCommentInternal, deleteComment as deleteCommentInternal, likeComment as likeCommentInternal, - + // --- Chat Functions --- // getUserChats as getUserChatsInternal, getChatById as getChatByIdInternal, @@ -47,7 +48,7 @@ import { createChat as createChatInternal, addMessageToChat as addMessageToChatInternal, deleteChat as deleteChatInternal, - + // --- Mapper Functions --- // queryDatabase as queryDatabaseInternal, } from "./helpers/utils/mod.ts"; @@ -89,7 +90,8 @@ export function insertSamples(): void { // +++ ACCOUNT FUNCTIONS -------------------------------------------- // export const getAllUsersFromDB = () => getAllUsersFromDBInternal(db); -export const getUserByUsername = (username: string) => getUserByUsernameInternal(db, username); +export const getUserByUsername = (username: string) => + getUserByUsernameInternal(db, username); // export const getAllUserInfoByID = (user_id: string) => getAllUserInfoByIDInternal(db, user_id); export const registerUser = ( user: string, @@ -101,21 +103,23 @@ export const registerUser = ( firstname: string, surname: string, account_created: string, -) => registerUserInternal( - db, - user, - password, - salt, - userGroup, - displayname, - user_email, - firstname, - surname, - account_created, -); +) => + registerUserInternal( + db, + user, + password, + salt, + userGroup, + displayname, + user_email, + firstname, + surname, + account_created, + ); // +++ POST FUNCTIONS ----------------------------------------------- // -export const getPostsFromDB = (user_uuid?: string) => getPostsFromDBInternal(db, user_uuid); +export const getPostsFromDB = (user_uuid?: string) => + getPostsFromDBInternal(db, user_uuid); export const getPostById = (postId: string) => getPostByIdInternal(db, postId); export const createPost = ( userId: string, @@ -129,10 +133,12 @@ export const updatePost = ( postType?: string, ) => updatePostInternal(db, postId, postText, postType); export const deletePost = (postId: string) => deletePostInternal(db, postId); -export const likePost = (postId: string, userId: string) => likePostInternal(db, postId, userId); +export const likePost = (postId: string, userId: string) => + likePostInternal(db, postId, userId); // +++ COMMENT FUNCTIONS -------------------------------------------- // -export const getCommentsFromDB = (post_id?: number) => getCommentsFromDBInternal(db, post_id); +export const getCommentsFromDB = (post_id?: number) => + getCommentsFromDBInternal(db, post_id); // export const getCommentsForComments = (comment_id: number) => getCommentsForCommentsInternal(db, comment_id); export const createComment = ( postId: string, @@ -140,18 +146,21 @@ export const createComment = ( createdAt: string, text: string, ) => createCommentInternal(db, postId, userId, createdAt, text); -export const updateComment = (commentId: string, text: string) => updateCommentInternal(db, commentId, text); -export const deleteComment = (commentId: string) => deleteCommentInternal(db, commentId); -export const likeComment = (commentId: string, userId: string) => likeCommentInternal(db, commentId, userId); +export const updateComment = (commentId: string, text: string) => + updateCommentInternal(db, commentId, text); +export const deleteComment = (commentId: string) => + deleteCommentInternal(db, commentId); +export const likeComment = (commentId: string, userId: string) => + likeCommentInternal(db, commentId, userId); // +++ CHAT FUNCTIONS ----------------------------------------------- // -export const getUserChats = (userId: string) => getUserChatsInternal(db, userId); +export const getUserChats = (userId: string) => + getUserChatsInternal(db, userId); export const getChatById = (chatId: string) => getChatByIdInternal(db, chatId); -export const getChatMessages = (chatId: string) => getChatMessagesInternal(db, chatId); -export const createChat = ( - participants: string[], - chatName: string, -) => createChatInternal(db, participants, chatName); +export const getChatMessages = (chatId: string) => + getChatMessagesInternal(db, chatId); +export const createChat = (participants: string[], chatName: string) => + createChatInternal(db, participants, chatName); export const addMessageToChat = ( chatId: string, senderId: string, @@ -160,4 +169,4 @@ export const addMessageToChat = ( export const deleteChat = (chatId: string) => deleteChatInternal(db, chatId); // +++ UTILITY FUNCTIONS -------------------------------------------- // -export { filterPosts }; \ No newline at end of file +export { filterPosts };