完成剩余需求
This commit is contained in:
@@ -30,7 +30,9 @@ export class AdminWebBlogController {
|
|||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
async list() {
|
async list() {
|
||||||
return this.adminWebBlogService.list();
|
return this.adminWebBlogService.list({
|
||||||
|
withAll: true,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
|||||||
@@ -14,13 +14,14 @@ import { OptionalAuthGuard } from 'src/auth/strategies/OptionalAuthGuard';
|
|||||||
import { UserService } from 'src/user/user.service';
|
import { UserService } from 'src/user/user.service';
|
||||||
import { createBlogCommentDto } from './dto/create.blogcomment.dto';
|
import { createBlogCommentDto } from './dto/create.blogcomment.dto';
|
||||||
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
|
import { Throttle, ThrottlerGuard } from '@nestjs/throttler';
|
||||||
|
import { BlogPermission } from './Blog.Permission.enum';
|
||||||
|
|
||||||
@Controller('blog')
|
@Controller('blog')
|
||||||
export class BlogController {
|
export class BlogController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly blogService: BlogService,
|
private readonly blogService: BlogService,
|
||||||
private readonly userService: UserService,
|
private readonly userService: UserService,
|
||||||
) {}
|
) { }
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
getBlogs() {
|
getBlogs() {
|
||||||
@@ -28,9 +29,24 @@ export class BlogController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
async getBlog(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
|
async getBlog(
|
||||||
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||||
|
@Param('p') password: string,
|
||||||
|
) {
|
||||||
const blog = await this.blogService.findById(id);
|
const blog = await this.blogService.findById(id);
|
||||||
if (!blog) throw new BadRequestException('文章不存在');
|
if (!blog) throw new BadRequestException('文章不存在或无权限访问');
|
||||||
|
|
||||||
|
if (!blog.permissions.includes(BlogPermission.Public)) {
|
||||||
|
// 无公开权限,则进一步检查是否有密码保护
|
||||||
|
if (blog.permissions.includes(BlogPermission.ByPassword)) {
|
||||||
|
throw new BadRequestException('文章不存在或无权限访问');
|
||||||
|
} else {
|
||||||
|
// 判断密码是否正确
|
||||||
|
if (!password || this.blogService.hashPassword(password) !== blog.password_hash) {
|
||||||
|
throw new BadRequestException('文章不存在或无权限访问');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const blogDataRes = await fetch(`${blog.contentUrl}`);
|
const blogDataRes = await fetch(`${blog.contentUrl}`);
|
||||||
const blogContent = await blogDataRes.text();
|
const blogContent = await blogDataRes.text();
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { Blog } from './entity/Blog.entity';
|
import { Blog } from './entity/Blog.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { ArrayContains, Repository } from 'typeorm';
|
||||||
import { BlogComment } from './entity/BlogComment.entity';
|
import { BlogComment } from './entity/BlogComment.entity';
|
||||||
import { BlogPermission } from './Blog.Permission.enum';
|
import { BlogPermission } from './Blog.Permission.enum';
|
||||||
import { createHash } from 'crypto';
|
import { createHash } from 'crypto';
|
||||||
@@ -15,12 +15,28 @@ export class BlogService {
|
|||||||
private readonly blogCommentRepository: Repository<BlogComment>,
|
private readonly blogCommentRepository: Repository<BlogComment>,
|
||||||
) { }
|
) { }
|
||||||
|
|
||||||
async list() {
|
async list(option: {
|
||||||
return this.blogRepository.find({
|
withAll?: boolean;
|
||||||
where: { deletedAt: null },
|
} = {}) {
|
||||||
|
return (await this.blogRepository.find({
|
||||||
order: {
|
order: {
|
||||||
createdAt: 'DESC',
|
createdAt: 'DESC',
|
||||||
},
|
},
|
||||||
|
}))
|
||||||
|
.filter(i => option.withAll || i.permissions.includes(BlogPermission.List))
|
||||||
|
.map(i => {
|
||||||
|
if (option.withAll) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
const { createdAt, deletedAt, id, title, viewCount } = i;
|
||||||
|
return {
|
||||||
|
createdAt,
|
||||||
|
deletedAt,
|
||||||
|
id,
|
||||||
|
title,
|
||||||
|
viewCount,
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,7 +60,7 @@ export class BlogService {
|
|||||||
|
|
||||||
return (await this.blogRepository.update(id, {
|
return (await this.blogRepository.update(id, {
|
||||||
...blog,
|
...blog,
|
||||||
password_hash: createHash('sha256').update(`${password}`).digest('hex'),
|
password_hash: this.hashPassword(password),
|
||||||
})).affected > 0;
|
})).affected > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +76,7 @@ export class BlogService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findById(id: string) {
|
async findById(id: string) {
|
||||||
return this.blogRepository.findOneBy({ id });
|
return await this.blogRepository.findOneBy({ id });
|
||||||
}
|
}
|
||||||
|
|
||||||
async incrementViewCount(id: string) {
|
async incrementViewCount(id: string) {
|
||||||
@@ -86,4 +102,8 @@ export class BlogService {
|
|||||||
const newComment = this.blogCommentRepository.create(comment);
|
const newComment = this.blogCommentRepository.create(comment);
|
||||||
return this.blogCommentRepository.save(newComment);
|
return this.blogCommentRepository.save(newComment);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hashPassword(password: string) {
|
||||||
|
return createHash('sha256').update(`${password}`).digest('hex');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import { BlogComment } from './BlogComment.entity';
|
import { BlogComment } from './BlogComment.entity';
|
||||||
import { BlogPermission } from '../Blog.Permission.enum';
|
import { BlogPermission } from '../Blog.Permission.enum';
|
||||||
|
|
||||||
|
/** @todo 考虑后续将权限的数据类型替换为json,以提高查询效率 */
|
||||||
@Entity()
|
@Entity()
|
||||||
export class Blog {
|
export class Blog {
|
||||||
@PrimaryGeneratedColumn('uuid')
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
|||||||
Reference in New Issue
Block a user