import { Injectable } from "@nestjs/common"; import { and, eq, sql } from "drizzle-orm"; import { DatabaseService } from "../../database/database.service"; import { commentLikes } from "../../database/schemas/comment_likes"; @Injectable() export class CommentLikesRepository { constructor(private readonly databaseService: DatabaseService) {} async addLike(commentId: string, userId: string) { await this.databaseService.db .insert(commentLikes) .values({ commentId, userId }) .onConflictDoNothing(); } async removeLike(commentId: string, userId: string) { await this.databaseService.db .delete(commentLikes) .where( and(eq(commentLikes.commentId, commentId), eq(commentLikes.userId, userId)), ); } async countByCommentId(commentId: string) { const results = await this.databaseService.db .select({ count: sql`count(*)` }) .from(commentLikes) .where(eq(commentLikes.commentId, commentId)); return Number(results[0]?.count || 0); } async isLikedByUser(commentId: string, userId: string) { const results = await this.databaseService.db .select() .from(commentLikes) .where( and(eq(commentLikes.commentId, commentId), eq(commentLikes.userId, userId)), ); return !!results[0]; } }