From 618b300befb5ca7fa0c9ee9cf26fb9aadbe8a409 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 31 Dec 2022 05:20:35 +0100 Subject: [PATCH] search api --- package-lock.json | 11 +++++++++++ package.json | 1 + pages/api/search.tsx | 30 ++++++++++++++++++++++++++++++ prisma/schema.prisma | 8 ++++---- styles/markdown.scss | 2 ++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 pages/api/search.tsx diff --git a/package-lock.json b/package-lock.json index f149c8d..bd52062 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 939e001..e0d362a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pages/api/search.tsx b/pages/api/search.tsx new file mode 100644 index 0000000..a207dc4 --- /dev/null +++ b/pages/api/search.tsx @@ -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([])); + } +} diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 7834824..5c49320 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -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()) } diff --git a/styles/markdown.scss b/styles/markdown.scss index 2789e8c..a2e9331 100644 --- a/styles/markdown.scss +++ b/styles/markdown.scss @@ -1,3 +1,5 @@ +// This file contains the markdown styles + .markdown { padding: 0 10px 0 10px; color: var(--md-color-font);