forked from houdunwang/v2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa759ea
commit 118703c
Showing
47 changed files
with
822 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import { comment } from './seed/comment' | ||
import { soft } from './seed/soft' | ||
import user from './seed/user' | ||
|
||
async function run() { | ||
await user() | ||
await soft() | ||
await comment() | ||
} | ||
|
||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Body, Controller, Delete, Get, Param, Post, UseGuards } from '@nestjs/common' | ||
import { User } from '@prisma/client' | ||
import { Auth } from 'src/auth/auth.decorator' | ||
import { CurrentUser } from 'src/auth/current-user.decorator' | ||
import { PrismaService } from 'src/common/prisma.service' | ||
import { Policy } from 'src/policy/policy.decorator' | ||
import { PolicyGuard } from 'src/policy/policy.guard' | ||
import { CommentPolicy } from './comment.policy' | ||
import { CommentResponse } from './comment.response' | ||
import { CommentService } from './comment.service' | ||
import { CreateCommentDto } from './dto/create-comment.dto' | ||
import { Throttle } from '@nestjs/throttler' | ||
|
||
@Controller('comment/:sid') | ||
export class CommentController { | ||
constructor(private readonly commentService: CommentService, private prisma: PrismaService) {} | ||
|
||
@Post() | ||
@Auth() | ||
@Throttle(190, 20) | ||
async create(@Body() createCommentDto: CreateCommentDto, @CurrentUser() user: User, @Param('sid') sid: number) { | ||
const comment = await this.commentService.create(createCommentDto, user, sid) | ||
return new CommentResponse(comment).make() | ||
} | ||
|
||
@Get() | ||
async findAll(@Param('sid') sid: number) { | ||
const comments = await this.commentService.findAll(+sid) | ||
return comments.map((comment) => { | ||
return new CommentResponse(comment).make() | ||
}) | ||
} | ||
|
||
@Delete(':id') | ||
//守卫:执行验证 | ||
@UseGuards(PolicyGuard) | ||
//设置元数据,提供给守卫 | ||
@Policy(CommentPolicy) | ||
//获取身份、登录 | ||
@Auth() | ||
async remove(@Param('id') id: string, @CurrentUser() user: User) { | ||
return this.commentService.remove(+id) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { CommentService } from './comment.service'; | ||
import { CommentController } from './comment.controller'; | ||
|
||
@Module({ | ||
controllers: [CommentController], | ||
providers: [CommentService] | ||
}) | ||
export class CommentModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Comment, User } from '@prisma/client' | ||
import { Request } from 'express' | ||
import { PrismaService } from 'src/common/prisma.service' | ||
import { IPolicy } from 'src/policy/policy.decorator' | ||
|
||
export class CommentPolicy implements IPolicy { | ||
constructor(private prisma: PrismaService, private request: Request) {} | ||
|
||
remove(model: Comment, user: User) { | ||
return model.userId == user.id | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Comment, User } from '@prisma/client' | ||
import { JsonResponse } from 'src/core/json.response' | ||
import { UserResponse } from 'src/user/user.response' | ||
|
||
export class CommentResponse extends JsonResponse<Comment & { user: User }> { | ||
// protected hidden: (keyof User)[] = ['password'] | ||
public make(): Comment & { user: User } { | ||
super.make() | ||
this.data.user = new UserResponse(this.data.user).make() | ||
// this.data.user = JsonResponse.handle(this.data.user, ['password', 'secret', 'mobile']) | ||
return this.data | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { CreateCommentDto } from './dto/create-comment.dto' | ||
import { UpdateCommentDto } from './dto/update-comment.dto' | ||
import { PrismaService } from 'src/common/prisma.service' | ||
import { User } from '@prisma/client' | ||
|
||
@Injectable() | ||
export class CommentService { | ||
constructor(private prisma: PrismaService) {} | ||
|
||
create(createCommentDto: CreateCommentDto, user: User, sid: number) { | ||
const { commentId, ...dto } = createCommentDto | ||
return this.prisma.comment.create({ | ||
data: { | ||
...dto, | ||
soft: { connect: { id: +sid } }, | ||
user: { connect: { id: user.id } }, | ||
reply: commentId && { connect: { id: +commentId } }, | ||
}, | ||
include: { | ||
replys: true, | ||
user: true, | ||
}, | ||
}) | ||
} | ||
|
||
findAll(sid: number) { | ||
return this.prisma.comment.findMany({ | ||
where: { | ||
softId: sid, | ||
}, | ||
include: { user: true, replys: true }, | ||
}) | ||
} | ||
|
||
remove(id: number) { | ||
return this.prisma.comment.deleteMany({ where: { id } }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Allow, IsNotEmpty, Length } from 'class-validator' | ||
|
||
export class CreateCommentDto { | ||
@IsNotEmpty({ message: '评论内容不能为空' }) | ||
@Length(10, 1000, { message: '评论内容不能少于10个字' }) | ||
content: string | ||
|
||
@Allow() | ||
commentId?: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { PartialType } from '@nestjs/mapped-types'; | ||
import { CreateCommentDto } from './create-comment.dto'; | ||
|
||
export class UpdateCommentDto extends PartialType(CreateCommentDto) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export class Comment {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { SetMetadata } from '@nestjs/common' | ||
|
||
export interface IPolicy {} | ||
export const POLICY_KEY = 'policy_key' | ||
|
||
export const Policy = (policy: IPolicy, action?: string) => SetMetadata(POLICY_KEY, { policy, action }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common' | ||
import { Reflector } from '@nestjs/core' | ||
import { Observable } from 'rxjs' | ||
import { POLICY_KEY } from './policy.decorator' | ||
import { PrismaService } from 'src/common/prisma.service' | ||
import { Request } from 'express' | ||
import { User } from '@prisma/client' | ||
|
||
@Injectable() | ||
export class PolicyGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector, private readonly prisma: PrismaService) {} | ||
|
||
async canActivate(context: ExecutionContext): Promise<boolean> { | ||
const { policy, action } = this.reflector.get<any>(POLICY_KEY, context.getHandler()) | ||
const controller = context.getClass().name.replace('Controller', '') | ||
const method = context.getHandler().name | ||
const request = context.switchToHttp().getRequest() as Request | ||
const user = request.user as User | ||
//超管放行 | ||
if (user?.id == 1) return true | ||
const model = await this.prisma[controller].findUnique({ | ||
where: { id: +(request.params.id || 0) }, | ||
}) | ||
const policyInstance = new policy(this.prisma, request) | ||
return policyInstance[action || method](model, user) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
@Module({}) | ||
export class PolicyModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.