import { Prisma, PrismaClient } from '@prisma/client'
import express from 'express'
const prisma = new PrismaClient()
const app = express()
[Link]([Link]())
[Link](`/signup`, async (req, res) => {
const { name, email, posts } = [Link]
const postData = posts?.map((post: [Link]) => {
return { title: post?.title, content: post?.content }
})
const result = await [Link]({
data: {
name,
email,
posts: {
create: postData,
},
},
})
[Link](result)
})
[Link](`/post`, async (req, res) => {
const { title, content, authorEmail } = [Link]
const result = await [Link]({
data: {
title,
content,
author: { connect: { email: authorEmail } },
},
})
[Link](result)
})
[Link]('/post/:id/views', async (req, res) => {
const { id } = [Link]
try {
const post = await [Link]({
where: { id: Number(id) },
data: {
viewCount: {
increment: 1,
},
},
})
[Link](post)
} catch (error) {
[Link]({ error: `Post with ID ${id} does not exist in the
database` })
})
[Link]('/publish/:id', async (req, res) => {
const { id } = [Link]
try {
const postData = await [Link]({
where: { id: Number(id) },
select: {
published: true,
},
})
const updatedPost = await [Link]({
where: { id: Number(id) || undefined },
data: { published: !postData?.published },
})
[Link](updatedPost)
} catch (error) {
[Link]({ error: `Post with ID ${id} does not exist in the
database` })
})
[Link](`/post/:id`, async (req, res) => {
const { id } = [Link]
const post = await [Link]({
where: {
id: Number(id),
},
})
[Link](post)
})
[Link]('/users', async (req, res) => {
const users = await [Link]()
[Link](users)
})
[Link]('/user/:id/drafts', async (req, res) => {
const { id } = [Link]
const drafts = await [Link]
.findUnique({
where: {
id: Number(id),
},
})
.posts({
where: { published: false },
})
[Link](drafts)
})
[Link](`/post/:id`, async (req, res) => {
const { id }: { id?: string } = [Link]
const post = await [Link]({
where: { id: Number(id) },
})
[Link](post)
})
[Link]('/feed', async (req, res) => {
const { searchString, skip, take, orderBy } = [Link]
const or: [Link] = searchString
?{
OR: [
{ title: { contains: searchString as string } },
{ content: { contains: searchString as string } },
],
: {}
const posts = await [Link]({
where: {
published: true,
...or,
},
include: { author: true },
take: Number(take) || undefined,
skip: Number(skip) || undefined,
orderBy: {
updatedAt: orderBy as [Link],
},
})
[Link](posts)
})
const server = [Link](3000, () =>
[Link](`
🚀 Server ready at: [Link]
See sample requests: [Link]
api`),