Skip to content

Commit b41e414

Browse files
committed
feat: likes backend
1 parent 2419b4a commit b41e414

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { PrismaClient } from "@prisma/client"
2+
const prisma = new PrismaClient()
3+
4+
import crypto from "crypto"
5+
6+
export async function PUT(req:Request) {
7+
const { articleId } = await req.json()
8+
9+
try {
10+
const article = await prisma.post.update({
11+
where: {
12+
id: articleId
13+
},
14+
data: {
15+
likes: {
16+
increment: 1
17+
}
18+
}
19+
});
20+
21+
return new Response(JSON.stringify(article), { status: 200 })
22+
}
23+
catch(err) {
24+
const a = crypto.randomBytes(20).toString("hex")
25+
console.error({
26+
err,
27+
randomCode: a
28+
})
29+
30+
return new Response(JSON.stringify({ message: "An error occurred" }), { status: 500 })
31+
}
32+
}

pnpm-lock.yaml

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

prisma/schema.prisma

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
66

77
generator client {
8-
provider = "prisma-client-js"
8+
provider = "prisma-client-js"
99
previewFeatures = ["driverAdapters"]
1010
}
1111

@@ -27,9 +27,10 @@ model User {
2727
model Post {
2828
id String @id @default(cuid())
2929
title String
30-
slug String @unique @default(cuid())
30+
slug String @unique @default(cuid())
31+
likes Int @default(0)
3132
content String
32-
contentMD String @default("")
33+
contentMD String @default("")
3334
published Boolean @default(false)
3435
author User @relation(fields: [authorId], references: [id])
3536
authorId String

0 commit comments

Comments
 (0)