docs: Add 'ToTest' annotations and extend logging in database service

This commit introduces 'ToTest' annotations to all methods in the database service. Furthermore, it extends the logging for specific functions, and includes an updatePost method for MongoDB interactions. These changes aim to improve code readability, debugging capabilities, and our database interactions.
This commit is contained in:
Mathis H (Avnyr) 2024-05-23 15:51:41 +02:00
parent 8e14bf77b4
commit 8620ccb167
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -206,7 +206,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async createPost(data: IPost): Promise<string | boolean> { async createPost(data: IPost): Promise<string | boolean> {
try { try {
const result = await this.mongo.use().collection("posts").insertOne(data); const result = await this.mongo.use().collection("posts").insertOne(data);
@ -219,7 +219,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async getPostById(id: string): Promise<IPost | false> { async getPostById(id: string): Promise<IPost | false> {
try { try {
const result = (await this.mongo const result = (await this.mongo
@ -233,7 +233,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async getPostsByUserId(userId: string): Promise<IPost[]> { async getPostsByUserId(userId: string): Promise<IPost[]> {
try { try {
const result = (await this.mongo const result = (await this.mongo
@ -247,7 +247,7 @@ export class DatabasesService {
return []; return [];
} }
} }
//ToTest
async createComment(data: IComment): Promise<string | boolean> { async createComment(data: IComment): Promise<string | boolean> {
try { try {
const result = await this.mongo const result = await this.mongo
@ -263,7 +263,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async getCommentsByPostId(postId: string): Promise<IComment[]> { async getCommentsByPostId(postId: string): Promise<IComment[]> {
try { try {
const result = (await this.mongo const result = (await this.mongo
@ -277,7 +277,7 @@ export class DatabasesService {
return []; return [];
} }
} }
//ToTest
async getPostsWithCommentsByUserId( async getPostsWithCommentsByUserId(
userId: string, userId: string,
): Promise<Array<{ post: IPost; comments: IComment[] | undefined }>> { ): Promise<Array<{ post: IPost; comments: IComment[] | undefined }>> {
@ -291,7 +291,13 @@ export class DatabasesService {
post, post,
comments: comments[index], comments: comments[index],
})); }));
//Log the result this.logs.debug(
"Posts with comments by user id:",
result.map((item) => ({
postId: item.post.id,
commentsCount: item.comments?.length || 0,
})),
);
return result; return result;
} catch (err) { } catch (err) {
@ -302,7 +308,7 @@ export class DatabasesService {
return []; return [];
} }
} }
//ToTest
async deleteComment(commentId: string): Promise<boolean> { async deleteComment(commentId: string): Promise<boolean> {
try { try {
const result = await this.mongo const result = await this.mongo
@ -319,7 +325,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async deletePost(postId: string): Promise<boolean> { async deletePost(postId: string): Promise<boolean> {
try { try {
const result = await this.mongo const result = await this.mongo
@ -336,7 +342,7 @@ export class DatabasesService {
return false; return false;
} }
} }
//ToTest
async getFollowersCountById(id: string) { async getFollowersCountById(id: string) {
const result = (await this.maria.execute( const result = (await this.maria.execute(
"SELECT COUNT(*) FROM follows WHERE target_id = ?", "SELECT COUNT(*) FROM follows WHERE target_id = ?",
@ -348,7 +354,7 @@ export class DatabasesService {
); );
return result; return result;
} }
//ToTest
async getFollowingCountById(id: string) { async getFollowingCountById(id: string) {
const result = (await this.maria.execute( const result = (await this.maria.execute(
"SELECT COUNT(*) AS count FROM follows WHERE source_id = ?", "SELECT COUNT(*) AS count FROM follows WHERE source_id = ?",
@ -360,15 +366,27 @@ export class DatabasesService {
); );
return result?.[0]?.count || 0; return result?.[0]?.count || 0;
} }
//ToTest
async getMostFollowedUser() { async getMostFollowedUser() {
const _sql = "SELECT target_id, COUNT(*) AS count FROM follows GROUP BY target_id ORDER BY count DESC LIMIT 3"; const _sql =
const result = (await this.maria.query(_sql)) as unknown as Array<{ target_id: string; count: number }>; "SELECT target_id, COUNT(*) AS count FROM follows GROUP BY target_id ORDER BY count DESC LIMIT 3";
const result = (await this.maria.query(_sql)) as unknown as Array<{
target_id: string;
count: number;
}>;
this.logs.debug( this.logs.debug(
"Fetching most followed users from database...", "Fetching most followed users from database...",
`${result?.length} user(s) found.`, `${result?.length} user(s) found.`,
); );
return result;
}
async updatePost(data: IPost) {
//MongoDB
await this.mongo
.use()
.collection("posts")
.updateOne({ id: data.id }, { $set: data });
} }
} }