Skip to content
Snippets Groups Projects
Commit 68454f82 authored by DESKTOP-9FO96TP\hehexd's avatar DESKTOP-9FO96TP\hehexd
Browse files

fixed router for posts

parent 34b71bef
No related branches found
No related tags found
1 merge request!17Fix tweeting
Pipeline #35504 passed
import { prisma } from "@/lib/db"
import { NextRequest, NextResponse } from "next/server"
import { getServerSession } from "next-auth/next"
import { authOptions } from "../auth/[...nextauth]/route";
import { Prisma } from "@prisma/client";
type post = Prisma.PostUncheckedCreateInput
export async function POST(req: NextRequest) {
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ status: 401 });
}
const userId = session.user.id
const data = await req.json()
console.log("router data: " + data, "status:")
console.log(data)
try {
await prisma.post.create({
data: data
/* data: data */
data:{
content: data.content,
userId: parseInt(userId),
published: true
}
})
console.log("created")
return NextResponse.json({ status: 201, message: 'Message Created' })
// res.status(200).json({message: 'Message Created'})
// res.status(200).json({message: 'Message Created'})
} catch (error) {
console.log("fail" + error);
// res.status(400)
}
console.log("post")
}
......
......@@ -2,21 +2,19 @@
import { Post, Prisma } from "@prisma/client";
import { useRouter } from "next/navigation";
import { startTransition, useState } from "react";
import { startTransition, useEffect, useState } from "react";
type messageType = Prisma.PostUncheckedCreateInput
export default function PostMessageForm(props: { data: Post[] | null }) {
const [formData, setFormData] = useState<messageType>({ content: "" } as messageType);
// const [messagesState, setMessages] = useState(props.data)
const router = useRouter();
async function postMessage(e: any) {
e.preventDefault()
// setMessages([...messagesState, formData])
console.log(formData)
formData.userId = 1
/* formData.userId = 1 */
const response = await fetch('http://localhost:3000/api/messages', {
method: 'POST',
body: JSON.stringify(formData)
......@@ -30,7 +28,7 @@ export default function PostMessageForm(props: { data: Post[] | null }) {
}
const characterCount = formData.content.length;
const isOverLimit = characterCount > 1000;
const isOverLimit = characterCount >= 1000;
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const { value } = e.target;
......
import 'next-auth';
declare module 'next-auth' {
interface Session {
user: {
id: string;
name?: string | null;
email?: string | null;
image?: string | null;
};
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment