-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from crux-bphc/update-search-service-after-ti…
…metable-update Update search service after timetable update and add search backend endpoint
- Loading branch information
Showing
4 changed files
with
160 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { Request, Response } from "express"; | ||
import { z } from "zod"; | ||
import { namedNonEmptyStringType } from "../../../../lib/src/zodFieldTypes.js"; | ||
import { env } from "../../config/server.js"; | ||
import { Timetable } from "../../entity/entities.js"; | ||
import { validate } from "../../middleware/zodValidateRequest.js"; | ||
|
||
const searchTimetableSchema = z.object({ | ||
query: z.object({ | ||
query: namedNonEmptyStringType("query"), | ||
}), | ||
}); | ||
|
||
export const searchTimetableValidator = validate(searchTimetableSchema); | ||
|
||
export const searchTimetable = async (req: Request, res: Response) => { | ||
try { | ||
const { query } = req.query; | ||
|
||
const searchServiceURL = `${env.SEARCH_SERVICE_URL}/timetable/search?query=${query}`; | ||
|
||
const response = await fetch(searchServiceURL, { | ||
method: "GET", | ||
headers: { "Content-Type": "application/json" }, | ||
}); | ||
|
||
const searchResults = await response.json(); | ||
|
||
if (!response.ok) { | ||
console.log("Error while searching timetable: ", searchResults.error); | ||
return res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
|
||
const timetables = searchResults.map( | ||
(el: { timetable: Timetable; score: string }) => el.timetable, | ||
); | ||
|
||
return res.json(timetables); | ||
} catch (err) { | ||
console.log(err); | ||
return res.status(500).json({ message: "Internal Server Error" }); | ||
} | ||
}; |
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