lint
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import {
|
||||
BadRequestException,
|
||||
Body,
|
||||
Controller,
|
||||
Delete,
|
||||
@@ -11,14 +10,11 @@ import {
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '@nestjs/passport';
|
||||
import { UpdatePasswordDto } from 'src/admin/dto/admin-user/update-password.dto';
|
||||
import { CreateBlogDto } from 'src/admin/dto/admin-web/create-blog.dto';
|
||||
import { SetBlogPasswordDto } from 'src/admin/dto/admin-web/set-blog-password.dto';
|
||||
import { UpdateBlogDto } from 'src/admin/dto/admin-web/update-blog.dto';
|
||||
import { Role } from 'src/auth/role.enum';
|
||||
import { BlogPermission } from 'src/blog/Blog.Permission.enum';
|
||||
import { BlogService } from 'src/blog/blog.service';
|
||||
import { Blog } from 'src/blog/entity/Blog.entity';
|
||||
import { Roles } from 'src/common/decorators/role.decorator';
|
||||
import { RolesGuard } from 'src/common/guard/roles.guard';
|
||||
|
||||
@@ -26,7 +22,7 @@ import { RolesGuard } from 'src/common/guard/roles.guard';
|
||||
@UseGuards(AuthGuard('jwt'), RolesGuard)
|
||||
@Roles(Role.Admin)
|
||||
export class AdminWebBlogController {
|
||||
constructor(private readonly adminWebBlogService: BlogService) { }
|
||||
constructor(private readonly adminWebBlogService: BlogService) {}
|
||||
|
||||
@Get()
|
||||
async list() {
|
||||
|
||||
@@ -15,5 +15,5 @@ export class CreateBlogDto {
|
||||
permissions: BlogPermission[];
|
||||
|
||||
@IsString()
|
||||
password: string;// 允许空串
|
||||
password: string; // 允许空串
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { IsString } from "class-validator";
|
||||
import { IsString } from 'class-validator';
|
||||
|
||||
export class SetBlogPasswordDto {
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
@IsString()
|
||||
password: string;
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ export class BlogController {
|
||||
constructor(
|
||||
private readonly blogService: BlogService,
|
||||
private readonly userService: UserService,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
@Get()
|
||||
getBlogs() {
|
||||
@@ -43,7 +43,10 @@ export class BlogController {
|
||||
throw new BadRequestException('文章不存在或无权限访问');
|
||||
} else {
|
||||
// 判断密码是否正确
|
||||
if (!password || this.blogService.hashPassword(password) !== blog.password_hash) {
|
||||
if (
|
||||
!password ||
|
||||
this.blogService.hashPassword(password) !== blog.password_hash
|
||||
) {
|
||||
throw new BadRequestException('文章不存在或无权限访问');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
export enum BlogPermission {
|
||||
Public = 'Public',
|
||||
ByPassword = 'ByPassword',
|
||||
List = 'List',
|
||||
}
|
||||
Public = 'Public',
|
||||
ByPassword = 'ByPassword',
|
||||
List = 'List',
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Blog } from './entity/Blog.entity';
|
||||
import { ArrayContains, Repository } from 'typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
import { BlogComment } from './entity/BlogComment.entity';
|
||||
import { BlogPermission } from './Blog.Permission.enum';
|
||||
import { createHash } from 'crypto';
|
||||
@@ -13,18 +13,24 @@ export class BlogService {
|
||||
private readonly blogRepository: Repository<Blog>,
|
||||
@InjectRepository(BlogComment)
|
||||
private readonly blogCommentRepository: Repository<BlogComment>,
|
||||
) { }
|
||||
) {}
|
||||
|
||||
async list(option: {
|
||||
withAll?: boolean;
|
||||
} = {}) {
|
||||
return (await this.blogRepository.find({
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
}))
|
||||
.filter(i => option.withAll || i.permissions.includes(BlogPermission.List))
|
||||
.map(i => {
|
||||
async list(
|
||||
option: {
|
||||
withAll?: boolean;
|
||||
} = {},
|
||||
) {
|
||||
return (
|
||||
await this.blogRepository.find({
|
||||
order: {
|
||||
createdAt: 'DESC',
|
||||
},
|
||||
})
|
||||
)
|
||||
.filter(
|
||||
(i) => option.withAll || i.permissions.includes(BlogPermission.List),
|
||||
)
|
||||
.map((i) => {
|
||||
if (option.withAll) {
|
||||
return i;
|
||||
}
|
||||
@@ -36,7 +42,7 @@ export class BlogService {
|
||||
id,
|
||||
title,
|
||||
viewCount,
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -44,7 +50,9 @@ export class BlogService {
|
||||
const { password, ...blog } = dto;
|
||||
if (blog.permissions.includes(BlogPermission.ByPassword)) {
|
||||
if (password) {
|
||||
blog.password_hash = createHash('sha256').update(`${password}`).digest('hex');
|
||||
blog.password_hash = createHash('sha256')
|
||||
.update(`${password}`)
|
||||
.digest('hex');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,10 +66,14 @@ export class BlogService {
|
||||
throw new Error('博客不存在');
|
||||
}
|
||||
|
||||
return (await this.blogRepository.update(id, {
|
||||
...blog,
|
||||
password_hash: this.hashPassword(password),
|
||||
})).affected > 0;
|
||||
return (
|
||||
(
|
||||
await this.blogRepository.update(id, {
|
||||
...blog,
|
||||
password_hash: this.hashPassword(password),
|
||||
})
|
||||
).affected > 0
|
||||
);
|
||||
}
|
||||
|
||||
async update(id: string, blog: Partial<Blog>) {
|
||||
|
||||
Reference in New Issue
Block a user