Now returns userId upon Register

This commit is contained in:
Lynixenn
2025-03-24 21:21:45 +01:00
parent 029fc10de7
commit 9e7039c72c
4 changed files with 82 additions and 55 deletions

View File

@@ -46,9 +46,10 @@ async function api_createPost(ctx: Context): Promise<void> {
} }
// Create timestamp in the format expected by the database // Create timestamp in the format expected by the database
const createdAt = `${Math.floor(Date.now() / 1000)}-${ const createdAt = `${Math.floor(Date.now() / 1000)}-${new Date()
new Date().toLocaleDateString("en-GB").split("/").join("-") .toLocaleDateString("en-GB")
}`; .split("/")
.join("-")}`;
const postId = await db_utils.createPost( const postId = await db_utils.createPost(
userId, userId,

View File

@@ -6,7 +6,6 @@
* @file postUtil.ts * @file postUtil.ts
*/ */
// +++ IMPORTS ------------------------------------------------------ // // +++ IMPORTS ------------------------------------------------------ //
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts"; import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
import { Post } from "../interfaces.ts"; import { Post } from "../interfaces.ts";
@@ -20,13 +19,19 @@ async function getPostsFromDB(db: DB, user_uuid?: string): Promise<Post[]> {
return await queryDatabase<Post>(db, query, params, mapPostRow); return await queryDatabase<Post>(db, query, params, mapPostRow);
} }
async function getPostById(db: DB, postId: string): Promise<Post | null> { async function getPostById(db: DB, postId: string): Promise<Post | null> {
const query = `SELECT * FROM posts WHERE posts_uuid = ?`; const query = `SELECT * FROM posts WHERE posts_uuid = ?`;
const posts = await queryDatabase<Post>(db, query, [postId], mapPostRow); const posts = await queryDatabase<Post>(db, query, [postId], mapPostRow);
return posts.length > 0 ? posts[0] : null; return posts.length > 0 ? posts[0] : null;
} }
async function createPost(db: DB, userId: string, createdAt: string, postText: string, postType: string): Promise<string> { async function createPost(
db: DB,
userId: string,
createdAt: string,
postText: string,
postType: string,
): Promise<string> {
const query = ` const query = `
INSERT INTO posts (user_id, created_at, post_text, post_type, likes, comments) INSERT INTO posts (user_id, created_at, post_text, post_type, likes, comments)
VALUES (?, ?, ?, ?, 0, 0) VALUES (?, ?, ?, ?, 0, 0)
@@ -36,7 +41,12 @@ async function createPost(db: DB, userId: string, createdAt: string, postText:
return db.lastInsertRowId.toString(); return db.lastInsertRowId.toString();
} }
async function updatePost(db: DB, postId: string, postText?: string, postType?: string): Promise<void> { async function updatePost(
db: DB,
postId: string,
postText?: string,
postType?: string,
): Promise<void> {
let query = `UPDATE posts SET `; let query = `UPDATE posts SET `;
const params: any[] = []; const params: any[] = [];
@@ -59,19 +69,19 @@ async function updatePost(db: DB, postId: string, postText?: string, postType?:
db.query(query, params); 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<void> { async function deletePost(db: DB, postId: string): Promise<void> {
// First delete all comments on the post
const deleteCommentsQuery = `DELETE FROM comments WHERE post_id = ?`; const deleteCommentsQuery = `DELETE FROM comments WHERE post_id = ?`;
db.query(deleteCommentsQuery, [postId]); db.query(deleteCommentsQuery, [postId]);
// Then delete the post itself
const deletePostQuery = `DELETE FROM posts WHERE posts_uuid = ?`; const deletePostQuery = `DELETE FROM posts WHERE posts_uuid = ?`;
db.query(deletePostQuery, [postId]); 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<void> { async function likePost(db: DB, postId: string, userId: string): Promise<void> {
// 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 = ?`; const query = `UPDATE posts SET likes = likes + 1 WHERE posts_uuid = ?`;
db.query(query, [postId]); db.query(query, [postId]);
} }

View File

@@ -29,8 +29,7 @@ function registerUser(
surname: string, surname: string,
account_created: string, account_created: string,
): string { ): string {
const query_user_exists = const query_user_exists = `SELECT * FROM accounts WHERE user_username = '${user}'`;
`SELECT * FROM accounts WHERE user_username = '${user}'`;
if (!query_user_exists) { if (!query_user_exists) {
return "noUser"; return "noUser";
} }
@@ -68,9 +67,12 @@ function registerUser(
'[]' '[]'
)`; )`;
db.query(query_add_user); db.query(query_add_user);
let userId = db.query(
`SELECT user_id FROM accounts WHERE user_username = '${user}'`,
);
console.log(`New user: ${user}`); console.log(`New user: ${user}`);
return "newUser"; return userId;
} }
/** /**
@@ -88,7 +90,12 @@ async function getAllUsersFromDB(db: DB): Promise<Accounts[]> {
async function getUserByUsername(db: DB, username: string): Promise<Accounts> { async function getUserByUsername(db: DB, username: string): Promise<Accounts> {
const query = `SELECT * FROM accounts WHERE username = '${username}'`; const query = `SELECT * FROM accounts WHERE username = '${username}'`;
const params: string[] = []; const params: string[] = [];
const result = await queryDatabase<Accounts>(db, query, params, mapAccountRow); const result = await queryDatabase<Accounts>(
db,
query,
params,
mapAccountRow,
);
return result[0]; return result[0];
} }

View File

@@ -1,8 +1,9 @@
/// <reference lib="deno.ns" />
/** /**
* @author Esad Mustafoski * @author Esad Mustafoski
* @description This file is responsible for creating Functions to easily access the Database, Intended for use in the API * @description This file is responsible for creating Functions to easily access the Database, Intended for use in the API
*/ */
/// <reference lib="deno.ns" />
// +++ IMPORTS ------------------------------------------------------ // // +++ IMPORTS ------------------------------------------------------ //
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts"; import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
@@ -89,7 +90,8 @@ export function insertSamples(): void {
// +++ ACCOUNT FUNCTIONS -------------------------------------------- // // +++ ACCOUNT FUNCTIONS -------------------------------------------- //
export const getAllUsersFromDB = () => getAllUsersFromDBInternal(db); 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 getAllUserInfoByID = (user_id: string) => getAllUserInfoByIDInternal(db, user_id);
export const registerUser = ( export const registerUser = (
user: string, user: string,
@@ -101,21 +103,23 @@ export const registerUser = (
firstname: string, firstname: string,
surname: string, surname: string,
account_created: string, account_created: string,
) => registerUserInternal( ) =>
db, registerUserInternal(
user, db,
password, user,
salt, password,
userGroup, salt,
displayname, userGroup,
user_email, displayname,
firstname, user_email,
surname, firstname,
account_created, surname,
); account_created,
);
// +++ POST FUNCTIONS ----------------------------------------------- // // +++ 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 getPostById = (postId: string) => getPostByIdInternal(db, postId);
export const createPost = ( export const createPost = (
userId: string, userId: string,
@@ -129,10 +133,12 @@ export const updatePost = (
postType?: string, postType?: string,
) => updatePostInternal(db, postId, postText, postType); ) => updatePostInternal(db, postId, postText, postType);
export const deletePost = (postId: string) => deletePostInternal(db, postId); 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 -------------------------------------------- // // +++ 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 getCommentsForComments = (comment_id: number) => getCommentsForCommentsInternal(db, comment_id);
export const createComment = ( export const createComment = (
postId: string, postId: string,
@@ -140,18 +146,21 @@ export const createComment = (
createdAt: string, createdAt: string,
text: string, text: string,
) => createCommentInternal(db, postId, userId, createdAt, text); ) => createCommentInternal(db, postId, userId, createdAt, text);
export const updateComment = (commentId: string, text: string) => updateCommentInternal(db, commentId, text); export const updateComment = (commentId: string, text: string) =>
export const deleteComment = (commentId: string) => deleteCommentInternal(db, commentId); updateCommentInternal(db, commentId, text);
export const likeComment = (commentId: string, userId: string) => likeCommentInternal(db, commentId, userId); export const deleteComment = (commentId: string) =>
deleteCommentInternal(db, commentId);
export const likeComment = (commentId: string, userId: string) =>
likeCommentInternal(db, commentId, userId);
// +++ CHAT FUNCTIONS ----------------------------------------------- // // +++ 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 getChatById = (chatId: string) => getChatByIdInternal(db, chatId);
export const getChatMessages = (chatId: string) => getChatMessagesInternal(db, chatId); export const getChatMessages = (chatId: string) =>
export const createChat = ( getChatMessagesInternal(db, chatId);
participants: string[], export const createChat = (participants: string[], chatName: string) =>
chatName: string, createChatInternal(db, participants, chatName);
) => createChatInternal(db, participants, chatName);
export const addMessageToChat = ( export const addMessageToChat = (
chatId: string, chatId: string,
senderId: string, senderId: string,