From 23c9a98f66912cac68b2e697733ca44ed182264e Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Thu, 24 Apr 2025 13:23:31 +0800 Subject: [PATCH] feat: Add endpoint for scanning and rebuilding recipe cache, and update UI to use new refresh method --- py/routes/recipe_routes.py | 24 ++++++++++++++++++++++++ static/js/recipes.js | 26 ++++++++++++++++++++++++++ templates/recipes.html | 2 +- 3 files changed, 51 insertions(+), 1 deletion(-) diff --git a/py/routes/recipe_routes.py b/py/routes/recipe_routes.py index 2b97832b..3cba14a3 100644 --- a/py/routes/recipe_routes.py +++ b/py/routes/recipe_routes.py @@ -74,6 +74,9 @@ class RecipeRoutes: # Add route to get recipes for a specific Lora app.router.add_get('/api/recipes/for-lora', routes.get_recipes_for_lora) + + # Add new endpoint for scanning and rebuilding the recipe cache + app.router.add_get('/api/recipes/scan', routes.scan_recipes) async def _init_cache(self, app): """Initialize cache on startup""" @@ -1255,3 +1258,24 @@ class RecipeRoutes: except Exception as e: logger.error(f"Error getting recipes for Lora: {str(e)}") return web.json_response({'success': False, 'error': str(e)}, status=500) + + async def scan_recipes(self, request: web.Request) -> web.Response: + """API endpoint for scanning and rebuilding the recipe cache""" + try: + # Ensure services are initialized + await self.init_services() + + # Force refresh the recipe cache + logger.info("Manually triggering recipe cache rebuild") + await self.recipe_scanner.get_cached_data(force_refresh=True) + + return web.json_response({ + 'success': True, + 'message': 'Recipe cache refreshed successfully' + }) + except Exception as e: + logger.error(f"Error refreshing recipe cache: {e}", exc_info=True) + return web.json_response({ + 'success': False, + 'error': str(e) + }, status=500) diff --git a/static/js/recipes.js b/static/js/recipes.js index 875c928e..012478b9 100644 --- a/static/js/recipes.js +++ b/static/js/recipes.js @@ -268,6 +268,32 @@ class RecipeManager { } } + /** + * Refreshes the recipe list by first rebuilding the cache and then loading recipes + */ + async refreshRecipes() { + try { + // Call the new endpoint to rebuild the recipe cache + const response = await fetch('/api/recipes/scan'); + + if (!response.ok) { + const data = await response.json(); + throw new Error(data.error || 'Failed to refresh recipe cache'); + } + + // After successful cache rebuild, load the recipes + await this.loadRecipes(true); + + appCore.showToast('Refresh complete', 'success'); + } catch (error) { + console.error('Error refreshing recipes:', error); + appCore.showToast(error.message || 'Failed to refresh recipes', 'error'); + + // Still try to load recipes even if scan failed + await this.loadRecipes(true); + } + } + async _loadSpecificRecipe(recipeId) { try { // Fetch specific recipe by ID diff --git a/templates/recipes.html b/templates/recipes.html index 4234fd77..93248209 100644 --- a/templates/recipes.html +++ b/templates/recipes.html @@ -37,7 +37,7 @@
- +