Should be fully functional now.

This commit is contained in:
Martin Brodbeck 2025-01-07 19:18:12 +01:00
parent 0668e7fdba
commit 3e389bf142

View file

@ -36,7 +36,7 @@ pub struct GetProgress {
percentage: f32,
device: String,
device_id: String,
timestamp: u64,
timestamp: i64,
}
#[tokio::main]
@ -168,93 +168,75 @@ async fn update_progress(
.unwrap()
.as_secs();
let document = payload.document.clone();
let row: (i64,) = sqlx::query_as("SELECT id FROM users WHERE username = $1")
.bind(&username)
.fetch_one(&db_pool)
.await
.unwrap();
println!("{document}");
let user_id = row.0;
// TODO: Get user_id
sqlx::query("INSERT INTO progresses (user_id, document, progress, percentage, device, device_id, timestamp) SELECT id, $1, $2, $3, $4, $5, $6 FROM users WHERE username = $7 ON CONFLICT (document) DO UPDATE SET ")
sqlx::query("INSERT INTO progresses (user_id, document, progress, percentage, device, device_id, timestamp) VALUES ($1, $2, $3, $4, $5, $6, $7) \
ON CONFLICT (user_id, document) DO UPDATE \
SET user_id = $1, document = $2, progress = $3, percentage = $4, device = $5, device_id = $6, timestamp = $7")
.bind(user_id)
.bind(&payload.document)
.bind(&payload.progress)
.bind(&payload.percentage)
.bind(&payload.device)
.bind(&payload.device_id)
.bind(timestamp as i64)
.bind(&username)
.execute(&db_pool)
.await
.unwrap();
/*if authorize(username, password) == false {
return StatusCode::UNAUTHORIZED;
}
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs();
let document = payload.document;
let doc_key = format!("user:{username}:document:{document}");
let _: () = con
.hset_multiple(
&doc_key,
&[
("percentage", &payload.percentage.to_string()),
("progress", &payload.progress),
("device", &payload.device),
("device_id", &payload.device_id),
("timestamp", &timestamp.to_string()),
],
)
.unwrap(); */
StatusCode::OK
}
async fn get_progress(
State(db_pool): State<PgPool>,
headers: HeaderMap,
Path(document): Path<String>,
) -> (StatusCode, Json<Value>) {
let username = headers["x-auth-user"].to_str().unwrap_or("");
let password = headers["x-auth-key"].to_str().unwrap_or("");
println!("Häh!");
let mut tx = db_pool.begin().await.unwrap();
/* if authorize(username, password) == false {
if authorize(&mut *tx, username, password).await == false {
return (StatusCode::UNAUTHORIZED, Json(json!("")));
}
let client = redis::Client::open("redis://127.0.0.1/").unwrap();
let mut con = client.get_connection().unwrap();
tx.commit().await.unwrap();
let doc_key = format!("user:{username}:document:{document}");
let row: (i64,) = sqlx::query_as("SELECT id FROM users WHERE username = $1")
.bind(&username)
.fetch_one(&db_pool)
.await
.unwrap();
let values: Vec<String> = con
.hget(
doc_key,
&["percentage", "progress", "device", "device_id", "timestamp"],
)
.unwrap_or_default();
let user_id = row.0;
if values.is_empty() {
return (StatusCode::OK, Json(json!({})));
let row: Option<(f32, String, String, String, i64)> = sqlx::query_as("SELECT percentage, progress, device, device_id, timestamp FROM progresses WHERE user_id = $1 AND document = $2")
.bind(user_id)
.bind(&document)
.fetch_optional(&db_pool)
.await
.unwrap();
if let Some(val) = row {
let progress = GetProgress {
percentage: val.0,
progress: val.1,
device: val.2,
device_id: val.3,
timestamp: val.4,
document,
};
return (StatusCode::OK, Json(json!(progress)));
}
let res = GetProgress {
percentage: values[0].parse().unwrap(),
progress: values[1].clone(),
device: values[2].clone(),
device_id: values[3].clone(),
timestamp: values[4].parse().unwrap(),
document,
}; */
//(StatusCode::OK, Json(json!(res)))
(StatusCode::OK, Json(json!("")))
}