Huge changes! Added some API features, but not fully.
This commit is contained in:
137
api/helpers/chat_api.ts
Normal file
137
api/helpers/chat_api.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description API file for Comments
|
||||
*/
|
||||
|
||||
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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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");
|
||||
}
|
||||
}
|
||||
128
api/helpers/comments_api.ts
Normal file
128
api/helpers/comments_api.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description API file for Comments
|
||||
*/
|
||||
|
||||
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
|
||||
async function api_getPostComments(ctx: any): Promise<void> {
|
||||
try {
|
||||
const postId = ctx.params.id;
|
||||
if (!postId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Post ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
const comments = await db_utils.getCommentsFromDB(Number(postId));
|
||||
ctx.response.body = comments;
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error retrieving comments");
|
||||
}
|
||||
}
|
||||
|
||||
async function api_createComment(ctx: any): Promise<void> {
|
||||
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, text } = result;
|
||||
|
||||
if (!userId || !text) {
|
||||
helper_utils.errorResponse(ctx, 400, "User ID and comment text 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 commentId = await db_utils.createComment(postId, userId, createdAt, text);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 201,
|
||||
body: { commentId }
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error creating comment");
|
||||
}
|
||||
}
|
||||
|
||||
async function api_updateComment(ctx: any): Promise<void> {
|
||||
try {
|
||||
const commentId = ctx.params.id;
|
||||
if (!commentId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Comment ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
const body = ctx.request.body;
|
||||
const result = await body.json();
|
||||
const { text } = result;
|
||||
|
||||
if (!text) {
|
||||
helper_utils.errorResponse(ctx, 400, "Comment text required");
|
||||
return;
|
||||
}
|
||||
|
||||
await db_utils.updateComment(commentId, text);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 200,
|
||||
body: { message: "Comment updated successfully" }
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error updating comment");
|
||||
}
|
||||
}
|
||||
|
||||
async function api_deleteComment(ctx: any): Promise<void> {
|
||||
try {
|
||||
const commentId = ctx.params.id;
|
||||
if (!commentId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Comment ID required");
|
||||
return;
|
||||
}
|
||||
|
||||
await db_utils.deleteComment(commentId);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 200,
|
||||
body: { message: "Comment deleted successfully" }
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error deleting comment");
|
||||
}
|
||||
}
|
||||
|
||||
async function api_likeComment(ctx: any): Promise<void> {
|
||||
try {
|
||||
const commentId = ctx.params.id;
|
||||
if (!commentId) {
|
||||
helper_utils.errorResponse(ctx, 400, "Comment 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.likeComment(commentId, userId);
|
||||
helper_utils.sendResponse(ctx, {
|
||||
status: 200,
|
||||
body: { message: "Comment liked successfully" }
|
||||
});
|
||||
} catch (error) {
|
||||
helper_utils.errorResponse(ctx, 500, "Error liking comment");
|
||||
}
|
||||
}
|
||||
11
api/helpers/mod.ts
Normal file
11
api/helpers/mod.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description A mod file is used to export all the functions in the folder, making them easier to access.
|
||||
* @file mod.ts
|
||||
*/
|
||||
|
||||
export * from "./chat_api.ts";
|
||||
export * from "./comments_api.ts";
|
||||
export * from "./post_api.ts";
|
||||
export * from "./user_api.ts";
|
||||
8
api/helpers/post_api.ts
Normal file
8
api/helpers/post_api.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description API file for Posts
|
||||
*/
|
||||
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
2
api/helpers/user_api.ts
Normal file
2
api/helpers/user_api.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
import * as db_utils from "../../database/utils.ts";
|
||||
import * as helper_utils from "../helpers.ts";
|
||||
Reference in New Issue
Block a user