mirror of
				https://github.com/DerTyp7/explainegy-nextjs.git
				synced 2025-10-31 05:37:12 +01:00 
			
		
		
		
	add api
This commit is contained in:
		| @@ -1,31 +1,31 @@ | ||||
| import prisma from "../../lib/prisma"; | ||||
|  | ||||
| export default async function search(req, res) { | ||||
| 	res.setHeader("Content-Type", "application/json"); | ||||
| 	let query: string = req.query?.q ?? ""; | ||||
| export default async function handler(req, res) { | ||||
|   res.setHeader("Content-Type", "application/json"); | ||||
|   let query: string = req.query?.q ?? ""; | ||||
|  | ||||
| 	query = query.toLowerCase().replaceAll("%20", ""); | ||||
| 	query = query.toLowerCase().replaceAll(" ", ""); | ||||
|   query = query.toLowerCase().replaceAll("%20", ""); | ||||
|   query = query.toLowerCase().replaceAll(" ", ""); | ||||
|  | ||||
| 	if (query.length > 1) { | ||||
| 		const articles = await prisma.article.findMany({ | ||||
| 			select: { title: true, name: true }, | ||||
| 			take: 5, | ||||
| 		}); //TODO order by most viewed | ||||
|   if (query.length > 0) { | ||||
|     const articles = await prisma.article.findMany({ | ||||
|       select: { title: true, name: true }, | ||||
|       take: 5, | ||||
|     }); //TODO order by most viewed | ||||
|  | ||||
| 		let result = []; | ||||
|     let result = []; | ||||
|  | ||||
| 		articles.forEach((a) => { | ||||
| 			let title = a.title.toLowerCase().replaceAll(" ", ""); | ||||
| 			title = title.toLowerCase().replaceAll("%20", ""); | ||||
|     articles.forEach((a) => { | ||||
|       let title = a.title.toLowerCase().replaceAll(" ", ""); | ||||
|       title = title.toLowerCase().replaceAll("%20", ""); | ||||
|  | ||||
| 			if (title.includes(query)) { | ||||
| 				result.push(a); | ||||
| 			} | ||||
| 		}); | ||||
|       if (title.includes(query)) { | ||||
|         result.push(a); | ||||
|       } | ||||
|     }); | ||||
|  | ||||
| 		res.end(JSON.stringify(result)); | ||||
| 	} else { | ||||
| 		res.end(JSON.stringify([])); | ||||
| 	} | ||||
|     res.end(JSON.stringify(result)); | ||||
|   } else { | ||||
|     res.end(JSON.stringify([])); | ||||
|   } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Janis
					Janis