Generated API Docs using AI, Might edit later if there is an issue or it's hard to read. Finished API Functions
This commit is contained in:
@@ -4,5 +4,139 @@
|
||||
* @description API file for Comments
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
|
||||
// +++ FUNCTIONS ----------------------------------------------------- //
|
||||
async function api_getChats(ctx: any): Promise<void> {
|
||||
try {
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
if (!authHeader) {
|
||||
helper_utils.errorResponse(ctx, 401, "Authentication Required");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get userId from query parameter (for testing) or use 1 as default (avoid errors because of userid)
|
||||
// Assumes 1 is a test account
|
||||
const userId = ctx.request.url.searchParams.get("userId") || "1";
|
||||
|
||||
const chats = await db_utils.getUserChats(userId);
|
||||
ctx.response.body = chats;
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error retrieving chats");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function api_getChatById(ctx: any): Promise<void> {
|
||||
try {
|
||||
const chatId = ctx.params.id;
|
||||
if (!chatId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Chat ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
const chat = await db_utils.getChatById(chatId);
|
||||
if (!chat) {
|
||||
helper_utils.errorResponse(ctx, 404, "Chat not found");
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.response.body = chat;
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error retrieving chat");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function api_createChat(ctx: any): Promise<void> {
|
||||
try {
|
||||
const body = ctx.request.body;
|
||||
const result = await body.json();
|
||||
const { participants, chatName } = result;
|
||||
|
||||
if (
|
||||
!participants || !Array.isArray(participants) || participants.length < 2
|
||||
) {
|
||||
helper_utils.errorResponse(
|
||||
ctx,
|
||||
400,
|
||||
"Two people required to create a chat",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const chatId = await db_utils.createChat(participants, chatName || "");
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 201,
|
||||
body: { chatId },
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error creating chat");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function api_sendMessage(ctx: any): Promise<void> {
|
||||
try {
|
||||
const chatId = ctx.params.id;
|
||||
if (!chatId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Chat ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
const body = ctx.request.body;
|
||||
const result = await body.json();
|
||||
const { senderId, content } = result;
|
||||
|
||||
if (!senderId || !content) {
|
||||
helper_utils.errorResponse(
|
||||
ctx,
|
||||
400,
|
||||
"Sender ID and message content required",
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const messageId = await db_utils.addMessageToChat(
|
||||
chatId,
|
||||
senderId,
|
||||
content,
|
||||
);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 201,
|
||||
body: { messageId },
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error sending message");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
async function api_deleteChat(ctx: any): Promise<void> {
|
||||
try {
|
||||
const chatId = ctx.params.id;
|
||||
if (!chatId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Chat ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
await db_utils.deleteChat(chatId);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 200,
|
||||
body: { message: "Chat deleted successfully" },
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error deleting chat");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
api_createChat,
|
||||
api_deleteChat,
|
||||
api_getChatById,
|
||||
api_getChats,
|
||||
api_sendMessage,
|
||||
};
|
||||
|
||||
@@ -4,10 +4,11 @@
|
||||
* @description API file for Comments
|
||||
*/
|
||||
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
|
||||
// +++ FUNCTIONS ----------------------------------------------------- //
|
||||
async function api_getPostComments(ctx: any): Promise<void> {
|
||||
try {
|
||||
const postId = ctx.params.id;
|
||||
@@ -20,6 +21,7 @@ async function api_getPostComments(ctx: any): Promise<void> {
|
||||
ctx.response.body = comments;
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error retrieving comments");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +54,7 @@ async function api_createComment(ctx: any): Promise<void> {
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error creating comment");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +82,7 @@ async function api_updateComment(ctx: any): Promise<void> {
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error updating comment");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +101,7 @@ async function api_deleteComment(ctx: any): Promise<void> {
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error deleting comment");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,5 +129,14 @@ async function api_likeComment(ctx: any): Promise<void> {
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error liking comment");
|
||||
console.log(error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
api_createComment,
|
||||
api_deleteComment,
|
||||
api_getPostComments,
|
||||
api_likeComment,
|
||||
api_updateComment,
|
||||
};
|
||||
@@ -4,11 +4,12 @@
|
||||
* @description API file for Posts
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
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
|
||||
// +++ FUNCTIONS ----------------------------------------------------- //
|
||||
async function api_getPostById(ctx: any): Promise<void> {
|
||||
try {
|
||||
const postId = ctx.params.id;
|
||||
@@ -135,3 +136,11 @@ async function api_likePost(ctx: any): Promise<void> {
|
||||
helper_utils.errorResponse(ctx, 500, "Error liking post");
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
api_createPost,
|
||||
api_deletePost,
|
||||
api_getPostById,
|
||||
api_likePost,
|
||||
api_updatePost,
|
||||
};
|
||||
@@ -1,2 +1,45 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description API file for Users
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
// import * as helper_utils from "../helpers.ts";
|
||||
import { Context } from "https://deno.land/x/oak@v17.1.2/mod.ts";
|
||||
|
||||
// +++ FUNCTIONS ----------------------------------------------------- //
|
||||
|
||||
/*async function api_user_getInfo(ctx: any): Promise<void> {
|
||||
const id = ctx.params.id;
|
||||
|
||||
if (!id) {
|
||||
ctx.response.status = 400;
|
||||
ctx.response.body = { error: "User ID required" };
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const user = await db_utils.getAllUserInfoByID(id);
|
||||
if (user === null || user === undefined) {
|
||||
helper_utils.errorResponse(ctx, 404, "User not found");
|
||||
return;
|
||||
}
|
||||
|
||||
ctx.response.body = user;
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, error as string);
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
async function api_getAllUsers(ctx: Context): Promise<void> {
|
||||
const getUsers = await db_utils.getAllUsersFromDB();
|
||||
ctx.response.body = getUsers;
|
||||
}
|
||||
|
||||
export {
|
||||
api_getAllUsers,
|
||||
// api_user_getInfo
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user