search api

This commit is contained in:
Janis
2022-12-31 05:20:35 +01:00
parent b20abeef41
commit 618b300bef
5 changed files with 48 additions and 4 deletions

11
package-lock.json generated
View File

@@ -17,6 +17,7 @@
"encoding": "^0.1.13",
"eslint": "8.30.0",
"eslint-config-next": "13.0.7",
"flexsearch": "^0.7.31",
"marked": "^4.2.4",
"next": "^13.1.1-canary.1",
"node-html-parser": "^6.1.4",
@@ -2324,6 +2325,11 @@
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ=="
},
"node_modules/flexsearch": {
"version": "0.7.31",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.31.tgz",
"integrity": "sha512-XGozTsMPYkm+6b5QL3Z9wQcJjNYxp0CYn3U1gO7dwD6PAqU1SVWZxI9CCg3z+ml3YfqdPnrBehaBrnH2AGKbNA=="
},
"node_modules/format": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz",
@@ -6441,6 +6447,11 @@
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ=="
},
"flexsearch": {
"version": "0.7.31",
"resolved": "https://registry.npmjs.org/flexsearch/-/flexsearch-0.7.31.tgz",
"integrity": "sha512-XGozTsMPYkm+6b5QL3Z9wQcJjNYxp0CYn3U1gO7dwD6PAqU1SVWZxI9CCg3z+ml3YfqdPnrBehaBrnH2AGKbNA=="
},
"format": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz",

View File

@@ -19,6 +19,7 @@
"encoding": "^0.1.13",
"eslint": "8.30.0",
"eslint-config-next": "13.0.7",
"flexsearch": "^0.7.31",
"marked": "^4.2.4",
"next": "^13.1.1-canary.1",
"node-html-parser": "^6.1.4",

30
pages/api/search.tsx Normal file
View File

@@ -0,0 +1,30 @@
import prisma from "../../lib/prisma";
export default async function search(req, res) {
res.setHeader("Content-Type", "application/json");
let query: string = req.query?.q ?? "";
query = query.toLowerCase().replaceAll("%20", "");
query = query.toLowerCase().replaceAll(" ", "");
if (query.length > 2) {
const articles = await prisma.article.findMany({
select: { title: true, name: true },
}); //TODO order by most viewed
let result = [];
articles.forEach((a) => {
let title = a.title.toLowerCase().replaceAll(" ", "");
title = title.toLowerCase().replaceAll("%20", "");
if (title.includes(query)) {
result.push(a);
}
});
res.end(JSON.stringify(result));
} else {
res.end(JSON.stringify([]));
}
}

View File

@@ -13,10 +13,10 @@ model Article {
title String @unique
markdown String
contentTableEntries ContentTableEntry[]
categoryId Int
category Category @relation(fields: [categoryId], references: [id])
typeId Int
type ArticleType @relation(fields: [typeId], references: [id])
categoryId Int?
category Category? @relation(fields: [categoryId], references: [id])
typeId Int?
type ArticleType? @relation(fields: [typeId], references: [id])
dateCreated DateTime @default(now())
dateUpdated DateTime @default(now())
}

View File

@@ -1,3 +1,5 @@
// This file contains the markdown styles
.markdown {
padding: 0 10px 0 10px;
color: var(--md-color-font);