-
Notifications
You must be signed in to change notification settings - Fork 778
/
post.ts
264 lines (242 loc) · 6.72 KB
/
post.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
import prisma from "@/lib/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import type { Post, Site } from ".prisma/client";
import type { Session } from "next-auth";
import { revalidate } from "@/lib/revalidate";
import { getBlurDataURL } from "@/lib/util";
import type { WithSitePost } from "@/types";
interface AllPosts {
posts: Array<Post>;
site: Site | null;
}
/**
* Get Post
*
* Fetches & returns either a single or all posts available depending on
* whether a `postId` query parameter is provided. If not all posts are
* returned in descending order.
*
* @param req - Next.js API Request
* @param res - Next.js API Response
*/
export async function getPost(
req: NextApiRequest,
res: NextApiResponse,
session: Session
): Promise<void | NextApiResponse<AllPosts | (WithSitePost | null)>> {
const { postId, siteId, published } = req.query;
if (
Array.isArray(postId) ||
Array.isArray(siteId) ||
Array.isArray(published)
)
return res.status(400).end("Bad request. Query parameters are not valid.");
if (!session.user.id)
return res.status(500).end("Server failed to get session user ID");
try {
if (postId) {
const post = await prisma.post.findFirst({
where: {
id: postId,
site: {
user: {
id: session.user.id,
},
},
},
include: {
site: true,
},
});
return res.status(200).json(post);
}
const site = await prisma.site.findFirst({
where: {
id: siteId,
user: {
id: session.user.id,
},
},
});
const posts = !site
? []
: await prisma.post.findMany({
where: {
site: {
id: siteId,
},
published: JSON.parse(published || "true"),
},
orderBy: {
createdAt: "desc",
},
});
return res.status(200).json({
posts,
site,
});
} catch (error) {
console.error(error);
return res.status(500).end(error);
}
}
/**
* Create Post
*
* Creates a new post from a provided `siteId` query parameter.
*
* Once created, the sites new `postId` will be returned.
*
* @param req - Next.js API Request
* @param res - Next.js API Response
*/
export async function createPost(
req: NextApiRequest,
res: NextApiResponse
): Promise<void | NextApiResponse<{
postId: string;
}>> {
const { siteId } = req.query;
if (Array.isArray(siteId))
return res
.status(400)
.end("Bad request. siteId parameter cannot be an array.");
try {
const response = await prisma.post.create({
data: {
image: `/placeholder.png`,
imageBlurhash:
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAoJJREFUWEfFl4lu4zAMRO3cx/9/au6reMaOdkxTTl0grQFCRoqaT+SQotq2bV9N8rRt28xms87m83l553eZ/9vr9Wpkz+ezkT0ej+6dv1X81AFw7M4FBACPVn2c1Z3zLgDeJwHgeLFYdAARYioAEAKJEG2WAjl3gCwNYymQQ9b7/V4spmIAwO6Wy2VnAMikBWlDURBELf8CuN1uHQSrPwMAHK5WqwFELQ01AIXdAa7XawfAb3p6AOwK5+v1ugAoEq4FRSFLgavfQ49jAGQpAE5wjgGCeRrGdBArwHOPcwFcLpcGU1X0IsBuN5tNgYhaiFFwHTiAwq8I+O5xfj6fOz38K+X/fYAdb7fbAgFAjIJ6Aav3AYlQ6nfnDoDz0+lUxNiLALvf7XaDNGQ6GANQBKR85V27B4D3QQRw7hGIYlQKWGM79hSweyCUe1blXhEAogfABwHAXAcqSYkxCtHLUK3XBajSc4Dj8dilAeiSAgD2+30BAEKV4GKcAuDqB4TdYwBgPQByCgApUBoE4EJUGvxUjF3Q69/zLw3g/HA45ABKgdIQu+JPIyDnisCfAxAFNFM0EFNQ64gfS0EUoQP8ighrZSjn3oziZEQpauyKbfjbZchHUL/3AS/Dd30gAkxuRACgfO+EWQW8qwI1o+wseNuKcQiESjALvwNoMI0TcRzD4lFcPYwIM+JTF5x6HOs8yI7jeB5oKhpMRFH9UwaSCDB2Jmg4rc6E2TT0biIaG0rQhNqyhpHBcayTTSXH6vcDL7/sdqRK8LkwTsU499E8vRcAojHcZ4AxABdilgrp4lsXk8oVqgwh7+6H3phqd8J0Kk4vbx/+sZqCD/vNLya/5dT9fAH8g1WdNGgwbQAAAABJRU5ErkJggg==",
site: {
connect: {
id: siteId,
},
},
},
});
return res.status(201).json({
postId: response.id,
});
} catch (error) {
console.error(error);
return res.status(500).end(error);
}
}
/**
* Delete Post
*
* Deletes a post from the database using a provided `postId` query
* parameter.
*
* @param req - Next.js API Request
* @param res - Next.js API Response
*/
export async function deletePost(
req: NextApiRequest,
res: NextApiResponse
): Promise<void | NextApiResponse> {
const { postId } = req.query;
if (Array.isArray(postId))
return res
.status(400)
.end("Bad request. postId parameter cannot be an array.");
try {
const response = await prisma.post.delete({
where: {
id: postId,
},
include: {
site: {
select: { subdomain: true, customDomain: true },
},
},
});
if (response?.site?.subdomain) {
// revalidate for subdomain
await revalidate(
`https://${response.site?.subdomain}.vercel.pub`, // hostname to be revalidated
response.site.subdomain, // siteId
response.slug // slugname for the post
);
}
if (response?.site?.customDomain)
// revalidate for custom domain
await revalidate(
`https://${response.site.customDomain}`, // hostname to be revalidated
response.site.customDomain, // siteId
response.slug // slugname for the post
);
return res.status(200).end();
} catch (error) {
console.error(error);
return res.status(500).end(error);
}
}
/**
* Update Post
*
* Updates a post & all of its data using a collection of provided
* query parameters. These include the following:
* - id
* - title
* - description
* - content
* - slug
* - image
* - imageBlurhash
* - published
*
* @param req - Next.js API Request
* @param res - Next.js API Response
*/
export async function updatePost(
req: NextApiRequest,
res: NextApiResponse
): Promise<void | NextApiResponse<Post>> {
const {
id,
title,
description,
content,
slug,
image,
published,
subdomain,
customDomain,
} = req.body;
try {
const post = await prisma.post.update({
where: {
id: id,
},
data: {
title,
description,
content,
slug,
image,
imageBlurhash: await getBlurDataURL(image),
published,
},
});
if (subdomain) {
// revalidate for subdomain
await revalidate(
`https://${subdomain}.vercel.pub`, // hostname to be revalidated
subdomain, // siteId
slug // slugname for the post
);
}
if (customDomain)
// revalidate for custom domain
await revalidate(
`https://${customDomain}`, // hostname to be revalidated
customDomain, // siteId
slug // slugname for the post
);
return res.status(200).json(post);
} catch (error) {
console.error(error);
return res.status(500).end(error);
}
}