server: Add a route to list recipes

This commit is contained in:
traxys 2023-06-22 23:04:46 +02:00
parent 69197a3852
commit 80e3d7ee86
3 changed files with 25 additions and 2 deletions

View file

@ -96,3 +96,8 @@ pub struct CreateRecipeRequest {
pub struct CreateRecipeResponse {
pub id: i64,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ListRecipesResponse {
pub recipes: Vec<(i64, String)>,
}

View file

@ -218,6 +218,8 @@ pub(crate) fn router(api_allowed: Option<HeaderValue>) -> Router<AppState> {
)
.route(
"/household/:house_id/recipe",
post(recipe::create_recipe).layer(mk_service(vec![Method::POST])),
post(recipe::create_recipe)
.get(recipe::list_recipes)
.layer(mk_service(vec![Method::POST, Method::GET])),
)
}

View file

@ -1,4 +1,4 @@
use api::{CreateRecipeRequest, CreateRecipeResponse};
use api::{CreateRecipeRequest, CreateRecipeResponse, ListRecipesResponse};
use axum::{extract::State, Json};
use sea_orm::{prelude::*, ActiveValue, TransactionTrait};
@ -61,3 +61,19 @@ pub(super) async fn create_recipe(
Ok(CreateRecipeResponse { id }.into())
}
pub(super) async fn list_recipes(
AuthorizedHousehold(household): AuthorizedHousehold,
State(state): State<AppState>,
) -> JsonResult<ListRecipesResponse> {
Ok(ListRecipesResponse {
recipes: household
.find_related(Recipe)
.all(&state.db)
.await?
.into_iter()
.map(|r| (r.id, r.name))
.collect(),
}
.into())
}