From 8620ccb167731ed093397cac34ced9c159fab877 Mon Sep 17 00:00:00 2001 From: Mathis Date: Thu, 23 May 2024 15:51:41 +0200 Subject: [PATCH] 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. --- src/services/databases/databases.service.ts | 52 ++++++++++++++------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/src/services/databases/databases.service.ts b/src/services/databases/databases.service.ts index e1733d9..9395d7a 100644 --- a/src/services/databases/databases.service.ts +++ b/src/services/databases/databases.service.ts @@ -206,7 +206,7 @@ export class DatabasesService { return false; } } - + //ToTest async createPost(data: IPost): Promise { try { const result = await this.mongo.use().collection("posts").insertOne(data); @@ -219,7 +219,7 @@ export class DatabasesService { return false; } } - + //ToTest async getPostById(id: string): Promise { try { const result = (await this.mongo @@ -233,7 +233,7 @@ export class DatabasesService { return false; } } - + //ToTest async getPostsByUserId(userId: string): Promise { try { const result = (await this.mongo @@ -247,7 +247,7 @@ export class DatabasesService { return []; } } - + //ToTest async createComment(data: IComment): Promise { try { const result = await this.mongo @@ -263,7 +263,7 @@ export class DatabasesService { return false; } } - + //ToTest async getCommentsByPostId(postId: string): Promise { try { const result = (await this.mongo @@ -277,7 +277,7 @@ export class DatabasesService { return []; } } - + //ToTest async getPostsWithCommentsByUserId( userId: string, ): Promise> { @@ -291,7 +291,13 @@ export class DatabasesService { post, 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; } catch (err) { @@ -302,7 +308,7 @@ export class DatabasesService { return []; } } - + //ToTest async deleteComment(commentId: string): Promise { try { const result = await this.mongo @@ -319,7 +325,7 @@ export class DatabasesService { return false; } } - + //ToTest async deletePost(postId: string): Promise { try { const result = await this.mongo @@ -336,7 +342,7 @@ export class DatabasesService { return false; } } - + //ToTest async getFollowersCountById(id: string) { const result = (await this.maria.execute( "SELECT COUNT(*) FROM follows WHERE target_id = ?", @@ -348,7 +354,7 @@ export class DatabasesService { ); return result; } - + //ToTest async getFollowingCountById(id: string) { const result = (await this.maria.execute( "SELECT COUNT(*) AS count FROM follows WHERE source_id = ?", @@ -360,15 +366,27 @@ export class DatabasesService { ); return result?.[0]?.count || 0; } - + //ToTest async getMostFollowedUser() { - const _sql = "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 }>; + const _sql = + "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( - "Fetching most followed users from database...", - `${result?.length} user(s) found.`, - ); + "Fetching most followed users from database...", + `${result?.length} user(s) found.`, + ); + return result; + } + async updatePost(data: IPost) { + //MongoDB + await this.mongo + .use() + .collection("posts") + .updateOne({ id: data.id }, { $set: data }); } }