add search for recipe title

This commit is contained in:
Anika Raemer 2025-10-04 21:04:05 +02:00
parent 7e831cfb64
commit 7ec4324fde
3 changed files with 37 additions and 2 deletions

View file

@ -12,7 +12,7 @@ const router = Router();
// Inject repo + mapper here
const recipeRepository = new RecipeRepository();
const compactRecipeMapper = new CompactRecipeDtoEntityMapper();
const compactRecipeController = new CompactRecipeHandler(recipeRepository, compactRecipeMapper);
const compactRecipeHandler = new CompactRecipeHandler(recipeRepository, compactRecipeMapper);
/**
* Load header data of all recipes
* Responds with a list of CompactRecipeDtos
@ -20,7 +20,9 @@ const compactRecipeController = new CompactRecipeHandler(recipeRepository, compa
router.get(
"/",
asyncHandler(async (req, res) => {
const response = await compactRecipeController.getAllCompactRecipes();
// 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() : "";
const response = await compactRecipeHandler.getMatchingRecipes(searchString);
res.status(201).json(response);
})
);

View file

@ -25,4 +25,19 @@ export class CompactRecipeHandler {
});
return recipeDtos;
}
/**
* Get all recipes matching search
*
* Recipe title must contain type string
* @todo Full text search??
*/
async getMatchingRecipes(searchString : string){
if(!searchString || searchString.length===0){
// get all
return this.getAllCompactRecipes();
} else {
return this.repository.findCompactRecipeBySearch(searchString);
}
}
}

View file

@ -1,6 +1,7 @@
import { AbstractRepository } from "./AbstractRepository.js";
import { RecipeEntity } from "../entities/RecipeEntity.js";
import { AppDataSource } from "../data-source.js";
import { Like } from "typeorm";
export class RecipeRepository extends AbstractRepository<RecipeEntity> {
constructor() {
@ -23,6 +24,23 @@ export class RecipeRepository extends AbstractRepository<RecipeEntity> {
});
}
/**
* Find all recipes matching the search. Currently it only searches on the title. Fetches only recipe header data but no relations.
* @param searchString String to search for
* @returns List of recipe entities matching the search criteria
*/
async findCompactRecipeBySearch(searchString : string): Promise<RecipeEntity[] | null>{
// @todo doesn't work like expected...
return this.repo.find(
{ where: {title: Like(`%${searchString}%`)}}
);
}
/**
* Update recipe and relations
* @param entity Updated entity
* @returns Updated Entity
*/
async updateRecipe(entity: RecipeEntity): Promise<RecipeEntity> {
return AppDataSource.transaction(async (em) => {
// load existing data