Huge changes! Added some API features, but not fully.
This commit is contained in:
86
database/helpers/utils/chat_utils.ts
Normal file
86
database/helpers/utils/chat_utils.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description This file is responsible for creating Functions to easily access the Database, Specifically for Chats
|
||||
* @file chatUtil.ts
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import { Chat, Message } from "../interfaces.ts";
|
||||
import { mapChatRow, mapMessageRow, queryDatabase } from "./mod.ts";
|
||||
|
||||
async function getUserChats(db: DB, userId: string): Promise<Chat[]> {
|
||||
const query = `SELECT * FROM chats WHERE participants LIKE '%${userId}%'`;
|
||||
return await queryDatabase<Chat>(db, query, [], mapChatRow);
|
||||
}
|
||||
|
||||
async function getChatById(db: DB, chatId: string): Promise<Chat | null> {
|
||||
const query = `SELECT * FROM chats WHERE chat_id = ?`;
|
||||
const chats = await queryDatabase<Chat>(db, query, [chatId], mapChatRow);
|
||||
return chats.length > 0 ? chats[0] : null;
|
||||
}
|
||||
|
||||
async function getChatMessages(db: DB, chatId: string): Promise<Message[]> {
|
||||
const query =
|
||||
`SELECT * FROM messages WHERE chat_id = ? ORDER BY timestamp ASC`;
|
||||
return await queryDatabase<Message>(db, query, [chatId], mapMessageRow);
|
||||
}
|
||||
|
||||
async function createChat(
|
||||
db: DB,
|
||||
participants: string[],
|
||||
chatName: string,
|
||||
): Promise<string> {
|
||||
const participantsJson = JSON.stringify(participants);
|
||||
const timestamp = `${Math.floor(Date.now() / 1000)}-${
|
||||
new Date().toLocaleDateString("en-GB").split("/").join("-")
|
||||
}`;
|
||||
|
||||
const query = `
|
||||
INSERT INTO chats (chat_name, participants, created_at)
|
||||
VALUES (?, ?, ?)
|
||||
`;
|
||||
|
||||
db.query(query, [chatName, participantsJson, timestamp]);
|
||||
return db.lastInsertRowId.toString();
|
||||
}
|
||||
|
||||
async function addMessageToChat(
|
||||
db: DB,
|
||||
chatId: string,
|
||||
senderId: string,
|
||||
content: string,
|
||||
): Promise<string> {
|
||||
const timestamp = `${Math.floor(Date.now() / 1000)}-${
|
||||
new Date().toLocaleDateString("en-GB").split("/").join("-")
|
||||
}`;
|
||||
|
||||
const query = `
|
||||
INSERT INTO messages (chat_id, sender_id, content, timestamp)
|
||||
VALUES (?, ?, ?, ?)
|
||||
`;
|
||||
|
||||
db.query(query, [chatId, senderId, content, timestamp]);
|
||||
return db.lastInsertRowId.toString();
|
||||
}
|
||||
|
||||
async function deleteChat(db: DB, chatId: string): Promise<void> {
|
||||
// First delete all messages in the chat
|
||||
const deleteMessagesQuery = `DELETE FROM messages WHERE chat_id = ?`;
|
||||
db.query(deleteMessagesQuery, [chatId]);
|
||||
|
||||
// Then delete the chat itself
|
||||
const deleteChatQuery = `DELETE FROM chats WHERE chat_id = ?`;
|
||||
db.query(deleteChatQuery, [chatId]);
|
||||
}
|
||||
|
||||
export {
|
||||
getUserChats,
|
||||
getChatById,
|
||||
getChatMessages,
|
||||
createChat,
|
||||
addMessageToChat,
|
||||
deleteChat
|
||||
};
|
||||
62
database/helpers/utils/comment_utils.ts
Normal file
62
database/helpers/utils/comment_utils.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description This file is responsible for creating Functions to easily access the Database, Specifically for Comments
|
||||
* @file commentUtil.ts
|
||||
*/
|
||||
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import { Comments } from "../interfaces.ts";
|
||||
import { mapCommentRow, queryDatabase } from "./mod.ts";
|
||||
|
||||
async function getCommentsFromDB(db: DB, 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>(db, query, params, mapCommentRow);
|
||||
}
|
||||
|
||||
async function createComment(db: DB, postId: string, userId: string, createdAt: string, text: string): Promise<string> {
|
||||
const query = `
|
||||
INSERT INTO comments (post_id, author_user_id, date_created_at, text, likes)
|
||||
VALUES (?, ?, ?, ?, 0)
|
||||
`;
|
||||
db.query(query, [postId, userId, createdAt, text]);
|
||||
|
||||
const updatePostQuery = `UPDATE posts SET comments = comments + 1 WHERE posts_uuid = ?`;
|
||||
db.query(updatePostQuery, [postId]);
|
||||
|
||||
return db.lastInsertRowId.toString();
|
||||
}
|
||||
|
||||
async function updateComment(db: DB, commentId: string, text: string): Promise<void> {
|
||||
const query = `UPDATE comments SET text = ? WHERE comment_id = ?`;
|
||||
db.query(query, [text, commentId]);
|
||||
}
|
||||
|
||||
async function deleteComment(db: DB, commentId: string): Promise<void> {
|
||||
const getPostIdQuery = `SELECT post_id FROM comments WHERE comment_id = ?`;
|
||||
const result = db.query(getPostIdQuery, [commentId]);
|
||||
const postId: any = result[0][0];
|
||||
|
||||
const deleteCommentQuery = `DELETE FROM comments WHERE comment_id = ?`;
|
||||
db.query(deleteCommentQuery, [commentId]);
|
||||
|
||||
const updatePostQuery = `UPDATE posts SET comments = comments - 1 WHERE posts_uuid = ?`;
|
||||
db.query(updatePostQuery, [postId]);
|
||||
}
|
||||
|
||||
async function likeComment(db: DB, commentId: string, userId: string): Promise<void> {
|
||||
const query = `UPDATE comments SET likes = likes + 1 WHERE comment_id = ?`;
|
||||
db.query(query, [commentId]);
|
||||
}
|
||||
|
||||
export {
|
||||
createComment,
|
||||
updateComment,
|
||||
deleteComment,
|
||||
likeComment,
|
||||
getCommentsFromDB,
|
||||
}
|
||||
45
database/helpers/utils/core_utils.ts
Normal file
45
database/helpers/utils/core_utils.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description Core Utility for querying the Database and inserting Sample Data
|
||||
* @file core_utils.ts
|
||||
*/
|
||||
|
||||
import { DB, Row } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import * as db_create from "../../create_db.ts";
|
||||
|
||||
// +++ Helper ------------------------------------------------------ //
|
||||
// "T" is a generic type, it can be anything and makes the function "flexible"(?)
|
||||
async function queryDatabase<T>(
|
||||
db: DB,
|
||||
query: string,
|
||||
params: any[],
|
||||
mapRow: (row: Row) => 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;
|
||||
}
|
||||
|
||||
// +++ FUNCTIONS --------------------------------------------------- //
|
||||
|
||||
/**
|
||||
* See:
|
||||
* @file ./create_db.ts
|
||||
*/
|
||||
function insertSamples(): void {
|
||||
db_create.insertSampleData();
|
||||
}
|
||||
|
||||
function createDatabaseIfNotExist(): void {
|
||||
db_create.createDatabase();
|
||||
}
|
||||
|
||||
export { queryDatabase, insertSamples, createDatabaseIfNotExist };
|
||||
13
database/helpers/utils/mod.ts
Normal file
13
database/helpers/utils/mod.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
/// <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_utils.ts";
|
||||
export * from "./comment_utils.ts";
|
||||
export * from "./post_utils.ts";
|
||||
export * from "./user_utils.ts";
|
||||
export * from "./core_utils.ts"
|
||||
export * from "../maphelper.ts";
|
||||
118
database/helpers/utils/post_utils.ts
Normal file
118
database/helpers/utils/post_utils.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description This file is responsible for creating Functions to easily access the Database, Specifically for Posts
|
||||
* @file postUtil.ts
|
||||
*/
|
||||
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import { Post } from "../interfaces.ts";
|
||||
import { mapPostRow, queryDatabase } from "./mod.ts";
|
||||
|
||||
async function getPostsFromDB(db: DB, 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>(db, query, params, mapPostRow);
|
||||
}
|
||||
|
||||
async function getPostById(db: DB, postId: string): Promise<Post | null> {
|
||||
const query = `SELECT * FROM posts WHERE posts_uuid = ?`;
|
||||
const posts = await queryDatabase<Post>(db, query, [postId], mapPostRow);
|
||||
return posts.length > 0 ? posts[0] : null;
|
||||
}
|
||||
|
||||
async function createPost(db: DB, userId: string, createdAt: string, postText: string, postType: string): Promise<string> {
|
||||
const query = `
|
||||
INSERT INTO posts (user_id, created_at, post_text, post_type, likes, comments)
|
||||
VALUES (?, ?, ?, ?, 0, 0)
|
||||
`;
|
||||
|
||||
db.query(query, [userId, createdAt, postText, postType]);
|
||||
return db.lastInsertRowId.toString();
|
||||
}
|
||||
|
||||
async function updatePost(db: DB, postId: string, postText?: string, postType?: string): Promise<void> {
|
||||
let query = `UPDATE posts SET `;
|
||||
const params: any[] = [];
|
||||
|
||||
if (postText) {
|
||||
query += `post_text = ?`;
|
||||
params.push(postText);
|
||||
|
||||
if (postType) {
|
||||
query += `, post_type = ?`;
|
||||
params.push(postType);
|
||||
}
|
||||
} else if (postType) {
|
||||
query += `post_type = ?`;
|
||||
params.push(postType);
|
||||
}
|
||||
|
||||
query += ` WHERE posts_uuid = ?`;
|
||||
params.push(postId);
|
||||
|
||||
db.query(query, params);
|
||||
}
|
||||
|
||||
async function deletePost(db: DB, postId: string): Promise<void> {
|
||||
// First delete all comments on the post
|
||||
const deleteCommentsQuery = `DELETE FROM comments WHERE post_id = ?`;
|
||||
db.query(deleteCommentsQuery, [postId]);
|
||||
|
||||
// Then delete the post itself
|
||||
const deletePostQuery = `DELETE FROM posts WHERE posts_uuid = ?`;
|
||||
db.query(deletePostQuery, [postId]);
|
||||
}
|
||||
|
||||
async function likePost(db: DB, postId: string, userId: string): Promise<void> {
|
||||
// In a real application, you would check if the user has already liked the post
|
||||
// and store like relationships in a separate table. This is a simplified version.
|
||||
const query = `UPDATE posts SET likes = likes + 1 WHERE posts_uuid = ?`;
|
||||
db.query(query, [postId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param posts_to_filter The Posts in an array to filter
|
||||
* @param post_types The types of Posts to filter for
|
||||
* @returns Array of Posts
|
||||
*/
|
||||
// Filter functions merged to one
|
||||
function filterPosts(posts_to_filter: Post[], post_types: string[]): Post[] {
|
||||
if (post_types.length === 0) {
|
||||
return posts_to_filter;
|
||||
}
|
||||
|
||||
// Set is a type that we can specify the values to
|
||||
// example: const set = new Set<number>();
|
||||
// set.add(1); Will work
|
||||
// set.add("2"); Will not work, it will error because
|
||||
// it is a string.
|
||||
const includedPostIds = new Set<number>();
|
||||
const result: Post[] = [];
|
||||
|
||||
for (const type of post_types) {
|
||||
for (const post of posts_to_filter) {
|
||||
if (!includedPostIds.has(post.posts_uuid) && post.post_type === type) {
|
||||
result.push(post);
|
||||
includedPostIds.add(post.posts_uuid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export {
|
||||
getPostsFromDB,
|
||||
getPostById,
|
||||
createPost,
|
||||
updatePost,
|
||||
deletePost,
|
||||
likePost,
|
||||
filterPosts,
|
||||
};
|
||||
95
database/helpers/utils/user_utils.ts
Normal file
95
database/helpers/utils/user_utils.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
/// <reference lib="deno.ns" />
|
||||
|
||||
/**
|
||||
* @author Esad Mustafoski
|
||||
* @description This file is responsible for creating Functions to easily access the Database, Specifically for Users
|
||||
* @file userUtil.ts
|
||||
*/
|
||||
|
||||
// +++ IMPORTS ------------------------------------------------------ //
|
||||
import { DB } from "https://deno.land/x/sqlite@v3.9.1/mod.ts";
|
||||
import { queryDatabase, mapAccountRow } from "./mod.ts";
|
||||
import { Accounts } from "../interfaces.ts";
|
||||
|
||||
/**
|
||||
* @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(
|
||||
db: DB,
|
||||
user: string,
|
||||
password: string,
|
||||
salt: string,
|
||||
userGroup: string,
|
||||
displayname: string,
|
||||
user_email: string,
|
||||
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";
|
||||
}
|
||||
|
||||
const query_add_user = `
|
||||
INSERT INTO accounts (
|
||||
username,
|
||||
password,
|
||||
password_salt,
|
||||
user_group,
|
||||
displayname,
|
||||
user_email,
|
||||
firstname,
|
||||
surname,
|
||||
account_created,
|
||||
bio,
|
||||
blocked_users,
|
||||
followers,
|
||||
following,
|
||||
contacts
|
||||
) VALUES (
|
||||
'${user}',
|
||||
'${password}',
|
||||
'${salt}',
|
||||
'${userGroup}',
|
||||
'${displayname}',
|
||||
'${user_email}',
|
||||
'${firstname}',
|
||||
'${surname}',
|
||||
'${account_created}',
|
||||
'',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]',
|
||||
'[]'
|
||||
)`;
|
||||
db.query(query_add_user);
|
||||
console.log(`New user: ${user}`);
|
||||
|
||||
return "newUser";
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns Array of all Users in the Database
|
||||
*/
|
||||
async function getAllUsersFromDB(db: DB): Promise<Accounts[]> {
|
||||
const query = `SELECT * FROM accounts`;
|
||||
return await queryDatabase<Accounts>(db, query, [], mapAccountRow);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param username
|
||||
* @returns Returns the Accounts for the User with the given username
|
||||
*/
|
||||
async function getUserByUsername(db: DB, username: string): Promise<Accounts> {
|
||||
const query = `SELECT * FROM accounts WHERE username = '${username}'`;
|
||||
const params: string[] = [];
|
||||
const result = await queryDatabase<Accounts>(db, query, params, mapAccountRow);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
export { registerUser, getAllUsersFromDB, getUserByUsername };
|
||||
Reference in New Issue
Block a user