-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from deepsingh132/development
Refactor: Replace useEffect with SWR 2.0 for improved data fetching
- Loading branch information
Showing
38 changed files
with
874 additions
and
616 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,32 @@ | ||
type Post = { | ||
content?: string | null; | ||
name: string; | ||
username: string; | ||
authorID: string; | ||
category?: | ||
| "art" | ||
| "music" | ||
| "hybrid" | ||
| "theatre" | ||
| "literature" | ||
| "science" | ||
| "sports" | ||
| "other"; | ||
tags?: string[]; | ||
image?: string; | ||
featured?: boolean; | ||
likes?: string[]; | ||
comments?: { | ||
userId: string; | ||
content: string; | ||
username: string; | ||
timestamp: string; | ||
userImg: string; | ||
url: string; | ||
name: string; | ||
}[]; | ||
userImg?: string; | ||
url?: string | null; | ||
createdAt?: string; | ||
updatedAt?: string; | ||
}; |
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
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 |
---|---|---|
@@ -1,23 +1,44 @@ | ||
import { connectMongoDB } from "@/libs/mongodb"; | ||
import Post from "../../../models/Post"; | ||
import { NextResponse } from "next/server"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
|
||
export async function GET(req: NextRequest) { | ||
try { | ||
await connectMongoDB(); | ||
const type = req.nextUrl.searchParams.get("type") || null; | ||
|
||
export async function GET() { | ||
await connectMongoDB(); | ||
const posts = await Post.find({}); // fetch data from the source | ||
return NextResponse.json({ posts }); // respond with JSON | ||
if (type) { | ||
const posts = await Post.find({ category: type }); | ||
return NextResponse.json({ posts }); | ||
} | ||
|
||
const posts = await Post.find({}); // fetch data from the source | ||
return NextResponse.json({ posts }); // respond with JSON | ||
} catch (error) { | ||
console.error("Error fetching posts: ", error); | ||
return NextResponse.json({ message: "Error fetching posts from MongoDB!" }); | ||
} | ||
} | ||
|
||
// Private route, handle post creation | ||
export async function POST(req) { | ||
try { | ||
const { username, name, userImg, content, authorID, category, url } = await req.json(); | ||
const { _id, username, name, userImg, content, authorID, category, url } = | ||
await req.json(); | ||
await connectMongoDB(); | ||
const res = await Post.create({ username, name, userImg, content, authorID, category, url }); | ||
const res = await Post.create({ | ||
_id, | ||
username, | ||
name, | ||
userImg, | ||
content, | ||
authorID, | ||
category, | ||
url, | ||
}); | ||
return NextResponse.json({ message: "success", post: res }); | ||
} catch (error) { | ||
console.error("Error creating post: ", error); | ||
return NextResponse.json({ message: "error: ", error }); | ||
} | ||
} catch (error) { | ||
console.error("Error creating post: ", error); | ||
return NextResponse.json({ message: "error: ", error }); | ||
} | ||
} |
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
Oops, something went wrong.