From 6a0bbf205987d0ebe780f0a5cdaf3cf5ef43562e Mon Sep 17 00:00:00 2001 From: Hannes Kuchelmeister Date: Fri, 3 Sep 2021 12:53:39 +0200 Subject: [PATCH] updating link from edit page works now --- src/main.rs | 53 +++++++++++++++++++++++++++++----------------------- src/model.rs | 11 +++++++++++ 2 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index e5d9407..f0bfff0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,19 +115,26 @@ fn redirect_to_index_page( async fn edit_process( tmpl: web::Data, query: web::Query>, + db_pool: web::Data>, ) -> Result { // TODO: implement handling if let Some(uuid_str) = query.get("uuid") { - if let Some(link) = query.get("link") { + if let Some(destination) = query.get("link") { match Uuid::parse_str(uuid_str) { Ok(uuid) => { - // TODO: actually save entry in database - redirect_to_edit_page( - tmpl, - "Edit successful!".to_string(), - uuid, - REDIRECT_TIMEOUT_S, - ) + let link = Link { + uuid: uuid.to_string(), + destination: destination.to_string(), + }; + match Link::update(link, db_pool).await { + Ok(_) => redirect_to_edit_page( + tmpl, + "Edit successful!".to_string(), + uuid, + REDIRECT_TIMEOUT_S, + ), + Err(err) => error_page(tmpl, format!("db error: {}", err.to_string())), + } } Err(err) => error_page(tmpl, format!("uuid parsing error: {}", err.to_string())), } @@ -150,23 +157,23 @@ async fn index_process( match query.get("link") { // TODO: actually parse link to url to make sure its valid Some(destination) => { - let insert_link = Link { - uuid: uuid.to_string(), + let insert_link = Link { + uuid: uuid.to_string(), destination: destination.to_string(), - }; + }; - match Link::create(insert_link, db_pool).await { - Ok(link) => match Uuid::parse_str(&link.uuid) { - Ok(uuid) => redirect_to_edit_page( - tmpl, - "Create was successful".to_string(), - uuid, - REDIRECT_TIMEOUT_S, - ), - Err(e) => error_page(tmpl, format!("uuid parsing error {}", e.to_string())), - }, - // TODO: actually redirect to index page to try again - Err(e) => error_page(tmpl, format!("db error: {}", e.to_string())), + match Link::create(insert_link, db_pool).await { + Ok(link) => match Uuid::parse_str(&link.uuid) { + Ok(uuid) => redirect_to_edit_page( + tmpl, + "Create was successful".to_string(), + uuid, + REDIRECT_TIMEOUT_S, + ), + Err(e) => error_page(tmpl, format!("uuid parsing error {}", e.to_string())), + }, + // TODO: actually redirect to index page to try again + Err(e) => error_page(tmpl, format!("db error: {}", e.to_string())), } } None => { diff --git a/src/model.rs b/src/model.rs index 2827146..0afce1a 100644 --- a/src/model.rs +++ b/src/model.rs @@ -30,6 +30,17 @@ impl Link { destination: rec.DESTINATION, }) } + pub async fn update(link: Link, pool: web::Data>) -> Result { + let mut tx = pool.begin().await?; + sqlx::query("UPDATE links SET destination = $2 WHERE uuid = $1;") + .bind(&link.uuid) + .bind(&link.destination) + .execute(&mut tx) + .await?; + + tx.commit().await?; + Ok(link) + } pub async fn create(link: Link, pool: web::Data>) -> Result { let mut tx = pool.begin().await?; sqlx::query("INSERT INTO links (uuid, destination) VALUES ($1, $2);")