do not use UPSERT
This commit is contained in:
parent
eab0e2bac8
commit
fd4603ba42
1 changed files with 39 additions and 18 deletions
39
src/main.rs
39
src/main.rs
|
@ -87,9 +87,7 @@ async fn main() {
|
||||||
|
|
||||||
let mut bind_with = "127.0.0.1:".to_owned();
|
let mut bind_with = "127.0.0.1:".to_owned();
|
||||||
bind_with += &listen_port;
|
bind_with += &listen_port;
|
||||||
let listener = tokio::net::TcpListener::bind(bind_with)
|
let listener = tokio::net::TcpListener::bind(bind_with).await.unwrap();
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
axum::serve(listener, app).await.unwrap();
|
axum::serve(listener, app).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -190,17 +188,25 @@ async fn update_progress(
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_secs();
|
.as_secs();
|
||||||
|
|
||||||
let row: (i64,) = sqlx::query_as("SELECT id FROM users WHERE username = $1")
|
let user_row: (i64,) = sqlx::query_as("SELECT id FROM users WHERE username = $1")
|
||||||
.bind(&username)
|
.bind(&username)
|
||||||
.fetch_one(&db_pool)
|
.fetch_one(&db_pool)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let user_id = row.0;
|
let user_id = user_row.0;
|
||||||
|
|
||||||
sqlx::query("INSERT INTO progresses (user_id, document, progress, percentage, device, device_id, timestamp) VALUES ($1, $2, $3, $4, $5, $6, $7) \
|
let mut tx = db_pool.begin().await.unwrap();
|
||||||
ON CONFLICT (user_id, document) DO UPDATE \
|
|
||||||
SET user_id = $1, document = $2, progress = $3, percentage = $4, device = $5, device_id = $6, timestamp = $7")
|
let doc_row = sqlx::query("SELECT 1 FROM progresses WHERE user_id = $1 AND document = $2")
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&payload.document)
|
||||||
|
.fetch_optional(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
if doc_row.is_none() {
|
||||||
|
sqlx::query("INSERT INTO progresses (user_id, document, progress, percentage, device, device_id, timestamp) VALUES ($1, $2, $3, $4, $5, $6, $7)")
|
||||||
.bind(user_id)
|
.bind(user_id)
|
||||||
.bind(&payload.document)
|
.bind(&payload.document)
|
||||||
.bind(&payload.progress)
|
.bind(&payload.progress)
|
||||||
|
@ -208,9 +214,24 @@ async fn update_progress(
|
||||||
.bind(&payload.device)
|
.bind(&payload.device)
|
||||||
.bind(&payload.device_id)
|
.bind(&payload.device_id)
|
||||||
.bind(timestamp as i64)
|
.bind(timestamp as i64)
|
||||||
.execute(&db_pool)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
} else {
|
||||||
|
sqlx::query("UPDATE progresses SET progress = $3, percentage = $4, device = $5, device_id = $6, timestamp = $7 WHERE user_id = $1 AND document = $2")
|
||||||
|
.bind(user_id)
|
||||||
|
.bind(&payload.document)
|
||||||
|
.bind(&payload.progress)
|
||||||
|
.bind(&payload.percentage)
|
||||||
|
.bind(&payload.device)
|
||||||
|
.bind(&payload.device_id)
|
||||||
|
.bind(timestamp as i64)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
tx.commit().await.unwrap();
|
||||||
|
|
||||||
StatusCode::OK
|
StatusCode::OK
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue