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

@@ -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<Post[]> {
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 posts = await queryDatabase<Post>(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<string> {
async function createPost(
db: DB,
userId: string,
createdAt: string,
postText: string,
postType: string,
): Promise<string> {
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<void> {
async function updatePost(
db: DB,
postId: string,
postText?: string,
postType?: string,
): Promise<void> {
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<void> {
// 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<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 = ?`;
db.query(query, [postId]);
}
@@ -115,4 +125,4 @@ export {
deletePost,
likePost,
filterPosts,
};
};

View File

@@ -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<Accounts[]> {
async function getUserByUsername(db: DB, username: string): Promise<Accounts> {
const query = `SELECT * FROM accounts WHERE username = '${username}'`;
const params: string[] = [];
const result = await queryDatabase<Accounts>(db, query, params, mapAccountRow);
const result = await queryDatabase<Accounts>(
db,
query,
params,
mapAccountRow,
);
return result[0];
}
export { registerUser, getAllUsersFromDB, getUserByUsername };
export { registerUser, getAllUsersFromDB, getUserByUsername };