fix core middleware and make search case insensitive
This commit is contained in:
parent
7ec4324fde
commit
760c91af56
3 changed files with 19 additions and 3 deletions
|
|
@ -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);
|
||||
})
|
||||
|
|
|
|||
17
src/index.ts
17
src/index.ts
|
|
@ -27,9 +27,24 @@ 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);
|
||||
|
||||
|
||||
// Setup routes
|
||||
app.use(authBasicRoute, authRoutes);
|
||||
app.use("/user", userRoutes);
|
||||
|
|
|
|||
|
|
@ -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}%`)}}
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue