fix core middleware and make search case insensitive

This commit is contained in:
Anika Raemer 2025-10-07 19:26:59 +02:00
parent 7ec4324fde
commit 760c91af56
3 changed files with 19 additions and 3 deletions

View file

@ -22,6 +22,7 @@ router.get(
asyncHandler(async (req, res) => {
// extract search string from query parameters, convert to lower case for case insensitive search
const searchString : string = req.query.search ? req.query.search.toString().toLowerCase() : "";
console.log("Searching for recipes with title containing", searchString)
const response = await compactRecipeHandler.getMatchingRecipes(searchString);
res.status(201).json(response);
})

View file

@ -27,6 +27,21 @@ async function startServer() {
await AppDataSource.runMigrations();
console.log("Migrations executed");
// Enable CORS before anything else
// @todo move to middleware util
app.use((req: Request, res: Response, next: NextFunction) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests quickly
if (req.method === 'OPTIONS') {
return res.sendStatus(200);
}
next();
});
// Activate Authentication
app.use(authentication);

View file

@ -1,7 +1,7 @@
import { AbstractRepository } from "./AbstractRepository.js";
import { RecipeEntity } from "../entities/RecipeEntity.js";
import { AppDataSource } from "../data-source.js";
import { Like } from "typeorm";
import { ILike, Like } from "typeorm";
export class RecipeRepository extends AbstractRepository<RecipeEntity> {
constructor() {
@ -32,7 +32,7 @@ export class RecipeRepository extends AbstractRepository<RecipeEntity> {
async findCompactRecipeBySearch(searchString : string): Promise<RecipeEntity[] | null>{
// @todo doesn't work like expected...
return this.repo.find(
{ where: {title: Like(`%${searchString}%`)}}
{ where: {title: ILike(`%${searchString}%`)}}
);
}