Skip to content

Commit 17e7b74

Browse files
committed
バックエンド側でauth_idからidを取得する処理を追加
1 parent 7d99a51 commit 17e7b74

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

app/allPost/[id]/components/CourseReview.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
"use client";
2-
import useUser from "app/hooks/useUser";
32
import { Button } from "@/components/ui/button";
43
import { Input } from "@/components/ui/input";
54
import { useRouter } from "next/navigation";
@@ -9,8 +8,9 @@ import toast from "react-hot-toast";
98

109
interface CourseReviewProps {
1110
id: number;
11+
auth_id: string;
1212
}
13-
const CourseReview = ({ id }: CourseReviewProps) => {
13+
const CourseReview = ({ id, auth_id }: CourseReviewProps) => {
1414
const {
1515
register,
1616
handleSubmit,
@@ -21,7 +21,6 @@ const CourseReview = ({ id }: CourseReviewProps) => {
2121
authorId: 0,
2222
},
2323
});
24-
const { user } = useUser();
2524
const router = useRouter();
2625
const [isLoading, setIsLoading] = useState(false);
2726

@@ -36,8 +35,8 @@ const CourseReview = ({ id }: CourseReviewProps) => {
3635
method: "POST",
3736
body: JSON.stringify({
3837
title: data.title,
39-
id: Number(id),
40-
authorId: user?.id,
38+
planId: Number(id),
39+
auth_id: auth_id,
4140
}),
4241
headers: {
4342
"Content-Type": "application/json",

app/api/post/coursepost/route.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,32 @@ import prisma from "utils/prisma/prismaClient";
33

44
export const POST = async (req: Request) => {
55
try {
6-
const { title, id, authorId } = await req.json();
6+
const { title, planId, auth_id } = await req.json();
77

88
await prisma.$connect();
9+
10+
const author = await prisma.user.findUnique({
11+
where: {
12+
auth_id: auth_id,
13+
},
14+
select: {
15+
id: true,
16+
},
17+
});
18+
919
const post = await prisma.post.create({
1020
data: {
1121
title: title,
12-
planId: id,
13-
authorId: authorId,
22+
planId: planId,
23+
authorId: author?.id,
1424
},
1525
});
1626
return NextResponse.json({ message: "Success", post }, { status: 201 });
1727
} catch (error) {
18-
return NextResponse.json({ message: "Error", error }, { status: 500 });
28+
return NextResponse.json(
29+
{ message: "エラーが発生しました", error },
30+
{ status: 500 }
31+
);
1932
} finally {
2033
await prisma.$disconnect();
2134
}

0 commit comments

Comments
 (0)