mirror of
https://github.com/DerTyp7/explainegy-nextjs.git
synced 2025-10-29 21:02:13 +01:00
search api
This commit is contained in:
11
package-lock.json
generated
11
package-lock.json
generated
@@ -17,6 +17,7 @@
|
|||||||
"encoding": "^0.1.13",
|
"encoding": "^0.1.13",
|
||||||
"eslint": "8.30.0",
|
"eslint": "8.30.0",
|
||||||
"eslint-config-next": "13.0.7",
|
"eslint-config-next": "13.0.7",
|
||||||
|
"flexsearch": "^0.7.31",
|
||||||
"marked": "^4.2.4",
|
"marked": "^4.2.4",
|
||||||
"next": "^13.1.1-canary.1",
|
"next": "^13.1.1-canary.1",
|
||||||
"node-html-parser": "^6.1.4",
|
"node-html-parser": "^6.1.4",
|
||||||
@@ -2324,6 +2325,11 @@
|
|||||||
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
|
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
|
||||||
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ=="
|
"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": {
|
"node_modules/format": {
|
||||||
"version": "0.2.2",
|
"version": "0.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz",
|
"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",
|
"resolved": "https://registry.npmjs.org/flatted/-/flatted-3.2.7.tgz",
|
||||||
"integrity": "sha512-5nqDSxl8nn5BSNxyR3n4I6eDmbolI6WT+QqR547RwxQapgjQBmtktdP+HTBb/a/zLsbzERTONyUB5pefh5TtjQ=="
|
"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": {
|
"format": {
|
||||||
"version": "0.2.2",
|
"version": "0.2.2",
|
||||||
"resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz",
|
"resolved": "https://registry.npmjs.org/format/-/format-0.2.2.tgz",
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
"encoding": "^0.1.13",
|
"encoding": "^0.1.13",
|
||||||
"eslint": "8.30.0",
|
"eslint": "8.30.0",
|
||||||
"eslint-config-next": "13.0.7",
|
"eslint-config-next": "13.0.7",
|
||||||
|
"flexsearch": "^0.7.31",
|
||||||
"marked": "^4.2.4",
|
"marked": "^4.2.4",
|
||||||
"next": "^13.1.1-canary.1",
|
"next": "^13.1.1-canary.1",
|
||||||
"node-html-parser": "^6.1.4",
|
"node-html-parser": "^6.1.4",
|
||||||
|
|||||||
30
pages/api/search.tsx
Normal file
30
pages/api/search.tsx
Normal 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([]));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,10 +13,10 @@ model Article {
|
|||||||
title String @unique
|
title String @unique
|
||||||
markdown String
|
markdown String
|
||||||
contentTableEntries ContentTableEntry[]
|
contentTableEntries ContentTableEntry[]
|
||||||
categoryId Int
|
categoryId Int?
|
||||||
category Category @relation(fields: [categoryId], references: [id])
|
category Category? @relation(fields: [categoryId], references: [id])
|
||||||
typeId Int
|
typeId Int?
|
||||||
type ArticleType @relation(fields: [typeId], references: [id])
|
type ArticleType? @relation(fields: [typeId], references: [id])
|
||||||
dateCreated DateTime @default(now())
|
dateCreated DateTime @default(now())
|
||||||
dateUpdated DateTime @default(now())
|
dateUpdated DateTime @default(now())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
// This file contains the markdown styles
|
||||||
|
|
||||||
.markdown {
|
.markdown {
|
||||||
padding: 0 10px 0 10px;
|
padding: 0 10px 0 10px;
|
||||||
color: var(--md-color-font);
|
color: var(--md-color-font);
|
||||||
|
|||||||
Reference in New Issue
Block a user