-
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
13 changed files
with
190 additions
and
204 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
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,59 +1,84 @@ | ||
import postgres from '@/lib/postgres' | ||
import { NextRequest, NextResponse } from 'next/server' | ||
|
||
export async function GET(req: NextRequest, res: NextResponse) { | ||
try { | ||
const user_id = Number(req.nextUrl.searchParams.get('user')) | ||
let page = parseInt(req.nextUrl.searchParams.get('page')!) | ||
let limit = 5 | ||
let offset = 0 | ||
|
||
const userFollowings = await postgres.follow.findMany({ | ||
select: { | ||
followingId: true, | ||
}, | ||
where: { | ||
followerId: user_id, | ||
}, | ||
}) | ||
const feed = await postgres.post.findMany({ | ||
where: { | ||
authorId: { | ||
in: userFollowings.map((user) => user.followingId), | ||
export async function POST(req: NextRequest) { | ||
const { page = 0, tag, id } = await req.json() | ||
if (!id) { | ||
return NextResponse.json({ error: 'No user found' }, { status: 500 }) | ||
} | ||
if (tag) { | ||
const postTags = await postgres.postTag.findMany({ | ||
select: { postId: true }, | ||
where: { tag: { name: { equals: tag } } }, | ||
}); | ||
const postIds = postTags.map((postTag) => postTag.postId); | ||
return fetchFeed({ | ||
where: { id: { in: postIds } }, | ||
orderBy: { createdAt: "desc" }, | ||
take: 5, | ||
skip: page * 5, | ||
include: { | ||
author: { | ||
include: { | ||
Followers: true, | ||
Followings: true, | ||
}, | ||
}, | ||
}, | ||
orderBy: { | ||
createdAt: 'desc', | ||
}, | ||
take: limit, | ||
skip: page * limit, | ||
include: { | ||
author: { | ||
include: { | ||
Followers: true, | ||
Followings: true, | ||
} | ||
}, | ||
_count: { | ||
select: { | ||
likes: true, | ||
comments: true, | ||
savedUsers: true, | ||
_count: { | ||
select: { | ||
likes: true, | ||
savedUsers: true, | ||
}, | ||
}, | ||
tags: { | ||
take: 1, | ||
include: { | ||
tag: true, | ||
}, | ||
}, | ||
}, | ||
tags: { | ||
take: 1, | ||
include: { | ||
tag: true, | ||
}); | ||
} else { | ||
const following = await postgres.follow.findMany({ | ||
select: { followingId: true }, | ||
where: { followerId: id }, | ||
}); | ||
const followingIds = following.map((user) => user.followingId); | ||
return fetchFeed({ | ||
where: { authorId: { in: followingIds } }, | ||
orderBy: { createdAt: "desc" }, | ||
take: 5, | ||
skip: page * 5, | ||
include: { | ||
author: { | ||
include: { | ||
Followers: true, | ||
Followings: true, | ||
}, | ||
}, | ||
}, | ||
} | ||
}) | ||
_count: { | ||
select: { | ||
likes: true, | ||
savedUsers: true, | ||
}, | ||
}, | ||
tags: { | ||
take: 1, | ||
include: { | ||
tag: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
return NextResponse.json({ feed }, { status: 200 }) | ||
const fetchFeed = async (query: any) => { | ||
try { | ||
const feed = await postgres.post.findMany(query); | ||
await new Promise((resolve) => setTimeout(resolve, 750)); | ||
return NextResponse.json({ feed: feed}, { status: 200 }); | ||
} catch (error) { | ||
return NextResponse.json({ error: error }, { status: 500 }); | ||
} | ||
catch (error) { | ||
console.error(error) | ||
return NextResponse.json({ error: 'Something went wrong' }, { status: 500 }) | ||
} | ||
} | ||
}; |
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,59 +1,15 @@ | ||
import postgres from "@/lib/postgres"; | ||
import { NextRequest, NextResponse } from "next/server"; | ||
// pages/api/some-route.ts | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
import { incrementPostViews } from '@/components/blog/actions'; | ||
|
||
export async function POST( | ||
req: NextRequest, | ||
{ params }: { params: { username: string } } | ||
) { | ||
try { | ||
// Get the 'slug' route parameter from the request object | ||
const username = params.username; | ||
const postUrl = req.nextUrl.searchParams.get("url"); | ||
export default async function POST(req: NextRequest, res: NextResponse) { | ||
const { post } = await req.json(); | ||
const { author } = await req.json(); | ||
|
||
if (username === undefined || username === null) { | ||
return NextResponse.json({ error: "User not found" }, { status: 404 }); | ||
} | ||
if (postUrl === undefined || postUrl === null) { | ||
return NextResponse.json({ error: "Post not found" }, { status: 404 }); | ||
} | ||
const cookie = await incrementPostViews({ post, author }); | ||
|
||
// Check if the view has already been counted by looking for a cookie | ||
const cookieName = `post_views_${username}_${postUrl}`; | ||
const hasViewed = req.cookies.get(cookieName); | ||
// Set the cookie | ||
res.cookies.set(cookie) | ||
|
||
if (!hasViewed) { | ||
// Execute a query to fetch the specific userid by name | ||
const author = await postgres.user.findUnique({ | ||
where: { | ||
username: username, | ||
}, | ||
select: { | ||
id: true, | ||
}, | ||
}); | ||
const authorID = author?.id; | ||
|
||
await postgres.post.update({ | ||
where: { | ||
url: postUrl, | ||
authorId: authorID, | ||
}, | ||
data: { | ||
views: { | ||
increment: 1, | ||
}, | ||
}, | ||
}); | ||
// Set a cookie to indicate that the post has been viewed | ||
req.cookies.set(cookieName, "true",) | ||
} | ||
|
||
return NextResponse.json({ message: "View added" }, { status: 200 }); | ||
} catch (error) { | ||
console.log(error); | ||
return NextResponse.json( | ||
{ error: "Something went wrong" }, | ||
{ status: 500 } | ||
); | ||
} | ||
} | ||
return NextResponse.json({ message: 'Post view incremented' }, { status: 200 }); | ||
} |
Oops, something went wrong.