-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchController.ts
45 lines (42 loc) · 926 Bytes
/
searchController.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 prisma from "#instances/prisma";
import { EnBookStatus } from "@prisma/client";
import { Request, Response } from "express";
import { isMobile } from '#helpers/index';
import { BOOKS_COUNT_DESK, BOOKS_COUNT_MOBILE } from "#config/index";
const index = async (req: Request, res: Response) => {
const query = (req.query.query as string).trim();
const mobile = isMobile(req.get('user-agent') || '');
const page = Number(req.query.page) || 1;
const limit = mobile ? BOOKS_COUNT_MOBILE : BOOKS_COUNT_DESK;
const books = await prisma.book.findMany({
where: {
status: {
not: EnBookStatus.DRAFT,
},
OR: [
{
title: {
contains: query
}
},
{
titleEn: {
contains: query
}
},
],
},
skip: (page - 1) * limit,
take: limit,
orderBy: {
title: 'asc'
},
include: {
lastUploadChapter: true,
}
});
res.json(books);
}
export default {
index,
}