1- import { NextResponse } from ' next/server' ;
2- import { createBlog , getAllBlogs , deleteBlogs } from ' ../../../../lib/blog' ;
1+ import { NextResponse } from " next/server" ;
2+ import { createBlog , getAllBlogs , deleteBlogs } from " ../../../../lib/blog" ;
33import { getAdminByEmail } from "../../../../lib/admin" ;
4- import jwt from ' jsonwebtoken' ;
5- import { cookies } from ' next/headers' ;
4+ import jwt from " jsonwebtoken" ;
5+ import { cookies } from " next/headers" ;
66
77const SECRET_KEY = process . env . JWT_SECRET ;
8- export const dynamic = 'force-dynamic' ;
9-
8+ // Removed dynamic = 'force-dynamic' for static export compatibility
109
1110export async function GET ( req ) {
1211 try {
1312 const { searchParams } = new URL ( req . url ) ;
14- const page = parseInt ( searchParams . get ( ' page' ) ) || 1 ;
15- const sort = searchParams . get ( ' sort' ) || ' recent' ;
13+ const page = parseInt ( searchParams . get ( " page" ) ) || 1 ;
14+ const sort = searchParams . get ( " sort" ) || " recent" ;
1615
1716 const { blogs, totalPages } = await getAllBlogs ( { page, sort } ) ;
1817 return NextResponse . json ( { blogs, totalPages } ) ;
1918 } catch ( error ) {
20- console . error ( 'Error fetching blogs:' , error ) ;
21- return NextResponse . json ( { error : 'Internal Server Error' } , { status : 500 } ) ;
19+ console . error ( "Error fetching blogs:" , error ) ;
20+ return NextResponse . json (
21+ { error : "Internal Server Error" } ,
22+ { status : 500 }
23+ ) ;
2224 }
2325}
2426
2527export async function POST ( req ) {
26- const tokenCookie = cookies ( ) . get ( ' token' ) ;
28+ const tokenCookie = cookies ( ) . get ( " token" ) ;
2729 const token = tokenCookie ?. value ;
2830
2931 if ( ! token ) {
30- console . error ( ' No token provided' ) ;
31- return NextResponse . json ( { error : ' Unauthorized' } , { status : 401 } ) ;
32+ console . error ( " No token provided" ) ;
33+ return NextResponse . json ( { error : " Unauthorized" } , { status : 401 } ) ;
3234 }
3335
3436 try {
@@ -51,20 +53,26 @@ export async function POST(req) {
5153 blogData . upVoters = [ ] ;
5254
5355 const result = await createBlog ( blogData ) ;
54- return NextResponse . json ( { message : 'Blog created' , blogId : result . insertedId } , { status : 201 } ) ;
56+ return NextResponse . json (
57+ { message : "Blog created" , blogId : result . insertedId } ,
58+ { status : 201 }
59+ ) ;
5560 } catch ( error ) {
56- console . error ( 'Error creating blog:' , error ) ;
57- return NextResponse . json ( { error : 'Internal Server Error' } , { status : 500 } ) ;
61+ console . error ( "Error creating blog:" , error ) ;
62+ return NextResponse . json (
63+ { error : "Internal Server Error" } ,
64+ { status : 500 }
65+ ) ;
5866 }
5967}
6068
6169export async function DELETE ( req ) {
62- const tokenCookie = cookies ( ) . get ( ' token' ) ;
70+ const tokenCookie = cookies ( ) . get ( " token" ) ;
6371 const token = tokenCookie ?. value ;
6472
6573 if ( ! token ) {
66- console . error ( ' No token provided' ) ;
67- return NextResponse . json ( { error : ' Unauthorized' } , { status : 401 } ) ;
74+ console . error ( " No token provided" ) ;
75+ return NextResponse . json ( { error : " Unauthorized" } , { status : 401 } ) ;
6876 }
6977
7078 try {
@@ -74,8 +82,8 @@ export async function DELETE(req) {
7482 // Retrieve admin's name from the database
7583 const admin = await getAdminByEmail ( email ) ;
7684 if ( ! admin ) {
77- console . error ( ' Admin not found' ) ;
78- return NextResponse . json ( { error : ' Unauthorized' } , { status : 401 } ) ;
85+ console . error ( " Admin not found" ) ;
86+ return NextResponse . json ( { error : " Unauthorized" } , { status : 401 } ) ;
7987 }
8088
8189 const authorName = admin . name ; // Use the admin's name
@@ -84,12 +92,18 @@ export async function DELETE(req) {
8492 const deleteResult = await deleteBlogs ( ids ) ;
8593
8694 if ( deleteResult . deletedCount === 0 ) {
87- return NextResponse . json ( { error : 'No matching blogs found' } , { status : 404 } ) ;
95+ return NextResponse . json (
96+ { error : "No matching blogs found" } ,
97+ { status : 404 }
98+ ) ;
8899 }
89100
90- return NextResponse . json ( { message : ' Blogs deleted successfully' } ) ;
101+ return NextResponse . json ( { message : " Blogs deleted successfully" } ) ;
91102 } catch ( error ) {
92- console . error ( 'Error deleting blogs:' , error ) ;
93- return NextResponse . json ( { error : 'Internal Server Error' } , { status : 500 } ) ;
103+ console . error ( "Error deleting blogs:" , error ) ;
104+ return NextResponse . json (
105+ { error : "Internal Server Error" } ,
106+ { status : 500 }
107+ ) ;
94108 }
95109}
0 commit comments