feat(recipe): implementada función para leer recetas según su nombre o ingredientes

parent eea006c6
......@@ -10,4 +10,6 @@ public interface ReadRecipeUseCase {
Page<Recipe> readAllByUser(String userId, Pageable pageable);
Page<Recipe> readFavoritesByUser(String userId, Pageable pageable);
Page<Recipe> searchRecipes(String userId, String query, Pageable pageable);
}
......@@ -38,4 +38,10 @@ public class ReadRecipeUseCaseImpl implements ReadRecipeUseCase {
return repository.readFavoritesByUser(userId, pageable)
.map(mapper::mapDocument);
}
@Override
public Page<Recipe> searchRecipes(String userId, String query, Pageable pageable) {
return repository.searchRecipes(userId, query, query, pageable)
.map(mapper::mapDocument);
}
}
......@@ -12,4 +12,6 @@ public interface ReadRecipeRepository {
Page<RecipeDocument> readAllByUser(String userId, Pageable pageable);
Page<RecipeDocument> readFavoritesByUser(String userId, Pageable pageable);
Page<RecipeDocument> searchRecipes(String userId, String name, String ingredients, Pageable pageable);
}
......@@ -50,4 +50,16 @@ public class ReadRecipeController {
return ResponseEntity.ok().body(service.readFavoritesByUser(userId, pageable).map(mapper::mapList));
}
@GetMapping("/search")
public ResponseEntity<Page<RecipeListDto>> searchRecipes(@RequestParam(required = false) String query,
@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "6") int size) {
Pageable pageable = PageRequest.of(page, size);
String userId = SecurityContextHolder.getContext().getAuthentication().getName();
Page<RecipeListDto> dtoPage = service.searchRecipes(userId, query, pageable).map(mapper::mapList);
return ResponseEntity.ok().body(dtoPage);
}
}
......@@ -30,4 +30,9 @@ public class ReadRecipeRepositoryImpl implements ReadRecipeRepository {
public Page<RecipeDocument> readFavoritesByUser(String userId, Pageable pageable) {
return recipeRepository.findByUserIdAndIsFavoriteTrue(userId, pageable);
}
@Override
public Page<RecipeDocument> searchRecipes(String userId, String name, String ingredients, Pageable pageable) {
return recipeRepository.findByUserIdAndNameIgnoreCaseContainingOrIngredientsNameIgnoreCaseContaining(userId, name, ingredients, pageable);
}
}
......@@ -8,4 +8,6 @@ public interface RecipeRepository extends MongoRepository<RecipeDocument, String
Page<RecipeDocument> findByUserId(String userId, Pageable pageable);
Page<RecipeDocument> findByUserIdAndIsFavoriteTrue(String userId, Pageable pageable);
Page<RecipeDocument> findByUserIdAndNameIgnoreCaseContainingOrIngredientsNameIgnoreCaseContaining(String userId, String name, String ingredients, Pageable pageable);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment