-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
250 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { NextRequest, NextResponse } from "next/server"; | ||
import prisma from "@/lib/prisma"; | ||
import getSession from "@/lib/session"; | ||
|
||
export async function POST(req: NextRequest) { | ||
const { title, description, content, slug, date, isDraft } = await req.json(); | ||
|
||
const session = await getSession(); | ||
|
||
if (!session?.user?.isAdmin) { | ||
return NextResponse.json( | ||
{ message: "You are not authorized to create a post!" }, | ||
{ status: 401 } | ||
); | ||
} | ||
|
||
try { | ||
const id_post = await prisma.posts.findFirst({ | ||
where: { | ||
slug, | ||
}, | ||
}); | ||
|
||
if (!id_post) { | ||
return NextResponse.json({ message: "Post not found!" }, { status: 404 }); | ||
} | ||
|
||
await prisma.posts.update({ | ||
where: { | ||
id: id_post.id, | ||
}, | ||
data: { | ||
title, | ||
description, | ||
slug, | ||
date, | ||
content, | ||
isDraft, | ||
}, | ||
}); | ||
|
||
return NextResponse.json( | ||
{ message: "Post updated successfully!" }, | ||
{ status: 200 } | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
return NextResponse.json( | ||
{ message: "Failed to update post!" }, | ||
{ status: 500 } | ||
); | ||
} | ||
|
||
return NextResponse.json( | ||
{ message: "Post updated successfully!" }, | ||
{ status: 201 } | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { EditPostForm } from "@/components/forms/PostForm"; | ||
import Header from "@/components/layout/header"; | ||
import getSession from "@/lib/session"; | ||
import { Session } from "@/lib/types"; | ||
import { redirect } from "next/navigation"; | ||
import prisma from "@/lib/prisma"; | ||
|
||
export default async function CreatePost({params}: {params: {slug: string}}) { | ||
const session: Session = await getSession(); | ||
if (!session?.user?.isAdmin) { | ||
redirect("/blog"); | ||
} | ||
|
||
const post = await prisma.posts.findFirst({ | ||
where: { | ||
slug: params.slug, | ||
}, | ||
}); | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
<main> | ||
<EditPostForm post={post!} /> | ||
</main> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters