Huge changes! Added some API features, but not fully.
This commit is contained in:
@@ -2,14 +2,53 @@
|
||||
* @author Esad Mustafoski
|
||||
* @description This file is responsible for creating Functions to easily access the Database, Intended for use in the API
|
||||
*/
|
||||
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import { DB, Row } from "https://deno.land/x/sqlite/mod.ts";
|
||||
import { dirname, fromFileUrl, join } from "https://deno.land/std/path/mod.ts";
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import {
|
||||
dirname,
|
||||
fromFileUrl,
|
||||
join,
|
||||
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
||||
import * as db_create from "./create_db.ts";
|
||||
|
||||
// Import all internal utilities with renamed imports to avoid naming conflicts
|
||||
import {
|
||||
// --- Account Functions ---
|
||||
getAllUsersFromDB as getAllUsersFromDBInternal,
|
||||
getUserByUsername as getUserByUsernameInternal,
|
||||
// getAllUserInfoByID as getAllUserInfoByIDInternal,
|
||||
registerUser as registerUserInternal,
|
||||
|
||||
// --- Post Functions ---
|
||||
getPostsFromDB as getPostsFromDBInternal,
|
||||
getPostById as getPostByIdInternal,
|
||||
createPost as createPostInternal,
|
||||
updatePost as updatePostInternal,
|
||||
deletePost as deletePostInternal,
|
||||
likePost as likePostInternal,
|
||||
filterPosts,
|
||||
|
||||
// --- Comment Functions ---
|
||||
getCommentsFromDB as getCommentsFromDBInternal,
|
||||
// getCommentsForComments as getCommentsForCommentsInternal,
|
||||
createComment as createCommentInternal,
|
||||
updateComment as updateCommentInternal,
|
||||
deleteComment as deleteCommentInternal,
|
||||
likeComment as likeCommentInternal,
|
||||
|
||||
// --- Chat Functions ---
|
||||
getUserChats as getUserChatsInternal,
|
||||
getChatById as getChatByIdInternal,
|
||||
getChatMessages as getChatMessagesInternal,
|
||||
createChat as createChatInternal,
|
||||
addMessageToChat as addMessageToChatInternal,
|
||||
deleteChat as deleteChatInternal,
|
||||
|
||||
// --- Mapper Functions ---
|
||||
queryDatabase as queryDatabaseInternal,
|
||||
} from "./helpers/utils/mod.ts";
|
||||
|
||||
// +++ VARIABLES ---------------------------------------------------- //
|
||||
// _dirname Is never getting used again, It's only needed because the DB Import
|
||||
// from SQLite doesn't like relative paths, so I use this as
|
||||
@@ -19,235 +58,37 @@ const dbPath: string = join(_dirname, "../database/esp-projekt.sqlite");
|
||||
const db = new DB(dbPath);
|
||||
|
||||
// +++ INTERFACES --------------------------------------------------- //
|
||||
// Used in the Functions to define the return type/Avoid type errors
|
||||
interface Post {
|
||||
posts_uuid: number;
|
||||
user_id: number;
|
||||
created_at: string;
|
||||
post_text: string;
|
||||
likes: number;
|
||||
comments: number;
|
||||
}
|
||||
|
||||
interface Accounts {
|
||||
user_id: number;
|
||||
user_group: string;
|
||||
bio: string;
|
||||
displayname: string;
|
||||
username: string;
|
||||
user_email: string;
|
||||
password: string;
|
||||
password_salt: string;
|
||||
firstname: string;
|
||||
surname: string;
|
||||
account_created: string;
|
||||
blocked_users: string;
|
||||
followers: string;
|
||||
following: string;
|
||||
contacts: string;
|
||||
}
|
||||
|
||||
interface Comments {
|
||||
comment_id: number;
|
||||
post_id: number;
|
||||
author_user_id: number;
|
||||
date_created_at: string;
|
||||
text: string;
|
||||
likes: number;
|
||||
}
|
||||
|
||||
// +++ Helper ------------------------------------------------------ //
|
||||
function mapPostRow(row: Row): Post {
|
||||
const [posts_uuid, user_id, created_at, post_text, likes, comments] = row;
|
||||
return {
|
||||
posts_uuid: Number(posts_uuid),
|
||||
user_id: Number(user_id),
|
||||
created_at: String(created_at),
|
||||
post_text: String(post_text),
|
||||
likes: Number(likes),
|
||||
comments: Number(comments),
|
||||
};
|
||||
}
|
||||
|
||||
function mapAccountRow(row: Row): Accounts {
|
||||
const [
|
||||
user_id,
|
||||
user_group,
|
||||
bio,
|
||||
displayname,
|
||||
username,
|
||||
user_email,
|
||||
password,
|
||||
password_salt,
|
||||
firstname,
|
||||
surname,
|
||||
account_created,
|
||||
blocked_users,
|
||||
followers,
|
||||
following,
|
||||
contacts,
|
||||
] = row;
|
||||
return {
|
||||
user_id: Number(user_id),
|
||||
user_group: String(user_group),
|
||||
bio: String(bio),
|
||||
displayname: String(displayname),
|
||||
username: String(username),
|
||||
user_email: String(user_email),
|
||||
password: String(password),
|
||||
password_salt: String(password_salt),
|
||||
firstname: String(firstname),
|
||||
surname: String(surname),
|
||||
account_created: String(account_created),
|
||||
blocked_users: String(blocked_users),
|
||||
followers: String(followers),
|
||||
following: String(following),
|
||||
contacts: String(contacts),
|
||||
};
|
||||
}
|
||||
|
||||
function mapCommentRow(row: Row): Comments {
|
||||
const [
|
||||
comment_id,
|
||||
post_id,
|
||||
author_user_id,
|
||||
date_created_at,
|
||||
text,
|
||||
likes,
|
||||
] = row;
|
||||
return {
|
||||
comment_id: Number(comment_id),
|
||||
post_id: Number(post_id),
|
||||
author_user_id: Number(author_user_id),
|
||||
date_created_at: String(date_created_at),
|
||||
text: String(text),
|
||||
likes: Number(likes),
|
||||
};
|
||||
}
|
||||
|
||||
// "T" is a generic type, it can be anything and makes the function "flexible"(?)
|
||||
async function queryDatabase<T>(
|
||||
// Only re-export interfaces that are needed by external code
|
||||
export type {
|
||||
Post,
|
||||
Accounts,
|
||||
Comments,
|
||||
Chat,
|
||||
Message,
|
||||
} from "./helpers/interfaces.ts";
|
||||
// +++ HELPER FUNCTIONS --------------------------------------------- //
|
||||
export function queryDatabase<T>(
|
||||
query: string,
|
||||
params: any[],
|
||||
mapRow: (row: Row) => T,
|
||||
mapRow: (row: any) => T,
|
||||
): Promise<T[]> {
|
||||
const results: T[] = [];
|
||||
try {
|
||||
const rows = await db.query(query, params);
|
||||
for (const row of rows) {
|
||||
results.push(mapRow(row));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Database query error:", error);
|
||||
}
|
||||
return results;
|
||||
return queryDatabaseInternal(db, query, params, mapRow);
|
||||
}
|
||||
|
||||
// +++ FUNCTIONS --------------------------------------------------- //
|
||||
|
||||
/**
|
||||
* See:
|
||||
* @file ./create_db.ts
|
||||
*/
|
||||
function insertSamples(): void {
|
||||
db_create.insertSampleData();
|
||||
}
|
||||
|
||||
function createDatabaseIfNotExist(): void {
|
||||
// +++ DATABASE INITIALIZATION -------------------------------------- //
|
||||
export function createDatabaseIfNotExist(): void {
|
||||
db_create.createDatabase();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user_uuid The UUID of the User to get the Posts for. Not required
|
||||
* @description If no user_uuid is provided, all Posts will be returned
|
||||
* @description If a user_uuid is provided, only the Posts from that User will be returned
|
||||
* @returns Array of all Posts in the Database
|
||||
*/
|
||||
async function getPostsFromDB(user_uuid?: string): Promise<Post[]> {
|
||||
const query = user_uuid
|
||||
? `SELECT * FROM posts WHERE user_id = ?`
|
||||
: `SELECT * FROM posts`;
|
||||
const params = user_uuid ? [user_uuid] : [];
|
||||
return await queryDatabase<Post>(query, params, mapPostRow);
|
||||
export function insertSamples(): void {
|
||||
db_create.insertSampleData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns Array of all Users in the Database
|
||||
*/
|
||||
async function getAllUsersFromDB(): Promise<Accounts[]> {
|
||||
const query = `SELECT * FROM accounts`;
|
||||
return await queryDatabase<Accounts>(query, [], mapAccountRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* @returns Returns the Accounts for the User with the given username
|
||||
*/
|
||||
async function getUserByUsername(username: string): Promise<Accounts> {
|
||||
const query = `SELECT * FROM accounts WHERE user_username = '${username}'`;
|
||||
const params: string[] = [];
|
||||
const result = await queryDatabase<Accounts>(query, params, mapAccountRow);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param post_id The ID of the Post to get the Comments for. Not required
|
||||
* @description If no post_id is provided, all Comments will be returned
|
||||
* @description If a post_id is provided, only the Comments for that Post will be returned
|
||||
* @returns Array of Comments for the Post, or an empty Array if there are no Comments
|
||||
*/
|
||||
async function getCommentsFromDB(post_id?: number): Promise<Comments[]> {
|
||||
const query = post_id
|
||||
? `SELECT * FROM comments WHERE post_id = ?`
|
||||
: `SELECT * FROM comments`;
|
||||
const params = post_id ? [post_id] : [];
|
||||
return await queryDatabase<Comments>(query, params, mapCommentRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param comment_id The ID of the Comment to get the Comments for
|
||||
* @returns Array of Comments for the Comment, or an empty Array if there are no Comments
|
||||
*/
|
||||
function getCommentsForComments(comment_id: number) {
|
||||
// Will be rewritten to use the queryDatabase function
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user_id The ID of the User to get
|
||||
* @returns All of an users Data from the Database
|
||||
* Included: Comments, Posts... Everything including the specific user_uuid in the database
|
||||
* Might be required for Administrating the User
|
||||
*/
|
||||
async function getAllUserInfoByID(user_id: string) {
|
||||
// Will be rewritten to use the queryDatabase function
|
||||
}
|
||||
|
||||
/**
|
||||
* @param user_id The ID of the User to get the Posts for
|
||||
* @returns Array of Posts from the User, or an empty Array if the User doesn't exist or has no Posts
|
||||
*/
|
||||
// Filter Functions, Not yet implemented
|
||||
function filterForImagePosts(posts_to_filter: Array<any>) {
|
||||
return [];
|
||||
}
|
||||
|
||||
function filterForVideoPosts(posts_to_filter: Array<any>) {
|
||||
return [];
|
||||
}
|
||||
|
||||
function filterForTextPosts(posts_to_filter: Array<any>) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Register/Login/User
|
||||
|
||||
/**
|
||||
* @param user The username of the User to add
|
||||
* @param password The hashed password of the User to add
|
||||
* @param salt The salt used for the password
|
||||
* @returns "noUser" if user exists, "newUser" if registration successful
|
||||
*/
|
||||
function registerUser(
|
||||
// +++ ACCOUNT FUNCTIONS -------------------------------------------- //
|
||||
export const getAllUsersFromDB = () => getAllUsersFromDBInternal(db);
|
||||
export const getUserByUsername = (username: string) => getUserByUsernameInternal(db, username);
|
||||
// export const getAllUserInfoByID = (user_id: string) => getAllUserInfoByIDInternal(db, user_id);
|
||||
export const registerUser = (
|
||||
user: string,
|
||||
password: string,
|
||||
salt: string,
|
||||
@@ -257,50 +98,63 @@ function registerUser(
|
||||
firstname: string,
|
||||
surname: string,
|
||||
account_created: string,
|
||||
): string {
|
||||
const query_user_exists =
|
||||
`SELECT * FROM accounts WHERE user_username = '${user}'`;
|
||||
if (!query_user_exists) {
|
||||
return "noUser";
|
||||
}
|
||||
) => registerUserInternal(
|
||||
db,
|
||||
user,
|
||||
password,
|
||||
salt,
|
||||
userGroup,
|
||||
displayname,
|
||||
user_email,
|
||||
firstname,
|
||||
surname,
|
||||
account_created,
|
||||
);
|
||||
|
||||
const query_add_user = `
|
||||
INSERT INTO accounts (
|
||||
user_username,
|
||||
password,
|
||||
password_salt,
|
||||
user_group,
|
||||
user_displayname,
|
||||
user_email,
|
||||
firstname,
|
||||
surname,
|
||||
account_created
|
||||
) VALUES (
|
||||
'${user}',
|
||||
'${password}',
|
||||
'${salt}',
|
||||
'${userGroup}',
|
||||
'${displayname}',
|
||||
'${user_email}',
|
||||
'${firstname}',
|
||||
'${surname}',
|
||||
'${account_created}'
|
||||
)`;
|
||||
db.query(query_add_user);
|
||||
console.log(`New user: ${user}`);
|
||||
// +++ POST FUNCTIONS ----------------------------------------------- //
|
||||
export const getPostsFromDB = (user_uuid?: string) => getPostsFromDBInternal(db, user_uuid);
|
||||
export const getPostById = (postId: string) => getPostByIdInternal(db, postId);
|
||||
export const createPost = (
|
||||
userId: string,
|
||||
createdAt: string,
|
||||
postText: string,
|
||||
postType: string,
|
||||
) => createPostInternal(db, userId, createdAt, postText, postType);
|
||||
export const updatePost = (
|
||||
postId: string,
|
||||
postText?: string,
|
||||
postType?: string,
|
||||
) => updatePostInternal(db, postId, postText, postType);
|
||||
export const deletePost = (postId: string) => deletePostInternal(db, postId);
|
||||
export const likePost = (postId: string, userId: string) => likePostInternal(db, postId, userId);
|
||||
|
||||
return "newUser";
|
||||
}
|
||||
// +++ COMMENT FUNCTIONS -------------------------------------------- //
|
||||
export const getCommentsFromDB = (post_id?: number) => getCommentsFromDBInternal(db, post_id);
|
||||
// export const getCommentsForComments = (comment_id: number) => getCommentsForCommentsInternal(db, comment_id);
|
||||
export const createComment = (
|
||||
postId: string,
|
||||
userId: string,
|
||||
createdAt: string,
|
||||
text: string,
|
||||
) => createCommentInternal(db, postId, userId, createdAt, text);
|
||||
export const updateComment = (commentId: string, text: string) => updateCommentInternal(db, commentId, text);
|
||||
export const deleteComment = (commentId: string) => deleteCommentInternal(db, commentId);
|
||||
export const likeComment = (commentId: string, userId: string) => likeCommentInternal(db, commentId, userId);
|
||||
|
||||
// Exporting all functions as this is a module
|
||||
export {
|
||||
createDatabaseIfNotExist,
|
||||
getAllUserInfoByID,
|
||||
getAllUsersFromDB,
|
||||
getCommentsForComments,
|
||||
getCommentsFromDB,
|
||||
getPostsFromDB,
|
||||
getUserByUsername,
|
||||
insertSamples,
|
||||
registerUser,
|
||||
};
|
||||
// +++ CHAT FUNCTIONS ----------------------------------------------- //
|
||||
export const getUserChats = (userId: string) => getUserChatsInternal(db, userId);
|
||||
export const getChatById = (chatId: string) => getChatByIdInternal(db, chatId);
|
||||
export const getChatMessages = (chatId: string) => getChatMessagesInternal(db, chatId);
|
||||
export const createChat = (
|
||||
participants: string[],
|
||||
chatName: string,
|
||||
) => createChatInternal(db, participants, chatName);
|
||||
export const addMessageToChat = (
|
||||
chatId: string,
|
||||
senderId: string,
|
||||
content: string,
|
||||
) => addMessageToChatInternal(db, chatId, senderId, content);
|
||||
export const deleteChat = (chatId: string) => deleteChatInternal(db, chatId);
|
||||
|
||||
// +++ UTILITY FUNCTIONS -------------------------------------------- //
|
||||
export { filterPosts };
|
||||
Reference in New Issue
Block a user