diff --git a/app/api/posts/[username]/views/route.ts b/app/api/posts/[username]/views/route.ts index 68d634c4..749ca0b4 100644 --- a/app/api/posts/[username]/views/route.ts +++ b/app/api/posts/[username]/views/route.ts @@ -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"; @@ -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 }); @@ -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) { diff --git a/prisma/migrations/20231106092655_reading_history/migration.sql b/prisma/migrations/20231106092655_reading_history/migration.sql new file mode 100644 index 00000000..4db06264 --- /dev/null +++ b/prisma/migrations/20231106092655_reading_history/migration.sql @@ -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; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 05ede97b..b9ae971a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -77,7 +77,7 @@ model User { Followers Follow[] @relation("Following") tagfollower TagFollow[] @relation("TagFollower") bookmarks Bookmark[] - + readinghistory ReadingHistory[] @@map("users") } @@ -103,6 +103,7 @@ model Post { comments Comment[] likes Like[] savedUsers Bookmark[] + readedUsers ReadingHistory[] @@index([createdAt]) @@map("posts") @@ -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") +} \ No newline at end of file