实现blog后端api
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { Controller, Get } from '@nestjs/common';
|
import { BadRequestException, Controller, Get, Param, ParseUUIDPipe } from '@nestjs/common';
|
||||||
import { BlogService } from './blog.service';
|
import { BlogService } from './blog.service';
|
||||||
|
|
||||||
@Controller('blog')
|
@Controller('blog')
|
||||||
@@ -12,4 +12,22 @@ export class BlogController {
|
|||||||
getBlogs() {
|
getBlogs() {
|
||||||
return this.blogService.list();
|
return this.blogService.list();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get(':id')
|
||||||
|
async getBlog(
|
||||||
|
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
|
||||||
|
) {
|
||||||
|
const blog = await this.blogService.findById(id);
|
||||||
|
if (!blog) throw new BadRequestException('文章不存在');
|
||||||
|
|
||||||
|
const blogDataRes = await fetch(`${blog.contentUrl}`);
|
||||||
|
const blogContent = await blogDataRes.text();
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: blog.id,
|
||||||
|
title: blog.title,
|
||||||
|
createdAt: blog.createdAt,
|
||||||
|
content: blogContent,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ export class Blog {
|
|||||||
@Column()
|
@Column()
|
||||||
contentUrl: string;
|
contentUrl: string;
|
||||||
|
|
||||||
|
@Column({ default: 0 })
|
||||||
|
viewCount: number;
|
||||||
|
|
||||||
@CreateDateColumn({ precision: 3 })
|
@CreateDateColumn({ precision: 3 })
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user