实现blog后端api

This commit is contained in:
2025-05-18 14:30:18 +08:00
parent 327f49bff0
commit 3f0e281d42
2 changed files with 22 additions and 1 deletions

View File

@@ -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,
};
}
}