use axum::{ debug_handler, extract::{Extension, Path, State}, response::{IntoResponse, Redirect, Response, Result}, }; // use tracing::{event, Level}; use crate::{ consts, data::{db, model}, html_templates::*, }; ///// RECIPE ///// #[debug_handler] pub async fn create( State(connection): State, Extension(user): Extension>, ) -> Result { if let Some(user) = user { let recipe_id = connection.create_recipe(user.id).await?; Ok(Redirect::to(&format!("/recipe/edit/{}", recipe_id)).into_response()) } else { Ok(MessageTemplate::new("Not logged in").into_response()) } } #[debug_handler] pub async fn edit_recipe( State(connection): State, Extension(user): Extension>, Path(recipe_id): Path, ) -> Result { if let Some(user) = user { let recipe = connection.get_recipe(recipe_id).await?.unwrap(); if recipe.user_id == user.id { Ok(RecipeEditTemplate { user: Some(user), recipes: Recipes { list: connection.get_all_recipe_titles().await?, current_id: Some(recipe_id), }, recipe, languages: consts::LANGUAGES, } .into_response()) } else { Ok(MessageTemplate::new("Unable to edit this recipe").into_response()) } } else { Ok(MessageTemplate::new("Not logged in").into_response()) } } #[debug_handler] pub async fn view( State(connection): State, Extension(user): Extension>, Path(recipe_id): Path, ) -> Result { let recipes = connection.get_all_recipe_titles().await?; match connection.get_recipe(recipe_id).await? { Some(recipe) => Ok(RecipeViewTemplate { user, recipes: Recipes { list: recipes, current_id: Some(recipe.id), }, recipe, } .into_response()), None => Ok(MessageTemplate::new_with_user( &format!("Cannot find the recipe {}", recipe_id), user, ) .into_response()), } }