Beginning of frontend + recipe editing
[recipes.git] / backend / src / services.rs
index 2d69cc5..b4d39f9 100644 (file)
@@ -94,6 +94,15 @@ impl From<actix_web::error::BlockingError> for ServiceError  {
     }
 }
 
+impl From<ron::error::SpannedError> for ServiceError  {
+    fn from(error: ron::error::SpannedError) -> Self {
+        ServiceError {
+            status_code: StatusCode::INTERNAL_SERVER_ERROR,
+            message: Some(format!("{:?}", error)),
+        }
+    }
+}
+
 impl std::fmt::Display for ServiceError {
     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
         if let Some(ref m) = self.message {
@@ -159,6 +168,32 @@ pub async fn view_recipe(req: HttpRequest, path: web::Path<(i64,)>, connection:
     }.to_response())
 }
 
+///// EDIT RECIPE /////
+
+#[derive(Template)]
+#[template(path = "edit_recipe.html")]
+struct EditRecipeTemplate {
+    user: Option<User>,
+    recipes: Vec<(i64, String)>,
+    current_recipe_id: Option<i64>,
+    current_recipe: model::Recipe,
+}
+
+#[get("/recipe/edit/{id}")]
+pub async fn edit_recipe(req: HttpRequest, path: web::Path<(i64,)>, connection: web::Data<db::Connection>) -> Result<HttpResponse> {
+    let (id,)= path.into_inner();
+    let user = get_current_user(&req, connection.clone()).await;
+    let recipes = connection.get_all_recipe_titles_async().await?;
+    let recipe = connection.get_recipe_async(id).await?;
+
+    Ok(EditRecipeTemplate {
+        user,
+        current_recipe_id: Some(recipe.id),
+        recipes,
+        current_recipe: recipe,
+    }.to_response())
+}
+
 ///// MESSAGE /////
 
 #[derive(Template)]