初步完成评论

This commit is contained in:
2025-06-07 03:21:27 +08:00
parent 11add3c1fa
commit 96316e3d51
12 changed files with 208 additions and 4 deletions

View File

@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Blog } from './entity/Blog.entity';
import { Repository } from 'typeorm';
import { BlogComment } from './entity/BlogComment';
@Injectable()
export class BlogService {
@@ -9,6 +10,8 @@ export class BlogService {
constructor(
@InjectRepository(Blog)
private readonly blogRepository: Repository<Blog>,
@InjectRepository(BlogComment)
private readonly blogCommentRepository: Repository<BlogComment>,
) { }
async list() {
@@ -43,4 +46,19 @@ export class BlogService {
async incrementViewCount(id: string) {
await this.blogRepository.increment({ id }, 'viewCount', 1);
}
async getComments(id: string) {
return this.blogCommentRepository.find({
where: { blogId: id },
relations: ['user'],
order: {
createdAt: 'DESC',
}
});
}
async createComment(comment: Partial<BlogComment>) {
const newComment = this.blogCommentRepository.create(comment);
return this.blogCommentRepository.save(newComment);
}
}