X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=backend%2Fsrc%2Fservices.rs;h=b4d39f9e6ca7f303606c855435f7037148e1b3e9;hb=cbe276fc0601041b13087a6ffd80c5b126dfbe59;hp=df81c437b0224d098461a761704df75c54f57d1a;hpb=d28e765e39e70ad2ab9a42885c786d5d8ba9ba40;p=recipes.git diff --git a/backend/src/services.rs b/backend/src/services.rs index df81c43..b4d39f9 100644 --- a/backend/src/services.rs +++ b/backend/src/services.rs @@ -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 for ServiceError { } } +impl From 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, - recipes: Vec<(i32, String)>, - current_recipe_id: Option, + recipes: Vec<(i64, String)>, + current_recipe_id: Option, } #[get("/")] @@ -137,13 +148,13 @@ pub async fn home_page(req: HttpRequest, connection: web::Data) #[template(path = "view_recipe.html")] struct ViewRecipeTemplate { user: Option, - recipes: Vec<(i32, String)>, - current_recipe_id: Option, + recipes: Vec<(i64, String)>, + current_recipe_id: Option, current_recipe: model::Recipe, } #[get("/recipe/view/{id}")] -pub async fn view_recipe(req: HttpRequest, path: web::Path<(i32,)>, connection: web::Data) -> Result { +pub async fn view_recipe(req: HttpRequest, path: web::Path<(i64,)>, connection: web::Data) -> Result { 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, + recipes: Vec<(i64, String)>, + current_recipe_id: Option, + current_recipe: model::Recipe, +} + +#[get("/recipe/edit/{id}")] +pub async fn edit_recipe(req: HttpRequest, path: web::Path<(i64,)>, connection: web::Data) -> Result { + 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, 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 = 'p: { + let port: Option = 'p: { let split_port: Vec<&str> = host.split(':').collect(); if split_port.len() == 2 { - if let Ok(p) = split_port[1].parse::() { + if let Ok(p) = split_port[1].parse::() { break 'p Some(p) } }