From 8b992017bad89f35b57b736975a49752fb70bb80 Mon Sep 17 00:00:00 2001 From: Esad Mustafoski Date: Fri, 14 Mar 2025 06:37:08 +0100 Subject: [PATCH] Modulized the project so it's easier to look over items, Unfinished for now, added extra functions for chat, API is not finished for the chat. and will not work. Files saved as a "Work in progress" type of commit. \n Document folders added, but nothing yet done inside of them. \n Some comments have been removed as they were bloated and made it hard to read the code, the functions themselves are self-explanatory and will be documented in the "docs" folders, containing the documentation in a markdown format --- api/helpers/chat_api.ts | 131 +--------------------------------------- api/helpers/post_api.ts | 129 +++++++++++++++++++++++++++++++++++++++ api/main.ts | 2 - 3 files changed, 130 insertions(+), 132 deletions(-) diff --git a/api/helpers/chat_api.ts b/api/helpers/chat_api.ts index 3d579e7..302c05b 100644 --- a/api/helpers/chat_api.ts +++ b/api/helpers/chat_api.ts @@ -5,133 +5,4 @@ */ import * as db_utils from "../../database/utils.ts"; -import * as helper_utils from "../helpers.ts"; -import { Context } from "https://deno.land/x/oak@v17.1.2/mod.ts"; - -// Post functions -async function api_getPostById(ctx: any): Promise { - try { - const postId = ctx.params.id; - if (!postId) { - helper_utils.errorResponse(ctx, 400, "Post ID required"); - return; - } - - const post = await db_utils.getPostById(postId); - if (!post) { - helper_utils.errorResponse(ctx, 404, "Post not found"); - return; - } - - ctx.response.body = post; - } catch (error) { - helper_utils.errorResponse(ctx, 500, "Error retrieving post"); - } -} - -async function api_createPost(ctx: Context): Promise { - try { - const body = ctx.request.body; - const result = await body.json(); - const { userId, postText, postType } = result; - - if (!userId || !postText || !postType) { - helper_utils.errorResponse( - ctx, - 400, - "User ID, post text, and post type required", - ); - return; - } - - // Create timestamp in the format expected by the database - const createdAt = `${Math.floor(Date.now() / 1000)}-${ - new Date().toLocaleDateString("en-GB").split("/").join("-") - }`; - - const postId = await db_utils.createPost( - userId, - createdAt, - postText, - postType, - ); - helper_utils.sendResponse(ctx, { - status: 201, - body: { postId }, - }); - } catch (error) { - helper_utils.errorResponse(ctx, 500, "Error creating post"); - } -} - -async function api_updatePost(ctx: any): Promise { - try { - const postId = ctx.params.id; - if (!postId) { - helper_utils.errorResponse(ctx, 400, "Post ID required"); - return; - } - - const body = ctx.request.body; - const result = await body.json(); - const { postText, postType } = result; - - if (!postText && !postType) { - helper_utils.errorResponse(ctx, 400, "No update data provided"); - return; - } - - await db_utils.updatePost(postId, postText, postType); - helper_utils.sendResponse(ctx, { - status: 200, - body: { message: "Post updated successfully" }, - }); - } catch (error) { - helper_utils.errorResponse(ctx, 500, "Error updating post"); - } -} - -async function api_deletePost(ctx: any): Promise { - try { - const postId = ctx.params.id; - if (!postId) { - helper_utils.errorResponse(ctx, 400, "Post ID required"); - return; - } - - await db_utils.deletePost(postId); - helper_utils.sendResponse(ctx, { - status: 200, - body: { message: "Post deleted successfully" }, - }); - } catch (error) { - helper_utils.errorResponse(ctx, 500, "Error deleting post"); - } -} - -async function api_likePost(ctx: any): Promise { - try { - const postId = ctx.params.id; - if (!postId) { - helper_utils.errorResponse(ctx, 400, "Post ID required"); - return; - } - - const body = ctx.request.body; - const result = await body.json(); - const { userId } = result; - - if (!userId) { - helper_utils.errorResponse(ctx, 400, "User ID required"); - return; - } - - await db_utils.likePost(postId, userId); - helper_utils.sendResponse(ctx, { - status: 200, - body: { message: "Post liked successfully" }, - }); - } catch (error) { - helper_utils.errorResponse(ctx, 500, "Error liking post"); - } -} +import * as helper_utils from "../helpers.ts"; \ No newline at end of file diff --git a/api/helpers/post_api.ts b/api/helpers/post_api.ts index 6736d58..a125fb4 100644 --- a/api/helpers/post_api.ts +++ b/api/helpers/post_api.ts @@ -6,3 +6,132 @@ import * as db_utils from "../../database/utils.ts"; import * as helper_utils from "../helpers.ts"; +import { Context } from "https://deno.land/x/oak@v17.1.2/mod.ts"; + +// Post functions +async function api_getPostById(ctx: any): Promise { + try { + const postId = ctx.params.id; + if (!postId) { + helper_utils.errorResponse(ctx, 400, "Post ID required"); + return; + } + + const post = await db_utils.getPostById(postId); + if (!post) { + helper_utils.errorResponse(ctx, 404, "Post not found"); + return; + } + + ctx.response.body = post; + } catch (error) { + helper_utils.errorResponse(ctx, 500, "Error retrieving post"); + } +} + +async function api_createPost(ctx: Context): Promise { + try { + const body = ctx.request.body; + const result = await body.json(); + const { userId, postText, postType } = result; + + if (!userId || !postText || !postType) { + helper_utils.errorResponse( + ctx, + 400, + "User ID, post text, and post type required", + ); + return; + } + + // Create timestamp in the format expected by the database + const createdAt = `${Math.floor(Date.now() / 1000)}-${ + new Date().toLocaleDateString("en-GB").split("/").join("-") + }`; + + const postId = await db_utils.createPost( + userId, + createdAt, + postText, + postType, + ); + helper_utils.sendResponse(ctx, { + status: 201, + body: { postId }, + }); + } catch (error) { + helper_utils.errorResponse(ctx, 500, "Error creating post"); + } +} + +async function api_updatePost(ctx: any): Promise { + try { + const postId = ctx.params.id; + if (!postId) { + helper_utils.errorResponse(ctx, 400, "Post ID required"); + return; + } + + const body = ctx.request.body; + const result = await body.json(); + const { postText, postType } = result; + + if (!postText && !postType) { + helper_utils.errorResponse(ctx, 400, "No update data provided"); + return; + } + + await db_utils.updatePost(postId, postText, postType); + helper_utils.sendResponse(ctx, { + status: 200, + body: { message: "Post updated successfully" }, + }); + } catch (error) { + helper_utils.errorResponse(ctx, 500, "Error updating post"); + } +} + +async function api_deletePost(ctx: any): Promise { + try { + const postId = ctx.params.id; + if (!postId) { + helper_utils.errorResponse(ctx, 400, "Post ID required"); + return; + } + + await db_utils.deletePost(postId); + helper_utils.sendResponse(ctx, { + status: 200, + body: { message: "Post deleted successfully" }, + }); + } catch (error) { + helper_utils.errorResponse(ctx, 500, "Error deleting post"); + } +} + +async function api_likePost(ctx: any): Promise { + try { + const postId = ctx.params.id; + if (!postId) { + helper_utils.errorResponse(ctx, 400, "Post ID required"); + return; + } + + const body = ctx.request.body; + const result = await body.json(); + const { userId } = result; + + if (!userId) { + helper_utils.errorResponse(ctx, 400, "User ID required"); + return; + } + + await db_utils.likePost(postId, userId); + helper_utils.sendResponse(ctx, { + status: 200, + body: { message: "Post liked successfully" }, + }); + } catch (error) { + helper_utils.errorResponse(ctx, 500, "Error liking post"); + } +} diff --git a/api/main.ts b/api/main.ts index 7f2ecdf..321d99e 100644 --- a/api/main.ts +++ b/api/main.ts @@ -134,8 +134,6 @@ async function _tokenChecker(ctx: Context, next: Next): Promise { */ } - - // API: Users async function api_user_getInfo(ctx: any): Promise { const id = ctx.params.id;