-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.ts
49 lines (47 loc) · 1.48 KB
/
connect.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
import { ConnectRouter } from "@connectrpc/connect";
import { PrismaClient } from "@prisma/client";
import { NicochuuService } from "./gen/nicochuu_connect.js";
const prisma = new PrismaClient();
export default (router: ConnectRouter) =>
router.service(NicochuuService, {
listVideos: async (req) => {
const [total, videos] = await prisma.$transaction([
prisma.nicovideoVideo.count({
where: { checked: false },
}),
prisma.nicovideoVideo.findMany({
take: req.take,
skip: req.skip,
where: { checked: false },
orderBy: { postedAt: "desc" },
select: { id: true, sourceId: true, postedAt: true },
}),
]);
return {
total: total,
videos: videos.map(({ postedAt, ...props }) => ({
postedAt: postedAt.toISOString(),
...props,
})),
};
},
getVideo: async (req) => {
return {
video: await prisma.nicovideoVideo
.findUnique({
where: { sourceId: req.sourceId },
select: { sourceId: true, postedAt: true },
})
.then((video) => (video ? { ...video, postedAt: video.postedAt.toISOString() } : undefined)),
};
},
checkVideo: async (req) => {
return prisma.nicovideoVideo
.update({
where: { sourceId: req.sourceId },
data: { checked: true },
})
.then(() => ({ ok: true }))
.catch(() => ({ ok: false }));
},
});