Huge changes! Added some API features, but not fully.
This commit is contained in:
0
api/doc/API.md
Normal file
0
api/doc/API.md
Normal file
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";
|
||||
61
api/main.ts
61
api/main.ts
@@ -1,7 +1,7 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @ðescription Main API file, Handles all the routing/api stuff
|
||||
* @description Main API file, Handles all the routing/api stuff
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
@@ -10,11 +10,13 @@ import {
|
||||
Context,
|
||||
Next,
|
||||
Router,
|
||||
} from "https://deno.land/x/oak/mod.ts";
|
||||
import { oakCors } from "https://deno.land/x/cors/mod.ts";
|
||||
} from "https://deno.land/x/oak@v17.1.2/mod.ts";
|
||||
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
|
||||
import * as db_utils from "../database/utils.ts";
|
||||
import * as helper_utils from "./helpers.ts";
|
||||
|
||||
import {} from "./helpers/mod.ts";
|
||||
|
||||
// +++ VARIABLES / TYPES --------------------------------------------- //
|
||||
const router = new Router();
|
||||
const app = new Application();
|
||||
@@ -63,13 +65,36 @@ router
|
||||
.get("/api/users", api_getAllUsers)
|
||||
.get("/api/user/:id/info", api_user_getInfo);
|
||||
|
||||
// -- Chat routes --
|
||||
router
|
||||
.get("/api/chats", api_getChats)
|
||||
.get("/api/chat/:id", api_getChatById)
|
||||
.post("/api/chat/create", api_createChat)
|
||||
.post("/api/chat/:id/message", api_sendMessage)
|
||||
.delete("/api/chat/:id", api_deleteChat);
|
||||
|
||||
// -- Post routes --
|
||||
router
|
||||
.get("/api/posts", api_posts_getAll);
|
||||
.get("/api/posts", api_posts_getAll)
|
||||
.get("/api/post/:id", api_getPostById)
|
||||
.post("/api/post/create", api_createPost)
|
||||
.put("/api/post/:id", api_updatePost)
|
||||
.delete("/api/post/:id", api_deletePost)
|
||||
.post("/api/post/:id/like", api_likePost);
|
||||
|
||||
// -- Comment Routes --
|
||||
router
|
||||
.get("/api/post/:id/comments", api_getPostComments)
|
||||
.post("/api/post/:id/comment", api_createComment)
|
||||
.put("/api/comment/:id", api_updateComment)
|
||||
.delete("/api/comment/:id", api_deleteComment)
|
||||
.post("/api/comment/:id/like", api_likeComment);
|
||||
|
||||
|
||||
// +++ FUNCTIONS ----------------------------------------------------- //
|
||||
async function authenticator(ctx: Context, next: Next): Promise<void> {
|
||||
|
||||
// ABANDONED FUNCTIONS
|
||||
async function _authenticator(ctx: Context, next: Next): Promise<void> {
|
||||
const authHeader = ctx.request.headers.get("Authorization");
|
||||
|
||||
if (!authHeader) {
|
||||
@@ -78,6 +103,8 @@ async function authenticator(ctx: Context, next: Next): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
// Bearer check
|
||||
// Bearer is often used for authentication in API's
|
||||
const match = authHeader.match(/^Bearer (.+)$/);
|
||||
if (!match) {
|
||||
ctx.response.status = 401;
|
||||
@@ -96,7 +123,7 @@ async function authenticator(ctx: Context, next: Next): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
async function tokenChecker(ctx: Context, next: Next): Promise<void> {
|
||||
async function _tokenChecker(ctx: Context, next: Next): Promise<void> {
|
||||
// logic below (TODO):
|
||||
/**
|
||||
* 1. check if the token is valid
|
||||
@@ -107,12 +134,9 @@ async function tokenChecker(ctx: Context, next: Next): Promise<void> {
|
||||
*/
|
||||
}
|
||||
|
||||
async function api_getAllUsers(ctx: Context): Promise<void> {
|
||||
const getUsers = await db_utils.getAllUsersFromDB();
|
||||
ctx.response.body = getUsers;
|
||||
}
|
||||
|
||||
// Users
|
||||
|
||||
// API: Users
|
||||
async function api_user_getInfo(ctx: any): Promise<void> {
|
||||
const id = ctx.params.id;
|
||||
|
||||
@@ -135,15 +159,17 @@ async function api_user_getInfo(ctx: any): Promise<void> {
|
||||
}
|
||||
}
|
||||
|
||||
// Posts
|
||||
async function api_getAllUsers(ctx: Context): Promise<void> {
|
||||
const getUsers = await db_utils.getAllUsersFromDB();
|
||||
ctx.response.body = getUsers;
|
||||
}
|
||||
// API: Posts
|
||||
async function api_posts_getAll(ctx: Context): Promise<void> {
|
||||
const posts = await db_utils.getPostsFromDB();
|
||||
ctx.response.body = posts;
|
||||
}
|
||||
|
||||
// Comments (missing)
|
||||
|
||||
// login/register
|
||||
// API: login/register
|
||||
async function api_register(ctx: Context): Promise<void> {
|
||||
try {
|
||||
const body = ctx.request.body;
|
||||
@@ -157,6 +183,7 @@ async function api_register(ctx: Context): Promise<void> {
|
||||
firstname,
|
||||
surname,
|
||||
} = result;
|
||||
// Claude 3-5 Sonnet was used for the first Date formatting
|
||||
const account_created = `${Math.floor(Date.now() / 1000)}-${
|
||||
new Date().toLocaleDateString("en-GB").split("/").join("-")
|
||||
}`;
|
||||
@@ -218,10 +245,10 @@ async function api_login(ctx: Context): Promise<string> {
|
||||
const storedSalt = user.password_salt;
|
||||
// Salt the provided password with the stored salt
|
||||
const saltedPassword = `${password}${storedSalt}`;
|
||||
// Hash the salted password
|
||||
// Hash the salted password
|
||||
const hash = await helper_utils.hashPassword(saltedPassword);
|
||||
|
||||
// Compare with stored hash
|
||||
// Compare the phashed password with the stored hash
|
||||
if (user.password !== hash) {
|
||||
helper_utils.errorResponse(ctx, 401, "Invalid password");
|
||||
return "Error";
|
||||
|
||||
Reference in New Issue
Block a user