updating link from edit page works now

This commit is contained in:
2021-09-03 12:53:39 +02:00
parent 5820549f01
commit 6a0bbf2059
2 changed files with 41 additions and 23 deletions

View File

@@ -115,19 +115,26 @@ fn redirect_to_index_page(
async fn edit_process( async fn edit_process(
tmpl: web::Data<tera::Tera>, tmpl: web::Data<tera::Tera>,
query: web::Query<HashMap<String, String>>, query: web::Query<HashMap<String, String>>,
db_pool: web::Data<Pool<Sqlite>>,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
// TODO: implement handling // TODO: implement handling
if let Some(uuid_str) = query.get("uuid") { 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) { match Uuid::parse_str(uuid_str) {
Ok(uuid) => { Ok(uuid) => {
// TODO: actually save entry in database let link = Link {
redirect_to_edit_page( uuid: uuid.to_string(),
tmpl, destination: destination.to_string(),
"Edit successful!".to_string(), };
uuid, match Link::update(link, db_pool).await {
REDIRECT_TIMEOUT_S, 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())), Err(err) => error_page(tmpl, format!("uuid parsing error: {}", err.to_string())),
} }
@@ -150,23 +157,23 @@ async fn index_process(
match query.get("link") { match query.get("link") {
// TODO: actually parse link to url to make sure its valid // TODO: actually parse link to url to make sure its valid
Some(destination) => { Some(destination) => {
let insert_link = Link { let insert_link = Link {
uuid: uuid.to_string(), uuid: uuid.to_string(),
destination: destination.to_string(), destination: destination.to_string(),
}; };
match Link::create(insert_link, db_pool).await { match Link::create(insert_link, db_pool).await {
Ok(link) => match Uuid::parse_str(&link.uuid) { Ok(link) => match Uuid::parse_str(&link.uuid) {
Ok(uuid) => redirect_to_edit_page( Ok(uuid) => redirect_to_edit_page(
tmpl, tmpl,
"Create was successful".to_string(), "Create was successful".to_string(),
uuid, uuid,
REDIRECT_TIMEOUT_S, REDIRECT_TIMEOUT_S,
), ),
Err(e) => error_page(tmpl, format!("uuid parsing error {}", e.to_string())), Err(e) => error_page(tmpl, format!("uuid parsing error {}", e.to_string())),
}, },
// TODO: actually redirect to index page to try again // TODO: actually redirect to index page to try again
Err(e) => error_page(tmpl, format!("db error: {}", e.to_string())), Err(e) => error_page(tmpl, format!("db error: {}", e.to_string())),
} }
} }
None => { None => {

View File

@@ -30,6 +30,17 @@ impl Link {
destination: rec.DESTINATION, destination: rec.DESTINATION,
}) })
} }
pub async fn update(link: Link, pool: web::Data<Pool<Sqlite>>) -> Result<Link, sqlx::Error> {
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<Pool<Sqlite>>) -> Result<Link, sqlx::Error> { pub async fn create(link: Link, pool: web::Data<Pool<Sqlite>>) -> Result<Link, sqlx::Error> {
let mut tx = pool.begin().await?; let mut tx = pool.begin().await?;
sqlx::query("INSERT INTO links (uuid, destination) VALUES ($1, $2);") sqlx::query("INSERT INTO links (uuid, destination) VALUES ($1, $2);")