feat: Add endpoint for scanning and rebuilding recipe cache, and update UI to use new refresh method

This commit is contained in:
Will Miao
2025-04-24 13:23:31 +08:00
parent 796fc33b5b
commit 23c9a98f66
3 changed files with 51 additions and 1 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -37,7 +37,7 @@
<div class="controls">
<div class="action-buttons">
<div title="Refresh recipe list" class="control-group">
<button onclick="recipeManager.loadRecipes(true)"><i class="fas fa-sync"></i> Refresh</button>
<button onclick="recipeManager.refreshRecipes()"><i class="fas fa-sync"></i> Refresh</button>
</div>
<div title="Import recipes" class="control-group">
<button onclick="importManager.showImportModal()"><i class="fas fa-file-import"></i> Import</button>