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(),
destination: destination.to_string(),
};
match Link::update(link, db_pool).await {
Ok(_) => redirect_to_edit_page(
tmpl, tmpl,
"Edit successful!".to_string(), "Edit successful!".to_string(),
uuid, uuid,
REDIRECT_TIMEOUT_S, 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())),
} }

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);")