-
Notifications
You must be signed in to change notification settings - Fork 0
/
prisma_sample.ts
150 lines (126 loc) · 3.21 KB
/
prisma_sample.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Prisma, PrismaClient } from '@prisma/client';
import express from 'express';
const prisma = new PrismaClient();
const app = express();
app.use(express.json());
app.post(`/signup`, async (req, res) => {
const { name, email, posts } = req.body;
const postData = posts?.map((post: Prisma.PostCreateInput) => {
return { title: post?.title, content: post?.content };
});
const result = await prisma.user.create({
data: {
name,
email,
posts: {
create: postData,
},
},
});
res.json(result);
});
app.post(`/post`, async (req, res) => {
const { title, content, authorEmail } = req.body;
const result = await prisma.post.create({
data: {
title,
content,
author: { connect: { email: authorEmail } },
},
});
res.json(result);
});
app.put('/post/:id/views', async (req, res) => {
const { id } = req.params;
try {
const post = await prisma.post.update({
where: { id: Number(id) },
data: {
viewCount: {
increment: 1,
},
},
});
res.json(post);
} catch (error) {
res.json({ error: `Post with ID ${id} does not exist in the database` });
}
});
app.put('/publish/:id', async (req, res) => {
const { id } = req.params;
try {
const postData = await prisma.post.findUnique({
where: { id: Number(id) },
select: {
published: true,
},
});
const updatedPost = await prisma.post.update({
where: { id: Number(id) || undefined },
data: { published: !postData?.published },
});
res.json(updatedPost);
} catch (error) {
res.json({ error: `Post with ID ${id} does not exist in the database` });
}
});
app.delete(`/post/:id`, async (req, res) => {
const { id } = req.params;
const post = await prisma.post.delete({
where: {
id: Number(id),
},
});
res.json(post);
});
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany();
res.json(users);
});
app.get('/user/:id/drafts', async (req, res) => {
const { id } = req.params;
const drafts = await prisma.user
.findUnique({
where: {
id: Number(id),
},
})
.posts({
where: { published: false },
});
res.json(drafts);
});
app.get(`/post/:id`, async (req, res) => {
const { id }: { id?: string } = req.params;
console.log('id: ', id);
const post = await prisma.post.findUnique({
where: { id: Number(id) },
});
res.json(post);
});
app.get('/feed', async (req, res) => {
const { searchString, skip, take, orderBy } = req.query;
const or: Prisma.PostWhereInput = searchString
? {
OR: [{ title: { contains: searchString as string } }, { content: { contains: searchString as string } }],
}
: {};
const posts = await prisma.post.findMany({
where: {
published: true,
...or,
},
include: { author: true },
take: Number(take) || undefined,
skip: Number(skip) || undefined,
orderBy: {
updatedAt: orderBy as Prisma.SortOrder,
},
});
res.json(posts);
});
const server = app.listen(3000, () =>
console.log(`
🚀 Server ready at: http://localhost:3000
⭐️ See sample requests: http://pris.ly/e/ts/rest-express#3-using-the-rest-api`),
);