Added API and Tests, Tests generated using AI and will be replaced sometime

This commit is contained in:
Esad Mustafoski
2024-11-05 02:05:39 +01:00
parent 7a43130a39
commit 3d31061087
6 changed files with 433 additions and 74 deletions

View File

@@ -1,15 +1,21 @@
// Responsible: Esad Mustafoski
/// <reference lib="deno.ns" />
import { DB } from "https://deno.land/x/sqlite/mod.ts";
import { dirname, fromFileUrl, join } from "https://deno.land/std/path/mod.ts";
// __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
// from SQLite doesn't like relative paths, so I use this as
// A Workaround
const __dirname:string = dirname(fromFileUrl(import.meta.url));
const dbPath:string = join(__dirname, "../database/esp-projekt.sqlite");
const _dirname: string = dirname(fromFileUrl(import.meta.url));
const dbPath: string = join(_dirname, "../database/esp-projekt.sqlite");
console.log(dbPath);
console.log(_dirname)
const db = new DB(dbPath);
console.log(db)
// Interfaces
// Interfaces used for the Lists so no data can be assigned wrongly
interface Post {
posts_uuid: number;
user_id: number;
@@ -19,37 +25,112 @@ interface Post {
comments: number;
}
interface Accounts {
user_id: number;
user_group: string;
bio: string;
displayname: string;
username: string;
user_email: string;
password: string;
firstname: string;
surname: string;
account_created: string;
blocked_users: string;
followers: string;
following: string;
contacts: string;
}
/**
* @returns
*/
async function getPostsFromDB() {
let dataresult: Array<Post> = [];
const data_result: Array<Post> = [];
try {
const rows = await db.query("SELECT * FROM posts");
const rows = await db.query(`SELECT * FROM posts`);
// Assuming `db.query` returns an array of arrays or tuples
for (const row of rows) {
const [posts_uuid, user_id, created_at, post_text, likes, comments] = row;
const [
posts_uuid,
user_id,
created_at,
post_text,
likes,
comments,
] = row;
dataresult.push({
posts_uuid: Number(posts_uuid), // Convert to string if necessary
data_result.push({
posts_uuid: Number(posts_uuid), // Convert to string if necessary
user_id: Number(user_id),
created_at: String(created_at), // Convert to Date if necessary
created_at: String(created_at), // Convert to Date if necessary
post_text: String(post_text),
likes: Number(likes), // Convert to number if necessary
comments: Number(comments), // Convert to number if necessary
likes: Number(likes), // Convert to number if necessary
comments: Number(comments), // Convert to number if necessary
});
}
} catch (error) {
console.error("Error fetching posts", error);
}
return dataresult;
return data_result;
}
/**
* @returns Array of all Users in the Database
*/
async function getAllUsersFromDB() {
const accounts_list: Array<Accounts> = [];
try {
const rows = await db.query("SELECT * FROM accounts");
for (const row of rows) {
const [
user_id,
user_group,
bio,
displayname,
username,
user_email,
password,
firstname,
surname,
account_created,
blocked_users,
followers,
following,
contacts,
] = row;
accounts_list.push({
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),
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),
});
}
} catch (error) {
console.error("Error fetching users", error);
}
return accounts_list;
}
// Test Function, not useful
// Promise needed because of "await"
async function countPosts(): Promise<number> {
let count = 0;
try {
for (const [c] of await db.query("SELECT COUNT(*) FROM posts")) {
count = c as number;
count = c as number;
}
} catch (error) {
console.error("Error counting posts:", error);
@@ -58,60 +139,36 @@ async function countPosts(): Promise<number> {
return count;
}
function getCommentsForPost(postid: bigint) {
/**
* @param post_id The ID of the Post to get the Comments for
* @returns Array of Comments for the Post, or an empty Array if there are no Comments
*/
function getCommentsForPost(post_id: number) {
}
function getCommentsForComments(commentid: bigint) {
/**
* @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) {
}
function getAllUsers() {
const users = [];
for (
const [
user_id,
user_group,
bio,
displayname,
username,
user_email,
password,
firstname,
surname,
account_created,
blocked_users,
followers,
following,
contacts,
] of db.query("SELECT * FROM Accounts")
) {
users.push({
user_id,
user_group,
bio,
displayname,
username,
user_email,
password,
firstname,
surname,
account_created,
blocked_users,
followers,
following,
contacts,
});
}
return users;
/**
* @param user_id The ID of the User to get
* @returns The User with the given ID, or null if the User doesn't exist
*/
function getUserInfoByID(user_id: number) {
}
function getUserByID(userid: bigint) {
}
function getAllPostsFromUser() {
/**
* @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
*/
function getAllPostsFromUser(user_id: number) {
}
// Filter Functions
function filterForImagePosts() {
function filterForImagePosts(posts_to_filter: Array<any>) {
return [];
}
@@ -124,15 +181,15 @@ function filterForTextPosts() {
}
// Export all Functions to make this a module
export {
getPostsFromDB,
export {
countPosts,
getCommentsForPost,
getCommentsForComments,
getAllUsers,
getUserByID,
getAllPostsFromUser,
filterForImagePosts,
filterForTextPosts,
filterForVideoPosts,
filterForTextPosts
getAllPostsFromUser,
getAllUsersFromDB,
getCommentsForComments,
getCommentsForPost,
getPostsFromDB,
getUserInfoByID,
};