Beginning of frontend + recipe editing
[recipes.git] / backend / src / services.rs
index df81c43..b4d39f9 100644 (file)
@@ -14,6 +14,8 @@ use crate::user::User;
 use crate::model;
 use crate::data::{db, asynchronous};
 
+mod api;
+
 ///// UTILS /////
 
 fn get_ip_and_user_agent(req: &HttpRequest) -> (String, String) {
@@ -92,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 {
@@ -119,8 +130,8 @@ impl actix_web::error::ResponseError for ServiceError {
 #[template(path = "home.html")]
 struct HomeTemplate {
     user: Option<User>,
-    recipes: Vec<(i32, String)>,
-    current_recipe_id: Option<i32>,
+    recipes: Vec<(i64, String)>,
+    current_recipe_id: Option<i64>,
 }
 
 #[get("/")]
@@ -137,13 +148,13 @@ pub async fn home_page(req: HttpRequest, connection: web::Data<db::Connection>)
 #[template(path = "view_recipe.html")]
 struct ViewRecipeTemplate {
     user: Option<User>,
-    recipes: Vec<(i32, String)>,
-    current_recipe_id: Option<i32>,
+    recipes: Vec<(i64, String)>,
+    current_recipe_id: Option<i64>,
     current_recipe: model::Recipe,
 }
 
 #[get("/recipe/view/{id}")]
-pub async fn view_recipe(req: HttpRequest, path: web::Path<(i32,)>, connection: web::Data<db::Connection>) -> Result<HttpResponse> {
+pub async fn view_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?;
@@ -157,6 +168,32 @@ pub async fn view_recipe(req: HttpRequest, path: web::Path<(i32,)>, 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)]
@@ -255,10 +292,10 @@ pub async fn sign_up_post(req: HttpRequest, form: web::Form<SignUpFormData>, con
         Ok(db::SignUpResult::UserCreatedWaitingForValidation(token)) => {
             let url = {
                 let host = req.headers().get(header::HOST).map(|v| v.to_str().unwrap_or_default()).unwrap_or_default();
-                let port: Option<i32> = 'p: {
+                let port: Option<u16> = 'p: {
                     let split_port: Vec<&str> = host.split(':').collect();
                     if split_port.len() == 2 {
-                        if let Ok(p) = split_port[1].parse::<i32>() {
+                        if let Ok(p) = split_port[1].parse::<u16>() {
                             break 'p Some(p)
                         }
                     }