Add card support

This commit is contained in:
2025-04-18 10:13:41 +02:00
parent fdacb4493a
commit c508f80e6f
2 changed files with 52 additions and 1 deletions

View File

@@ -21,7 +21,8 @@ impl Database {
r#" r#"
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
name TEXT NOT NULL name TEXT NOT NULL,
card TEXT
); );
CREATE TABLE IF NOT EXISTS interprets ( CREATE TABLE IF NOT EXISTS interprets (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY,
@@ -121,6 +122,24 @@ impl Database {
Ok(id) Ok(id)
} }
pub async fn user_get_id(&self, user_card: &str) -> Result<i64> {
let mut conn = self.pool.acquire().await?;
let record = sqlx::query(
r#"
SELECT id FROM users
WHERE card = ?
"#,
)
.bind(user_card)
.fetch_one(&mut *conn)
.await?;
let id = record.try_get("id")?;
Ok(id)
}
pub async fn user_add_rating( pub async fn user_add_rating(
&self, &self,
user_id: i64, user_id: i64,

View File

@@ -34,6 +34,7 @@ pub async fn http_serve(database: &Database, mpris_producer: Sender<(String, Str
.route("/", get(root)) .route("/", get(root))
.route("/rating/{rating}", get(cache_rating_only)) .route("/rating/{rating}", get(cache_rating_only))
.route("/userid/{user_id}", get(add_userid)) .route("/userid/{user_id}", get(add_userid))
.route("/usercard/{user_card}", get(add_userid_by_card))
.route("/{user_id}/{rating}", get(add_rating)) .route("/{user_id}/{rating}", get(add_rating))
.with_state(shared_state); .with_state(shared_state);
@@ -92,3 +93,34 @@ async fn add_userid(Path(user_id): Path<i64>, State(shared): State<SharedState>)
} }
} }
} }
async fn add_userid_by_card(
Path(user_card): Path<String>,
State(shared): State<SharedState>,
) -> Response {
let mut mpris_rx = shared.mpris_sender.subscribe();
// Get the current interpret and track from the watch channel
let (interpret, track) = (*mpris_rx.borrow_and_update()).clone();
let rating = shared.rating.load(Ordering::Relaxed);
let user_id = match shared.database.user_get_id(&user_card).await {
Ok(id) => id,
Err(e) => {
return (StatusCode::BAD_REQUEST, e.to_string()).into_response();
}
};
match shared
.database
.user_add_rating(user_id, &interpret, &track, rating)
.await
{
Ok(_) => (StatusCode::OK, "Done.").into_response(),
Err(e) => {
eprintln!("HTTP error: {e}");
(StatusCode::BAD_REQUEST, e.to_string()).into_response()
}
}
}