format + lint

This commit is contained in:
2025-06-14 14:12:18 +08:00
parent e777afc433
commit 90a67b681e
69 changed files with 1756 additions and 1583 deletions

View File

@@ -1,4 +1,14 @@
import { BadRequestException, Body, Controller, Get, Param, ParseUUIDPipe, Post, Req, Request, UseGuards } from '@nestjs/common';
import {
BadRequestException,
Body,
Controller,
Get,
Param,
ParseUUIDPipe,
Post,
Request,
UseGuards,
} from '@nestjs/common';
import { BlogService } from './blog.service';
import { OptionalAuthGuard } from 'src/auth/strategies/OptionalAuthGuard';
import { UserService } from 'src/user/user.service';
@@ -6,86 +16,91 @@ import { createBlogCommentDto } from './dto/create.blogcomment.dto';
@Controller('blog')
export class BlogController {
constructor(
private readonly blogService: BlogService,
private readonly userService: UserService,
) {}
constructor(
private readonly blogService: BlogService,
private readonly userService: UserService,
) { }
@Get()
getBlogs() {
return this.blogService.list();
}
@Get()
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('文章不存在');
@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();
const blogDataRes = await fetch(`${blog.contentUrl}`);
const blogContent = await blogDataRes.text();
await this.blogService.incrementViewCount(id);
return {
id: blog.id,
title: blog.title,
createdAt: blog.createdAt,
content: blogContent,
};
}
await this.blogService.incrementViewCount(id);
return {
id: blog.id,
title: blog.title,
createdAt: blog.createdAt,
content: blogContent,
};
}
@Get(':id/comments')
async getBlogComments(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
) {
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
@Get(':id/comments')
async getBlogComments(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
) {
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
return await this.blogService.getComments(id);
}
return await this.blogService.getComments(id);
}
// 该接口允许匿名评论但仍需验证userId合法性
@UseGuards(OptionalAuthGuard)
@Post(':id/comment')
async createBlogComment(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
@Body() commentData: createBlogCommentDto,
@Request() req,
) {
const { userId } = req.user || {};
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
// 该接口允许匿名评论但仍需验证userId合法性
@UseGuards(OptionalAuthGuard)
@Post(':id/comment')
async createBlogComment(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
@Body() commentData: createBlogCommentDto,
@Request() req,
) {
const { userId } = req.user || {};
const blog = await this.blogService.findById(id);
if (!blog) throw new BadRequestException('文章不存在');
const user = userId ? await this.userService.findOne({ userId }) : null;
let user = userId ? await this.userService.findOne({ userId }) : null;
// 获取IP归属地
const ip = req.ip || req.socket.remoteAddress || req.headers['x-forwarded-for'] || '未知';
let address = '未知';
if (!['::1'].includes(ip)) {
const addressRes = await (await fetch(`https://mesh.if.iqiyi.com/aid/ip/info?version=1.1.1&ip=${ip}`)).json();
if (addressRes?.code == 0) {
const country: string = addressRes?.data?.countryCN || '未知';
const province: string = addressRes?.data?.provinceCN || '中国';
if (country !== '中国') {
// 非中国,显示国家
address = country;
} else {
// 中国,显示省份
address = province;
}
}
// 获取IP归属地
const ip =
req.ip ||
req.socket.remoteAddress ||
req.headers['x-forwarded-for'] ||
'未知';
let address = '未知';
if (!['::1'].includes(ip)) {
const addressRes = await (
await fetch(
`https://mesh.if.iqiyi.com/aid/ip/info?version=1.1.1&ip=${ip}`,
)
).json();
if (addressRes?.code == 0) {
const country: string = addressRes?.data?.countryCN || '未知';
const province: string = addressRes?.data?.provinceCN || '中国';
if (country !== '中国') {
// 非中国,显示国家
address = country;
} else {
// 中国,显示省份
address = province;
}
const comment = {
...commentData,
blogId: id,
user: user,
ip: ip,
address: address,
};
return await this.blogService.createComment(comment);
}
}
const comment = {
...commentData,
blogId: id,
user: user,
ip: ip,
address: address,
};
return await this.blogService.createComment(comment);
}
}