Files
mstfyldz/app/api/posts/route.ts
T
2026-08-16 17:18:23 +03:00

81 lines
1.6 KiB
TypeScript

import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
export async function GET() {
try {
const posts = await prisma.post.findMany({
orderBy: {
createdAt: "desc",
},
});
return NextResponse.json(posts);
} catch (error) {
console.error("Error fetching posts:", error);
return NextResponse.json(
{ error: "Failed to fetch posts from database" },
{ status: 500 }
);
}
}
export async function POST(request: Request) {
try {
const body = await request.json();
const {
id,
type,
date,
timestamp,
mood,
moodLabel,
title,
content,
marginNotes,
tags,
likes,
highlightWords,
authorNote,
imageUrl,
imageCaption,
codeSnippet,
codeLanguage,
audioDuration,
audioTitle,
stampedText,
} = body;
const newPost = await prisma.post.create({
data: {
id: id || `post-${Date.now()}`,
type,
date,
timestamp,
mood,
moodLabel,
title,
content,
marginNotes: marginNotes || [],
tags: tags || [],
likes: likes || 0,
highlightWords: highlightWords || [],
authorNote,
imageUrl,
imageCaption,
codeSnippet,
codeLanguage,
audioDuration,
audioTitle,
stampedText,
},
});
return NextResponse.json(newPost, { status: 201 });
} catch (error) {
console.error("Error creating post:", error);
return NextResponse.json(
{ error: "Failed to create post in database" },
{ status: 500 }
);
}
}