Skip to content

Commit

Permalink
Add reading history functionality to track user's
Browse files Browse the repository at this point in the history
post views
  • Loading branch information
thebkht committed Nov 6, 2023
1 parent 6dd0ff7 commit 7d675bf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/api/posts/[username]/views/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { incrementPostViews } from "@/components/blog/actions";
import { getSessionUser } from "@/components/get-session-user";
import postgres from "@/lib/postgres";
import { NextRequest, NextResponse } from "next/server";

Expand All @@ -10,6 +11,7 @@ export async function POST(
// Get the 'slug' route parameter from the request object
const username = params.username;
const postUrl = req.nextUrl.searchParams.get("url");
const session = await getSessionUser()

if (username === undefined || username === null) {
return NextResponse.json({ error: "User not found" }, { status: 404 });
Expand All @@ -21,6 +23,25 @@ export async function POST(
const cookie = await incrementPostViews({ author: username, post: postUrl });

req.cookies.set(cookie.name, cookie.value);

const post = await postgres.post.findUnique({
where: {
url: postUrl,
author: {
username,
}
},
select: {
id: true,
}
});

const readingHistory = await postgres.readingHistory.create({
data: {
postId: Number(post?.id),
userId: Number(session?.id),
},
})

return NextResponse.json({ message: "View added" }, { status: 200 });
} catch (error) {
Expand Down
16 changes: 16 additions & 0 deletions prisma/migrations/20231106092655_reading_history/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CreateTable
CREATE TABLE "readinghistory" (
"id" SERIAL NOT NULL,
"postId" INTEGER NOT NULL,
"userId" INTEGER NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,

CONSTRAINT "readinghistory_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "readinghistory" ADD CONSTRAINT "readinghistory_postId_fkey" FOREIGN KEY ("postId") REFERENCES "posts"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "readinghistory" ADD CONSTRAINT "readinghistory_userId_fkey" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
15 changes: 14 additions & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ model User {
Followers Follow[] @relation("Following")
tagfollower TagFollow[] @relation("TagFollower")
bookmarks Bookmark[]
readinghistory ReadingHistory[]
@@map("users")
}
Expand All @@ -103,6 +103,7 @@ model Post {
comments Comment[]
likes Like[]
savedUsers Bookmark[]
readedUsers ReadingHistory[]
@@index([createdAt])
@@map("posts")
Expand Down Expand Up @@ -233,3 +234,15 @@ model UserSettings {
@@map("usersettings")
}

model ReadingHistory {
id Int @id @default(autoincrement())
post Post @relation(fields: [postId], references: [id])
postId Int
user User @relation(fields: [userId], references: [id])
userId Int
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
@@map("readinghistory")
}

0 comments on commit 7d675bf

Please sign in to comment.