Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
wanghao1993 committed Sep 9, 2024
1 parent be40b76 commit 90d177b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 56 deletions.
114 changes: 59 additions & 55 deletions app/api/blog/detail/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,97 @@ import { getServerSession } from "next-auth";
import { getSession } from "next-auth/react";
import { authOptions } from "@/lib/auth_options";
import { message } from "antd";
export const dynamic = "force-dynamic"; // defaults to auto
import { z } from "zod";

export const dynamic = "force-dynamic";

const postDetailSchema = z.object({
blog_key: z.string(),
// 其他字段的验证规则,如果有的话
});

export async function GET(request: Request) {
const key = new URL(request.url).searchParams.get("key");
if (!key) {
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"无key"
"无 key"
);
} else {
try {
const res = await prisma.post.findUnique({
where: { blog_key: key },
});

if (!res) {
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"key值错误"
);
} else {
return responseHandler(res);
}
} catch (e) {
}
try {
const res = await prisma.post.findUnique({
where: { blog_key: key },
});
if (!res) {
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"未知异常"
"key 值错误"
);
}
return responseHandler(res);
} catch (error) {
console.error("GET 请求错误:", error);
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"未知异常"
);
}
}

export async function POST(req: Request, res: Response) {
const body: PostTypes.PostDetail = await req.json();
const rawBody = await req.json();
const validationResult = postDetailSchema.safeParse(rawBody);
if (!validationResult.success) {
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"数据格式错误"
);
}
const body = validationResult.data;
const session = await getServerSession(authOptions);
const post = await prisma.post.findUnique({
where: {
blog_key: body.blog_key,
},
where: { blog_key: body.blog_key },
});

if (post && session?.user) {
const user = await prisma.user.findUnique({
where: {
email: session.user.email as string,
},
});
const likes_count = post.likes_count;

if (user) {
const idx = likes_count.findIndex((item) => item === user.id);
if (idx > -1) {
likes_count.splice(idx, 1);
} else {
likes_count.push(user.id);
}

try {
const updatePost = await prisma.post.update({
where: {
blog_key: body.blog_key,
},
data: {
likes_count,
},
});

return responseHandler(updatePost);
} catch (e) {
try {
const user = await prisma.user.findUnique({
where: { email: session.user.email as string },
});
if (!user) {
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
e.message
"用户查询错误"
);
}
} else {
const likes_count = post.likes_count || [];
const idx = likes_count.findIndex((item) => item === user.id);
if (idx > -1) {
likes_count.splice(idx, 1);
} else {
likes_count.push(user.id);
}
const updatePost = await prisma.post.update({
where: { blog_key: body.blog_key },
data: { likes_count },
});
return responseHandler(updatePost);
} catch (error) {
console.error("POST 请求错误:", error);
return responseHandler(
null,
BusinessCode.normal,
BusinessCode.abnormal,
"用户查询错误"
error.message || "未知异常"
);
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"sharp": "^0.33.5",
"tencentcloud-sdk-nodejs": "^4.0.888",
"unist-util-visit": "^5.0.0",
"vfile": "^6.0.3"
"vfile": "^6.0.3",
"zod": "^3.23.8"
},
"devDependencies": {
"@types/crypto-js": "^4.2.2",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 90d177b

Please sign in to comment.