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;
}
}
//ToTest
async createPost(data: IPost): Promise<string | boolean> {
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<IPost | false> {
try {
const result = (await this.mongo
@ -233,7 +233,7 @@ export class DatabasesService {
return false;
}
}
//ToTest
async getPostsByUserId(userId: string): Promise<IPost[]> {
try {
const result = (await this.mongo
@ -247,7 +247,7 @@ export class DatabasesService {
return [];
}
}
//ToTest
async createComment(data: IComment): Promise<string | boolean> {
try {
const result = await this.mongo
@ -263,7 +263,7 @@ export class DatabasesService {
return false;
}
}
//ToTest
async getCommentsByPostId(postId: string): Promise<IComment[]> {
try {
const result = (await this.mongo
@ -277,7 +277,7 @@ export class DatabasesService {
return [];
}
}
//ToTest
async getPostsWithCommentsByUserId(
userId: string,
): Promise<Array<{ post: IPost; comments: IComment[] | undefined }>> {
@ -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<boolean> {
try {
const result = await this.mongo
@ -319,7 +325,7 @@ export class DatabasesService {
return false;
}
}
//ToTest
async deletePost(postId: string): Promise<boolean> {
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.`,
);
return result;
}
async updatePost(data: IPost) {
//MongoDB
await this.mongo
.use()
.collection("posts")
.updateOne({ id: data.id }, { $set: data });
}
}