Refresh indicies

This commit is contained in:
eraden 2024-11-17 20:04:59 +01:00
parent 31e0920a50
commit 0499c71fbb
2 changed files with 22 additions and 1 deletions

View File

@ -693,9 +693,9 @@ async fn recipe_form(admin: Identity, db: Data<DatabaseConnection>) -> RecipeFor
#[post("/create")]
async fn create_recipe(
admin: Identity,
// form: actix_web::web::Json<RecipeForm>,
form: QsForm<RecipeForm>,
db: Data<DatabaseConnection>,
search: Data<crate::actors::search::Search>,
) -> Result<HttpResponse, Error> {
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<DatabaseConnection>,
form: QsForm<EditRecipeForm>,
search: Data<crate::actors::search::Search>,
) -> Result<HttpResponse, Error> {
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())

View File

@ -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<Vec<(String, Option<String>, String)
pub fn parse_tags(s: &str) -> Result<Vec<String>, Error> {
s.lines().map(|line| Ok(line.to_string())).try_collect()
}
pub async fn refresh_records(db: Data<DatabaseConnection>, search: Data<Search>) {
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::<Vec<_>>();
let _ = search.send(Refresh { records }).await;
}