实现博客评论
This commit is contained in:
@@ -9,6 +9,7 @@ import { PassportModule } from '@nestjs/passport';
|
||||
import { JwtStrategy } from './strategies/jwt.strategy';
|
||||
import { ConfigModule, ConfigService } from '@nestjs/config';
|
||||
import { VerificationModule } from 'src/verification/verification.module';
|
||||
import { OptionalAuthGuard } from './strategies/OptionalAuthGuard';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
@@ -32,11 +33,13 @@ import { VerificationModule } from 'src/verification/verification.module';
|
||||
providers: [
|
||||
AuthService,
|
||||
JwtStrategy,
|
||||
OptionalAuthGuard,
|
||||
],
|
||||
exports: [
|
||||
PassportModule,
|
||||
JwtStrategy,
|
||||
AuthService,
|
||||
OptionalAuthGuard,
|
||||
]
|
||||
})
|
||||
export class AuthModule { }
|
||||
|
||||
22
tone-page-server/src/auth/strategies/OptionalAuthGuard.ts
Normal file
22
tone-page-server/src/auth/strategies/OptionalAuthGuard.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from "@nestjs/common";
|
||||
import { AuthGuard } from "@nestjs/passport";
|
||||
import { Observable, retry } from "rxjs";
|
||||
|
||||
@Injectable()
|
||||
export class OptionalAuthGuard extends AuthGuard('jwt') implements CanActivate {
|
||||
async canActivate(context: ExecutionContext): Promise<boolean> {
|
||||
try {
|
||||
await super.canActivate(context);
|
||||
return true;
|
||||
} catch (error) {
|
||||
return true;// 如果验证失败,仍然允许访问
|
||||
}
|
||||
}
|
||||
|
||||
handleRequest<TUser = any>(err: any, user: any, info: any, context: ExecutionContext, status?: any): TUser {
|
||||
if (err || !user) {
|
||||
return null; // 如果没有用户信息,返回null
|
||||
}
|
||||
return user; // 如果有用户信息,返回用户对象
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
import { BadRequestException, Body, Controller, Get, Param, ParseUUIDPipe, Post } from '@nestjs/common';
|
||||
import { BadRequestException, Body, Controller, Get, Param, ParseUUIDPipe, Post, Req, Request, UseGuards } from '@nestjs/common';
|
||||
import { BlogService } from './blog.service';
|
||||
import { OptionalAuthGuard } from 'src/auth/strategies/OptionalAuthGuard';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
import { createBlogCommentDto } from './dto/create.blogcomment.dto';
|
||||
|
||||
@Controller('blog')
|
||||
export class BlogController {
|
||||
|
||||
constructor(
|
||||
private readonly blogService: BlogService,
|
||||
private readonly userService: UserService,
|
||||
) { }
|
||||
|
||||
@Get()
|
||||
@@ -42,18 +46,24 @@ export class BlogController {
|
||||
return await this.blogService.getComments(id);
|
||||
}
|
||||
|
||||
// TODO:鉴权,该接口允许匿名评论,但仍需验证userId合法性
|
||||
// 该接口允许匿名评论,但仍需验证userId合法性
|
||||
@UseGuards(OptionalAuthGuard)
|
||||
@Post(':id/comment')
|
||||
async createBlogComment(
|
||||
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||
@Body() commentData: { content: string },
|
||||
@Body() commentData: createBlogCommentDto,
|
||||
@Request() req,
|
||||
) {
|
||||
const { userId } = req.user || {};
|
||||
const blog = await this.blogService.findById(id);
|
||||
if (!blog) throw new BadRequestException('文章不存在');
|
||||
|
||||
let user = userId ? await this.userService.findOne({ userId }) : null;
|
||||
|
||||
const comment = {
|
||||
...commentData,
|
||||
blogId: id,
|
||||
user: user,
|
||||
};
|
||||
|
||||
return await this.blogService.createComment(comment);
|
||||
|
||||
@@ -4,9 +4,11 @@ import { BlogService } from './blog.service';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
import { Blog } from './entity/Blog.entity';
|
||||
import { BlogComment } from './entity/BlogComment';
|
||||
import { AuthModule } from 'src/auth/auth.module';
|
||||
import { UserModule } from 'src/user/user.module';
|
||||
|
||||
@Module({
|
||||
imports: [TypeOrmModule.forFeature([Blog, BlogComment])],
|
||||
imports: [TypeOrmModule.forFeature([Blog, BlogComment]), AuthModule, UserModule],
|
||||
controllers: [BlogController],
|
||||
providers: [BlogService],
|
||||
exports: [BlogService],
|
||||
|
||||
@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Blog } from './entity/Blog.entity';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BlogComment } from './entity/BlogComment';
|
||||
import { UserService } from 'src/user/user.service';
|
||||
|
||||
@Injectable()
|
||||
export class BlogService {
|
||||
|
||||
10
tone-page-server/src/blog/dto/create.blogcomment.dto.ts
Normal file
10
tone-page-server/src/blog/dto/create.blogcomment.dto.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { IsOptional, IsString, IsUUID } from "class-validator";
|
||||
|
||||
export class createBlogCommentDto {
|
||||
@IsString({ message: '评论内容不能为空' })
|
||||
content: string;
|
||||
|
||||
@IsOptional()
|
||||
@IsUUID('4', { message: '父评论ID格式错误' })
|
||||
parentId?: string;
|
||||
}
|
||||
Reference in New Issue
Block a user