32 أسطر
998 B
TypeScript
32 أسطر
998 B
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectModel } from '@nestjs/mongoose';
|
|
import { Model, Types } from 'mongoose';
|
|
import { Like, LikeDocument } from './schemas/like.schema';
|
|
|
|
@Injectable()
|
|
export class LikesRepository {
|
|
constructor(@InjectModel(Like.name) private readonly likeModel: Model<LikeDocument>) {}
|
|
|
|
async findOne(userId: string, targetId: string, targetType: 'post' | 'comment'): Promise<LikeDocument | null> {
|
|
return this.likeModel
|
|
.findOne({
|
|
userId: new Types.ObjectId(userId),
|
|
targetId: new Types.ObjectId(targetId),
|
|
targetType,
|
|
})
|
|
.exec();
|
|
}
|
|
|
|
async create(userId: string, targetId: string, targetType: 'post' | 'comment'): Promise<LikeDocument> {
|
|
return this.likeModel.create({
|
|
userId: new Types.ObjectId(userId),
|
|
targetId: new Types.ObjectId(targetId),
|
|
targetType,
|
|
});
|
|
}
|
|
|
|
async deleteById(id: string): Promise<void> {
|
|
await this.likeModel.findByIdAndDelete(id).exec();
|
|
}
|
|
}
|