From 0499c71fbbc92002778384a1c90f19bb3ff2a87b Mon Sep 17 00:00:00 2001 From: eraden Date: Sun, 17 Nov 2024 20:04:59 +0100 Subject: [PATCH] Refresh indicies --- cooked/src/routes.rs | 5 ++++- cooked/src/utils.rs | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/cooked/src/routes.rs b/cooked/src/routes.rs index 254eae1..0d544cf 100644 --- a/cooked/src/routes.rs +++ b/cooked/src/routes.rs @@ -693,9 +693,9 @@ async fn recipe_form(admin: Identity, db: Data) -> RecipeFor #[post("/create")] async fn create_recipe( admin: Identity, - // form: actix_web::web::Json, form: QsForm, db: Data, + search: Data, ) -> Result { let mut form = form.into_inner(); form.session = admin.id().ok(); @@ -728,6 +728,7 @@ async fn create_recipe( tracing::error!("Failed to copy file: {}", failure.image_url); return Ok(HttpResponse::InternalServerError().finish()); }; + crate::utils::refresh_records(db, search).await; Ok(HttpResponse::SeeOther() .append_header(("location", format!("/recipe/{recipe_id}").as_str())) .finish()) @@ -1127,6 +1128,7 @@ async fn update_recipe( admin: Identity, db: Data, form: QsForm, + search: Data, ) -> Result { let mut form = form.into_inner(); form.session = admin.id().ok(); @@ -1150,6 +1152,7 @@ async fn update_recipe( } Ok(recipe_id) => { let _ = t.commit().await; + crate::utils::refresh_records(db, search).await; Ok(HttpResponse::SeeOther() .append_header(("location", format!("/recipe/{recipe_id}").as_str())) .finish()) diff --git a/cooked/src/utils.rs b/cooked/src/utils.rs index cc072fd..5dce03c 100644 --- a/cooked/src/utils.rs +++ b/cooked/src/utils.rs @@ -1,3 +1,7 @@ +use actix_web::web::Data; +use sea_orm::DatabaseConnection; + +use crate::actors::search::Search; use crate::types::Error; use crate::types::Error::*; @@ -57,3 +61,17 @@ pub fn parse_ingredients(s: &str) -> Result, String) pub fn parse_tags(s: &str) -> Result, Error> { s.lines().map(|line| Ok(line.to_string())).try_collect() } + +pub async fn refresh_records(db: Data, search: Data) { + use crate::actors::search::*; + use sea_orm::prelude::*; + + let records = crate::entities::prelude::Recipies::find() + .all(&**db) + .await + .unwrap_or_default() + .into_iter() + .map(|recipe| RecipeRecord::from(recipe)) + .collect::>(); + let _ = search.send(Refresh { records }).await; +}