Changed functions in database/utils.ts

This commit is contained in:
Lynixen
2024-11-06 21:35:41 +01:00
parent 064b864246
commit f45945474d
2 changed files with 44 additions and 12 deletions

View File

@@ -18,19 +18,19 @@ const app = new Application();
// Example: localhost:8000/api will show "testAPIPoint" // Example: localhost:8000/api will show "testAPIPoint"
// in the HTML // in the HTML
router router
.get("/", (ctx) => { .get("/", (ctx):void => {
ctx.response.body = "ESP API Site"; ctx.response.body = "ESP API Site";
}) })
.get("/api", (ctx) => { .get("/api", (ctx):void => {
ctx.response.body = "testAPIPoint"; ctx.response.body = "testAPIPoint";
}) })
.get("/api/users", (ctx) => { .get("/api/users", async (ctx):Promise<void> => {
const getUsers = db_utils.getAllUsersFromDB(); const getUsers = await db_utils.getAllUsersFromDB();
ctx.response.body = getUsers; //getAllUsers(); ctx.response.body = getUsers; //getAllUsers();
}) })
.get("/api/posts", async (ctx) => { .get("/api/posts", async (ctx):Promise<void> => {
const getPosts = await db_utils.getPostsFromDB(); const getPosts = await db_utils.getPostsFromDB();
const countedPosts = await db_utils.countPosts(); const countedPosts:number = await db_utils.countPosts();
ctx.response.body = { getPosts, countedPosts }; ctx.response.body = { getPosts, countedPosts };
}); });

View File

@@ -55,7 +55,6 @@ interface Comments {
likes: number; likes: number;
} }
// +++ FUNCTIONS---------------------------------------------------- // // +++ FUNCTIONS---------------------------------------------------- //
/** /**
@@ -72,7 +71,9 @@ async function getPostsFromDB(user_uuid?: string): Promise<Post[]> {
if (user_uuid === undefined) { if (user_uuid === undefined) {
rows = await db.query(`SELECT * FROM posts`); rows = await db.query(`SELECT * FROM posts`);
} else { } else {
rows = await db.query(`SELECT * FROM posts WHERE user_id = ${user_uuid}`); rows = await db.query(
`SELECT * FROM posts WHERE user_id = ${user_uuid}`,
);
} }
for (const row of rows) { for (const row of rows) {
@@ -178,7 +179,9 @@ async function getCommentsFromDB(post_id?: number): Promise<Comments[]> {
if (post_id === undefined) { if (post_id === undefined) {
rows = await db.query(`SELECT * FROM comments`); rows = await db.query(`SELECT * FROM comments`);
} else { } else {
rows = await db.query(`SELECT * FROM comments WHERE post_id = ${post_id}`); rows = await db.query(
`SELECT * FROM comments WHERE post_id = ${post_id}`,
);
} }
for (const row of rows) { for (const row of rows) {
@@ -219,8 +222,37 @@ function getCommentsForComments(comment_id: number) {
* @param user_id The ID of the User to get * @param user_id The ID of the User to get
* @returns All of an users Data from the Database * @returns All of an users Data from the Database
* Included: Comments, Posts... Everything including the specific user_uuid in the database * Included: Comments, Posts... Everything including the specific user_uuid in the database
* Might be required for Administrating the User
*/ */
function getAllUserInfoByID(user_id: number) { async function getAllUserInfoByID(user_id: number) {
const data_result_account: Array<Accounts> = [];
const data_result_post: Array<Post> = [];
const data_result_comments: Array<Comments> = [];
let rowsAccount: Row[] = [];
let rowsPost: Row[] = [];
let rowsComments: Row[] = [];
try {
const [rowsAccount, rowsPost, rowsComments] = await Promise.all([
db.query('SELECT * FROM accounts WHERE uuid = ?', [user_id]),
db.query('SELECT * FROM posts WHERE uuid = ?', [user_id]),
db.query('SELECT * FROM comments WHERE uuid = ?', [user_id])
]);
} catch (error) {
console.error(`
Failed to get User Info ${error}
rowsAccount: ${rowsAccount}
rowsPost: ${rowsPost}
rowsComments: ${rowsComments}
`);
return [];
}
return { data_result_account, data_result_comments, data_result_post };
} }
/** /**
@@ -246,9 +278,9 @@ export {
filterForImagePosts, filterForImagePosts,
filterForTextPosts, filterForTextPosts,
filterForVideoPosts, filterForVideoPosts,
getAllUserInfoByID,
getAllUsersFromDB, getAllUsersFromDB,
getCommentsForComments, getCommentsForComments,
getCommentsFromDB, getCommentsFromDB,
getPostsFromDB, getPostsFromDB,
getAllUserInfoByID,
}; };