135 lines
4 KiB
Rust
135 lines
4 KiB
Rust
use askama::Template;
|
|
use axum::{
|
|
debug_handler,
|
|
extract::{Extension, Path, State},
|
|
response::{Html, IntoResponse, Redirect, Response},
|
|
};
|
|
|
|
use crate::{
|
|
Context, Result,
|
|
data::{db, model},
|
|
html_templates::*,
|
|
translation::Sentence,
|
|
};
|
|
|
|
#[debug_handler]
|
|
pub async fn create(
|
|
State(connection): State<db::Connection>,
|
|
Extension(context): Extension<Context>,
|
|
) -> Result<Response> {
|
|
if let Some(user) = context.user {
|
|
let recipe_id = connection.create_recipe(user.id).await?;
|
|
Ok(Redirect::to(&format!(
|
|
"/{}/recipe/edit/{}",
|
|
context.tr.current_lang_code(),
|
|
recipe_id
|
|
))
|
|
.into_response())
|
|
} else {
|
|
Ok(
|
|
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
|
|
.into_response(),
|
|
)
|
|
}
|
|
}
|
|
|
|
#[debug_handler]
|
|
pub async fn edit(
|
|
State(connection): State<db::Connection>,
|
|
Extension(context): Extension<Context>,
|
|
Path(recipe_id): Path<i64>,
|
|
) -> Result<Response> {
|
|
if let Some(ref user) = context.user {
|
|
if let Some(recipe) = connection.get_recipe(recipe_id, false).await? {
|
|
if user.can_edit_recipe(&recipe) {
|
|
Ok(Html(
|
|
RecipeEditTemplate {
|
|
recipes: Recipes::new(
|
|
connection,
|
|
&context.user,
|
|
context.tr.current_lang_code(),
|
|
Some(recipe_id),
|
|
)
|
|
.await?,
|
|
context,
|
|
recipe,
|
|
}
|
|
.render()?,
|
|
)
|
|
.into_response())
|
|
} else {
|
|
Ok(Html(
|
|
MessageTemplate::new(
|
|
context.tr.t(Sentence::RecipeNotAllowedToEdit),
|
|
context.tr,
|
|
)
|
|
.render()?,
|
|
)
|
|
.into_response())
|
|
}
|
|
} else {
|
|
Ok(Html(
|
|
MessageTemplate::new(context.tr.t(Sentence::RecipeNotFound), context.tr)
|
|
.render()?,
|
|
)
|
|
.into_response())
|
|
}
|
|
} else {
|
|
Ok(
|
|
Html(MessageTemplate::new(context.tr.t(Sentence::NotLoggedIn), context.tr).render()?)
|
|
.into_response(),
|
|
)
|
|
}
|
|
}
|
|
|
|
#[debug_handler]
|
|
pub async fn view(
|
|
State(connection): State<db::Connection>,
|
|
Extension(context): Extension<Context>,
|
|
Path(recipe_id): Path<i64>,
|
|
) -> Result<Response> {
|
|
match connection.get_recipe(recipe_id, true).await? {
|
|
Some(recipe) => {
|
|
if !recipe.is_public
|
|
&& (context.user.is_none() || recipe.user_id != context.user.as_ref().unwrap().id)
|
|
{
|
|
return Ok(Html(
|
|
MessageTemplate::new_with_user(
|
|
&context
|
|
.tr
|
|
.tp(Sentence::RecipeNotAllowedToView, &[Box::new(recipe_id)]),
|
|
context.tr,
|
|
context.user,
|
|
)
|
|
.render()?,
|
|
)
|
|
.into_response());
|
|
}
|
|
|
|
Ok(Html(
|
|
RecipeViewTemplate {
|
|
recipes: Recipes::new(
|
|
connection,
|
|
&context.user,
|
|
context.tr.current_lang_code(),
|
|
Some(recipe_id),
|
|
)
|
|
.await?,
|
|
context,
|
|
recipe,
|
|
}
|
|
.render()?,
|
|
)
|
|
.into_response())
|
|
}
|
|
None => Ok(Html(
|
|
MessageTemplate::new_with_user(
|
|
context.tr.t(Sentence::RecipeNotFound),
|
|
context.tr,
|
|
context.user,
|
|
)
|
|
.render()?,
|
|
)
|
|
.into_response()),
|
|
}
|
|
}
|