forked from alexeagleson/nextjs-fullstack-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement search API and results page query
- Loading branch information
1 parent
3c4cf38
commit f7321a2
Showing
10 changed files
with
149 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
[ | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Cat", | ||
"title": "This is a link to a search result about cats", | ||
"text": "Did you know their whiskers can sense vibrations in the air? Description of the search result. The description might be a bit long and it will tell you everything you need to know about the search result." | ||
}, | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Dog", | ||
"title": "This is a link to a search result about dogs", | ||
"text": "They sure do love to bark. Description of the search result. The description might be a bit long and it will tell you everything you need to know about the search result." | ||
}, | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Cats_%26_Dogs", | ||
"title": "This is a link to a search result about both cats and dogs", | ||
"text": "Both of them have tails. Description of the search result. The description might be a bit long and it will tell you everything you need to know about the search result." | ||
}, | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Broccoli", | ||
"title": "This is a link to a search result about broccoli", | ||
"text": "Broccoli was invented by crossing cauliflower with pea seeds. Description of the search result. The description might be a bit long and it will tell you everything you need to know about the search result." | ||
}, | ||
{ | ||
"url": "https://en.wikipedia.org/wiki/Cauliflower", | ||
"title": "This is a link to a search result about cauliflower", | ||
"text": "Who invented cauliflower? Description of the search result. The description might be a bit long and it will tell you everything you need to know about the search result." | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface ISearchData { | ||
url: string; | ||
title: string; | ||
text: string; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import type { NextApiRequest, NextApiResponse } from 'next'; | ||
import database from '../../../lib/search/database.json'; | ||
import { ISearchData } from '../../../lib/search/types'; | ||
|
||
interface IApiSearchRequest extends NextApiRequest { | ||
body: { searchTerm?: string }; | ||
} | ||
|
||
export type IApiSearchResponseData = ISearchData[]; | ||
|
||
export default function handler( | ||
req: IApiSearchRequest, | ||
res: NextApiResponse<IApiSearchResponseData> | ||
) { | ||
const { | ||
body: { searchTerm }, | ||
} = req; | ||
|
||
if (req.method === 'POST' && searchTerm && searchTerm.length > 0) { | ||
// Creates a regex search pattern for a case insensitive match from the user's search term | ||
const searchPattern = new RegExp(searchTerm, 'i'); | ||
|
||
const filteredResults = database.filter((result) => { | ||
return ( | ||
// Check the user's search term again either the title or the text of the database entry | ||
searchPattern.test(result.title) || searchPattern.test(result.text) | ||
); | ||
}); | ||
res.status(200).json(filteredResults); | ||
} else { | ||
res.status(400).json([]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters