use actix_identity::Identity; use actix_web::{HttpResponse, Responder, error::Result, get, web}; use crate::{ app::App, storage::sql::recipe_parent::{RecipeParent, RecipeParentId, RecipeParentIdStub}, }; /// Return all registered recipe parents #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), )] #[get("/recipe_parents/")] pub(crate) async fn recipe_parents(app: web::Data, _user: Identity) -> Result { let all: Vec = RecipeParent::get_all(&app).await?; Ok(HttpResponse::Ok().json(all)) } /// Return all registered recipe parents, that have no parents themselves #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), )] #[get("/recipe_parents_toplevel/")] pub(crate) async fn recipe_parents_toplevel( app: web::Data, _user: Identity, ) -> Result { let all: Vec = RecipeParent::get_all(&app) .await? .into_iter() .filter(|parent| parent.parent.is_none()) .collect(); Ok(HttpResponse::Ok().json(all)) } /// Return all parents, that have this parent as parent #[utoipa::path( responses( ( status = OK, description = "All parents found", body = Vec ), ( status = UNAUTHORIZED, description = "You did not login before calling this endpoint", ), ( status = INTERNAL_SERVER_ERROR, description = "Server encountered error", body = String ) ), params( ( "id" = RecipeParentId, description = "Recipe parent id" ), ), )] #[get("/recipe_parents_under/{id}")] pub(crate) async fn recipe_parents_under( app: web::Data, id: web::Path, _user: Identity, ) -> Result { let id = id.into_inner().into(); let all: Vec<_> = RecipeParent::get_all(&app) .await? .into_iter() .filter(|parent| parent.parent.is_some_and(|found| found == id)) .collect(); Ok(HttpResponse::Ok().json(all)) }