实现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';
|
||||
|
||||
@Controller('blog')
|
||||
@@ -12,4 +12,22 @@ export class BlogController {
|
||||
getBlogs() {
|
||||
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,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user