-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
services.ts
45 lines (38 loc) · 1013 Bytes
/
services.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
import { error } from '@sveltejs/kit';
import prisma from '@db';
export async function getPaste(key: string) {
let data = await prisma.paste.findUnique({
where: { key },
select: {
content: true,
encrypted: true,
passwordProtected: true,
initVector: true,
language: true,
expiresCount: true,
readCount: true,
ownerId: true
}
});
if (!data) throw error(404, 'Not found');
data = await prisma.paste.update({
where: { key },
data: { readCount: { increment: 1 } }
});
const { expiresCount, readCount } = data;
if (expiresCount !== null && expiresCount < readCount) {
await prisma.paste.delete({ where: { key } });
throw error(404, 'Not found');
}
const { content, encrypted, passwordProtected, initVector, language, ownerId } = data;
return { key, content, encrypted, passwordProtected, initVector, language, ownerId };
}
export async function deleteExpiredPastes() {
await prisma.paste.deleteMany({
where: {
expiresAt: {
lt: new Date()
}
}
});
}