Simplify web API (put ids in url)
This commit is contained in:
parent
0c43935bef
commit
6e017e41a3
14 changed files with 403 additions and 588 deletions
|
|
@ -133,92 +133,99 @@ pub fn make_service(
|
|||
// .route("/user/update", put(services::ron::update_user))
|
||||
.route("/lang", put(services::ron::set_lang))
|
||||
.route("/recipe/titles", get(services::ron::recipe::get_titles))
|
||||
.route("/recipe/title", patch(services::ron::recipe::set_title))
|
||||
.route(
|
||||
"/recipe/description",
|
||||
"/recipe/{id}/title",
|
||||
patch(services::ron::recipe::set_title),
|
||||
)
|
||||
.route(
|
||||
"/recipe/{id}/description",
|
||||
patch(services::ron::recipe::set_description),
|
||||
)
|
||||
.route(
|
||||
"/recipe/servings",
|
||||
"/recipe/{id}/servings",
|
||||
patch(services::ron::recipe::set_servings),
|
||||
)
|
||||
.route(
|
||||
"/recipe/estimated_time",
|
||||
"/recipe/{id}/estimated_time",
|
||||
patch(services::ron::recipe::set_estimated_time),
|
||||
)
|
||||
.route(
|
||||
"/recipe/tags",
|
||||
"/recipe/{id}/tags",
|
||||
get(services::ron::recipe::get_tags)
|
||||
.post(services::ron::recipe::add_tags)
|
||||
.delete(services::ron::recipe::rm_tags),
|
||||
)
|
||||
// TODO
|
||||
// .route("/recipe/tags".get(services::ron::recipe::get_all_tags))
|
||||
.route(
|
||||
"/recipe/difficulty",
|
||||
"/recipe/{id}/difficulty",
|
||||
patch(services::ron::recipe::set_difficulty),
|
||||
)
|
||||
.route(
|
||||
"/recipe/language",
|
||||
"/recipe/{id}/language",
|
||||
patch(services::ron::recipe::set_language),
|
||||
)
|
||||
.route(
|
||||
"/recipe/is_public",
|
||||
"/recipe/{id}/is_public",
|
||||
patch(services::ron::recipe::set_is_public),
|
||||
)
|
||||
.route("/recipe", delete(services::ron::recipe::rm))
|
||||
.route("/recipe/groups", get(services::ron::recipe::get_groups))
|
||||
.route("/recipe/{id}", delete(services::ron::recipe::rm))
|
||||
.route(
|
||||
"/recipe/group",
|
||||
post(services::ron::recipe::add_group).delete(services::ron::recipe::rm_group),
|
||||
"/recipe/{id}/groups",
|
||||
get(services::ron::recipe::get_groups),
|
||||
)
|
||||
.route("/recipe/{id}/group", post(services::ron::recipe::add_group))
|
||||
.route(
|
||||
"/recipe/group_name",
|
||||
"/groups/order",
|
||||
patch(services::ron::recipe::set_groups_order),
|
||||
)
|
||||
.route("/group/{id}", delete(services::ron::recipe::rm_group))
|
||||
.route(
|
||||
"/group/{id}/name",
|
||||
patch(services::ron::recipe::set_group_name),
|
||||
)
|
||||
.route(
|
||||
"/recipe/group_comment",
|
||||
"/group/{id}/comment",
|
||||
patch(services::ron::recipe::set_group_comment),
|
||||
)
|
||||
.route("/group/{id}/step", post(services::ron::recipe::add_step))
|
||||
.route(
|
||||
"/recipe/groups_order",
|
||||
patch(services::ron::recipe::set_groups_order),
|
||||
"/steps/order",
|
||||
patch(services::ron::recipe::set_steps_order),
|
||||
)
|
||||
.route("/step/{id}", delete(services::ron::recipe::rm_step))
|
||||
.route(
|
||||
"/recipe/step",
|
||||
post(services::ron::recipe::add_step).delete(services::ron::recipe::rm_step),
|
||||
)
|
||||
.route(
|
||||
"/recipe/step_action",
|
||||
"/step/{id}/action",
|
||||
patch(services::ron::recipe::set_step_action),
|
||||
)
|
||||
.route(
|
||||
"/recipe/steps_order",
|
||||
patch(services::ron::recipe::set_steps_order),
|
||||
"/step/{id}/ingredient",
|
||||
post(services::ron::recipe::add_ingredient),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredient",
|
||||
post(services::ron::recipe::add_ingredient)
|
||||
.delete(services::ron::recipe::rm_ingredient),
|
||||
"/ingredients/order",
|
||||
patch(services::ron::recipe::set_ingredients_order),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredient_name",
|
||||
"/ingredient/{id}",
|
||||
delete(services::ron::recipe::rm_ingredient),
|
||||
)
|
||||
.route(
|
||||
"/ingredient/{id}/name",
|
||||
patch(services::ron::recipe::set_ingredient_name),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredient_comment",
|
||||
"/ingredient/{id}/comment",
|
||||
patch(services::ron::recipe::set_ingredient_comment),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredient_quantity",
|
||||
"/ingredient/{id}/quantity",
|
||||
patch(services::ron::recipe::set_ingredient_quantity),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredient_unit",
|
||||
"/ingredient/{id}/unit",
|
||||
patch(services::ron::recipe::set_ingredient_unit),
|
||||
)
|
||||
.route(
|
||||
"/recipe/ingredients_order",
|
||||
patch(services::ron::recipe::set_ingredients_order),
|
||||
)
|
||||
.route(
|
||||
"/calendar/scheduled_recipes",
|
||||
get(services::ron::calendar::get_scheduled_recipes),
|
||||
|
|
@ -233,7 +240,7 @@ pub fn make_service(
|
|||
"/shopping_list/checked",
|
||||
patch(services::ron::shopping_list::set_entry_checked),
|
||||
)
|
||||
.route("/translation", get(services::ron::get_translation))
|
||||
.route("/translation/{id}", get(services::ron::get_translation))
|
||||
.fallback(services::ron::not_found);
|
||||
|
||||
let fragments_routes = Router::new().route(
|
||||
|
|
|
|||
|
|
@ -431,7 +431,7 @@ ORDER BY [name]
|
|||
{
|
||||
let mut tx = self.tx().await?;
|
||||
for tag in tags {
|
||||
let tag = tag.as_ref().trim().to_lowercase();
|
||||
let tag = normalize_tag(tag.as_ref());
|
||||
let tag_id: i64 = if let Some(tag_id) =
|
||||
sqlx::query_scalar("SELECT [id] FROM [Tag] WHERE [name] = $1")
|
||||
.bind(&tag)
|
||||
|
|
@ -471,6 +471,7 @@ ON CONFLICT DO NOTHING
|
|||
{
|
||||
let mut tx = self.tx().await?;
|
||||
for tag in tags {
|
||||
let tag = normalize_tag(tag.as_ref());
|
||||
if let Some(tag_id) = sqlx::query_scalar::<_, i64>(
|
||||
r#"
|
||||
DELETE FROM [RecipeTag]
|
||||
|
|
@ -483,7 +484,7 @@ RETURNING [RecipeTag].[tag_id]
|
|||
"#,
|
||||
)
|
||||
.bind(recipe_id)
|
||||
.bind(tag.as_ref())
|
||||
.bind(tag)
|
||||
.fetch_optional(&mut *tx)
|
||||
.await?
|
||||
{
|
||||
|
|
@ -940,6 +941,10 @@ ORDER BY [date]
|
|||
}
|
||||
}
|
||||
|
||||
pub fn normalize_tag(tag: &str) -> String {
|
||||
tag.to_lowercase()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, Query, State},
|
||||
extract::{Extension, Path, Query, State},
|
||||
http::{HeaderMap, StatusCode},
|
||||
response::{IntoResponse, Result},
|
||||
};
|
||||
|
|
@ -25,14 +25,14 @@ pub async fn set_lang(
|
|||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
headers: HeaderMap,
|
||||
ExtractRon(ron): ExtractRon<common::ron_api::SetLang>,
|
||||
ExtractRon(lang): ExtractRon<String>,
|
||||
) -> Result<(CookieJar, StatusCode)> {
|
||||
let mut jar = CookieJar::from_headers(&headers);
|
||||
if let Some(user) = context.user {
|
||||
connection.set_user_lang(user.id, &ron.lang).await?;
|
||||
connection.set_user_lang(user.id, &lang).await?;
|
||||
} else {
|
||||
// Only set the cookie if the user is not connected.
|
||||
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, ron.lang))
|
||||
let cookie = Cookie::build((consts::COOKIE_LANG_NAME, lang))
|
||||
.same_site(SameSite::Lax)
|
||||
.path("/");
|
||||
jar = jar.add(cookie);
|
||||
|
|
@ -43,11 +43,9 @@ pub async fn set_lang(
|
|||
#[debug_handler]
|
||||
pub async fn get_translation(
|
||||
Extension(context): Extension<Context>,
|
||||
translation_id: Query<ron_api::Id>,
|
||||
Path(id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(ron_api::Value {
|
||||
value: context.tr.t_from_id(translation_id.id),
|
||||
}))
|
||||
Ok(ron_response_ok(context.tr.t_from_id(id)))
|
||||
}
|
||||
|
||||
/*** 404 ***/
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
use axum::{
|
||||
debug_handler,
|
||||
extract::{Extension, State},
|
||||
extract::{Extension, Path, State},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Result},
|
||||
};
|
||||
use axum_extra::extract::Query;
|
||||
use common::ron_api;
|
||||
// use tracing::{event, Level};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
app::Context, data::db, data::model, ron_extractor::ExtractRon, ron_utils::ron_response_ok,
|
||||
|
|
@ -19,23 +19,22 @@ use super::rights::*;
|
|||
#[debug_handler]
|
||||
pub async fn get_titles(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_ids: Query<ron_api::Ids>,
|
||||
recipe_ids: Query<Vec<i64>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(ron_api::Strings {
|
||||
strs: connection.get_recipe_titles(&recipe_ids.ids).await?,
|
||||
}))
|
||||
Ok(ron_response_ok(
|
||||
connection.get_recipe_titles(&recipe_ids).await?,
|
||||
))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_title(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeTitle>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(title): ExtractRon<String>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_title(ron.recipe_id, &ron.title)
|
||||
.await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.set_recipe_title(recipe_id, &title).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -43,11 +42,12 @@ pub async fn set_title(
|
|||
pub async fn set_description(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeDescription>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(description): ExtractRon<String>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_description(ron.recipe_id, &ron.description)
|
||||
.set_recipe_description(recipe_id, &description)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -56,12 +56,11 @@ pub async fn set_description(
|
|||
pub async fn set_servings(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeServings>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(servings): ExtractRon<Option<u32>>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_servings(ron.recipe_id, ron.servings)
|
||||
.await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.set_recipe_servings(recipe_id, servings).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -69,11 +68,12 @@ pub async fn set_servings(
|
|||
pub async fn set_estimated_time(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeEstimatedTime>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(time): ExtractRon<Option<u32>>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_estimated_time(ron.recipe_id, ron.estimated_time)
|
||||
.set_recipe_estimated_time(recipe_id, time)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -81,30 +81,22 @@ pub async fn set_estimated_time(
|
|||
#[debug_handler]
|
||||
pub async fn get_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<ron_api::Id>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
Ok(ron_response_ok(ron_api::Tags {
|
||||
recipe_id: recipe_id.id,
|
||||
tags: connection.get_recipes_tags(recipe_id.id).await?,
|
||||
}))
|
||||
Ok(ron_response_ok(
|
||||
connection.get_recipes_tags(recipe_id).await?,
|
||||
))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn add_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Tags>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(tags): ExtractRon<Vec<String>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
connection
|
||||
.add_recipe_tags(
|
||||
ron.recipe_id,
|
||||
&ron.tags
|
||||
.into_iter()
|
||||
.map(|tag| tag.to_lowercase())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.add_recipe_tags(recipe_id, &tags).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -112,18 +104,11 @@ pub async fn add_tags(
|
|||
pub async fn rm_tags(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Tags>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(tags): ExtractRon<Vec<String>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
connection
|
||||
.rm_recipe_tags(
|
||||
ron.recipe_id,
|
||||
&ron.tags
|
||||
.into_iter()
|
||||
.map(|tag| tag.to_lowercase())
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.rm_recipe_tags(recipe_id, &tags).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -131,11 +116,12 @@ pub async fn rm_tags(
|
|||
pub async fn set_difficulty(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeDifficulty>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(difficulty): ExtractRon<ron_api::Difficulty>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_difficulty(ron.recipe_id, ron.difficulty)
|
||||
.set_recipe_difficulty(recipe_id, difficulty)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -144,20 +130,19 @@ pub async fn set_difficulty(
|
|||
pub async fn set_language(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeLanguage>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(lang): ExtractRon<String>,
|
||||
) -> Result<StatusCode> {
|
||||
if !crate::translation::available_codes()
|
||||
.iter()
|
||||
.any(|&l| l == ron.lang)
|
||||
.any(|&l| l == lang)
|
||||
{
|
||||
// TODO: log?
|
||||
warn!("Can't find language: {}", lang);
|
||||
return Ok(StatusCode::BAD_REQUEST);
|
||||
}
|
||||
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_language(ron.recipe_id, &ron.lang)
|
||||
.await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.set_recipe_language(recipe_id, &lang).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -165,11 +150,12 @@ pub async fn set_language(
|
|||
pub async fn set_is_public(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetRecipeIsPublic>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
ExtractRon(is_public): ExtractRon<bool>,
|
||||
) -> Result<StatusCode> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.recipe_id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection
|
||||
.set_recipe_is_public(ron.recipe_id, ron.is_public)
|
||||
.set_recipe_is_public(recipe_id, is_public)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -178,10 +164,10 @@ pub async fn set_is_public(
|
|||
pub async fn rm(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.id).await?;
|
||||
connection.rm_recipe(ron.id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
connection.rm_recipe(recipe_id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -225,12 +211,12 @@ impl From<model::Ingredient> for ron_api::Ingredient {
|
|||
#[debug_handler]
|
||||
pub async fn get_groups(
|
||||
State(connection): State<db::Connection>,
|
||||
recipe_id: Query<ron_api::Id>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
// Here we don't check user rights on purpose.
|
||||
Ok(ron_response_ok(
|
||||
connection
|
||||
.get_groups(recipe_id.id)
|
||||
.get_groups(recipe_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(ron_api::Group::from)
|
||||
|
|
@ -242,22 +228,32 @@ pub async fn get_groups(
|
|||
pub async fn add_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(recipe_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe(&connection, &context.user, ron.id).await?;
|
||||
let id = connection.add_recipe_group(ron.id).await?;
|
||||
check_user_rights_recipe(&connection, &context.user, recipe_id).await?;
|
||||
let id = connection.add_recipe_group(recipe_id).await?;
|
||||
Ok(ron_response_ok(id))
|
||||
}
|
||||
|
||||
Ok(ron_response_ok(ron_api::Id { id }))
|
||||
#[debug_handler]
|
||||
pub async fn set_groups_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ids): ExtractRon<Vec<i64>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_groups(&connection, &context.user, &ids).await?;
|
||||
connection.set_groups_order(&ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_group(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(groupe_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &context.user, ron.id).await?;
|
||||
connection.rm_recipe_group(ron.id).await?;
|
||||
check_user_rights_recipe_group(&connection, &context.user, groupe_id).await?;
|
||||
connection.rm_recipe_group(groupe_id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -265,10 +261,11 @@ pub async fn rm_group(
|
|||
pub async fn set_group_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetGroupName>,
|
||||
Path(group_id): Path<i64>,
|
||||
ExtractRon(name): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &context.user, ron.group_id).await?;
|
||||
connection.set_group_name(ron.group_id, &ron.name).await?;
|
||||
check_user_rights_recipe_group(&connection, &context.user, group_id).await?;
|
||||
connection.set_group_name(group_id, &name).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -276,23 +273,11 @@ pub async fn set_group_name(
|
|||
pub async fn set_group_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetGroupComment>,
|
||||
Path(group_id): Path<i64>,
|
||||
ExtractRon(comment): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &context.user, ron.group_id).await?;
|
||||
connection
|
||||
.set_group_comment(ron.group_id, &ron.comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_groups_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_groups(&connection, &context.user, &ron.ids).await?;
|
||||
connection.set_groups_order(&ron.ids).await?;
|
||||
check_user_rights_recipe_group(&connection, &context.user, group_id).await?;
|
||||
connection.set_group_comment(group_id, &comment).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -300,22 +285,32 @@ pub async fn set_groups_order(
|
|||
pub async fn add_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(group_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_group(&connection, &context.user, ron.id).await?;
|
||||
let id = connection.add_recipe_step(ron.id).await?;
|
||||
check_user_rights_recipe_group(&connection, &context.user, group_id).await?;
|
||||
let id = connection.add_recipe_step(group_id).await?;
|
||||
Ok(ron_response_ok(id))
|
||||
}
|
||||
|
||||
Ok(ron_response_ok(ron_api::Id { id }))
|
||||
#[debug_handler]
|
||||
pub async fn set_steps_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ids): ExtractRon<Vec<i64>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_steps(&connection, &context.user, &ids).await?;
|
||||
connection.set_steps_order(&ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_step(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(step_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &context.user, ron.id).await?;
|
||||
connection.rm_recipe_step(ron.id).await?;
|
||||
check_user_rights_recipe_step(&connection, &context.user, step_id).await?;
|
||||
connection.rm_recipe_step(step_id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -323,21 +318,11 @@ pub async fn rm_step(
|
|||
pub async fn set_step_action(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetStepAction>,
|
||||
Path(step_id): Path<i64>,
|
||||
ExtractRon(action): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &context.user, ron.step_id).await?;
|
||||
connection.set_step_action(ron.step_id, &ron.action).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_steps_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_steps(&connection, &context.user, &ron.ids).await?;
|
||||
connection.set_steps_order(&ron.ids).await?;
|
||||
check_user_rights_recipe_step(&connection, &context.user, step_id).await?;
|
||||
connection.set_step_action(step_id, &action).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -345,21 +330,32 @@ pub async fn set_steps_order(
|
|||
pub async fn add_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(step_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_step(&connection, &context.user, ron.id).await?;
|
||||
let id = connection.add_recipe_ingredient(ron.id).await?;
|
||||
Ok(ron_response_ok(ron_api::Id { id }))
|
||||
check_user_rights_recipe_step(&connection, &context.user, step_id).await?;
|
||||
let id = connection.add_recipe_ingredient(step_id).await?;
|
||||
Ok(ron_response_ok(id))
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredients_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ids): ExtractRon<Vec<i64>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredients(&connection, &context.user, &ids).await?;
|
||||
connection.set_ingredients_order(&ids).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn rm_ingredient(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Id>,
|
||||
Path(ingredient_id): Path<i64>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ron.id).await?;
|
||||
connection.rm_recipe_ingredient(ron.id).await?;
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ingredient_id).await?;
|
||||
connection.rm_recipe_ingredient(ingredient_id).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -367,12 +363,11 @@ pub async fn rm_ingredient(
|
|||
pub async fn set_ingredient_name(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientName>,
|
||||
Path(ingredient_id): Path<i64>,
|
||||
ExtractRon(name): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_name(ron.ingredient_id, &ron.name)
|
||||
.await?;
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ingredient_id).await?;
|
||||
connection.set_ingredient_name(ingredient_id, &name).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
|
|
@ -380,11 +375,12 @@ pub async fn set_ingredient_name(
|
|||
pub async fn set_ingredient_comment(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientComment>,
|
||||
Path(ingredient_id): Path<i64>,
|
||||
ExtractRon(comment): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ron.ingredient_id).await?;
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_comment(ron.ingredient_id, &ron.comment)
|
||||
.set_ingredient_comment(ingredient_id, &comment)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -393,11 +389,12 @@ pub async fn set_ingredient_comment(
|
|||
pub async fn set_ingredient_quantity(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientQuantity>,
|
||||
Path(ingredient_id): Path<i64>,
|
||||
ExtractRon(quantity): ExtractRon<Option<f64>>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ron.ingredient_id).await?;
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_quantity(ron.ingredient_id, ron.quantity)
|
||||
.set_ingredient_quantity(ingredient_id, quantity)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
@ -406,22 +403,10 @@ pub async fn set_ingredient_quantity(
|
|||
pub async fn set_ingredient_unit(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::SetIngredientUnit>,
|
||||
Path(ingredient_id): Path<i64>,
|
||||
ExtractRon(unit): ExtractRon<String>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ron.ingredient_id).await?;
|
||||
connection
|
||||
.set_ingredient_unit(ron.ingredient_id, &ron.unit)
|
||||
.await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
pub async fn set_ingredients_order(
|
||||
State(connection): State<db::Connection>,
|
||||
Extension(context): Extension<Context>,
|
||||
ExtractRon(ron): ExtractRon<ron_api::Ids>,
|
||||
) -> Result<impl IntoResponse> {
|
||||
check_user_rights_recipe_ingredients(&connection, &context.user, &ron.ids).await?;
|
||||
connection.set_ingredients_order(&ron.ids).await?;
|
||||
check_user_rights_recipe_ingredient(&connection, &context.user, ingredient_id).await?;
|
||||
connection.set_ingredient_unit(ingredient_id, &unit).await?;
|
||||
Ok(StatusCode::OK)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -257,107 +257,62 @@ async fn create_recipe_and_edit_it() -> Result<(), Box<dyn Error>> {
|
|||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/title")
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/title"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(ron_api::to_string("AAA").unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/description"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(ron_api::to_string("BBB").unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/servings"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(ron_api::to_string(Some(42)).unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/estimated_time"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(ron_api::to_string(Some(420)).unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/difficulty"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeTitle {
|
||||
recipe_id,
|
||||
title: "AAA".into(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
ron_api::to_string(ron_api::Difficulty::Hard)
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/description")
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/language"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeDescription {
|
||||
recipe_id,
|
||||
description: "BBB".into(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.bytes(ron_api::to_string("fr").unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/servings")
|
||||
.patch(&format!("/ron-api/recipe/{recipe_id}/is_public"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeServings {
|
||||
recipe_id,
|
||||
servings: Some(42),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/estimated_time")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeEstimatedTime {
|
||||
recipe_id,
|
||||
estimated_time: Some(420),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/difficulty")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeDifficulty {
|
||||
recipe_id,
|
||||
difficulty: ron_api::Difficulty::Hard,
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/language")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeLanguage {
|
||||
recipe_id,
|
||||
lang: "fr".into(),
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
let response = server
|
||||
.patch("/ron-api/recipe/is_public")
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::SetRecipeIsPublic {
|
||||
recipe_id,
|
||||
is_public: true,
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.bytes(ron_api::to_string(true).unwrap().into())
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
|
||||
|
|
@ -430,70 +385,61 @@ async fn recipe_tags() -> Result<(), Box<dyn Error>> {
|
|||
|
||||
// Tags list must be empty.
|
||||
let response = server
|
||||
.get("/ron-api/recipe/tags")
|
||||
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_query_param("id", recipe_id)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert!(tags.tags.is_empty());
|
||||
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert!(tags.is_empty());
|
||||
|
||||
// Act.
|
||||
// Add some tags.
|
||||
let response = server
|
||||
.post("/ron-api/recipe/tags")
|
||||
.post(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::Tags {
|
||||
recipe_id,
|
||||
tags: vec!["ABC".into(), "xyz".into()],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
ron_api::to_string(vec!["ABC".to_string(), "xyz".to_string()])
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Assert.
|
||||
response.assert_status_ok();
|
||||
let response = server
|
||||
.get("/ron-api/recipe/tags")
|
||||
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_query_param("id", recipe_id)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert_eq!(tags.tags.len(), 2);
|
||||
assert!(tags.tags.contains(&"abc".to_string())); // Tags are in lower case.
|
||||
assert!(tags.tags.contains(&"xyz".to_string()));
|
||||
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert_eq!(tags.len(), 2);
|
||||
assert!(tags.contains(&"abc".to_string())); // Tags are in lower case.
|
||||
assert!(tags.contains(&"xyz".to_string()));
|
||||
|
||||
// Act.
|
||||
// Remove some tags.
|
||||
let response = server
|
||||
.delete("/ron-api/recipe/tags")
|
||||
.delete(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_header(http::header::CONTENT_TYPE, common::consts::MIME_TYPE_RON)
|
||||
.bytes(
|
||||
ron_api::to_string(ron_api::Tags {
|
||||
recipe_id,
|
||||
tags: vec!["XYZ".into(), "qwe".into()],
|
||||
})
|
||||
.unwrap()
|
||||
.into(),
|
||||
ron_api::to_string(vec!["XYZ".to_string(), "qwe".to_string()])
|
||||
.unwrap()
|
||||
.into(),
|
||||
)
|
||||
.await;
|
||||
|
||||
// Assert.
|
||||
response.assert_status_ok();
|
||||
let response = server
|
||||
.get("/ron-api/recipe/tags")
|
||||
.get(&format!("/ron-api/recipe/{recipe_id}/tags"))
|
||||
.add_cookie(cookie.clone())
|
||||
.add_query_param("id", recipe_id)
|
||||
.await;
|
||||
response.assert_status_ok();
|
||||
let tags: ron_api::Tags = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert_eq!(tags.tags.len(), 1);
|
||||
assert_eq!(tags.tags[0], "abc".to_string());
|
||||
let tags: Vec<String> = ron::de::from_bytes(response.as_bytes()).unwrap();
|
||||
assert_eq!(tags.len(), 1);
|
||||
assert_eq!(tags[0], "abc".to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue