diff --git a/db/1.sqlite-shm b/db/1.sqlite-shm index 5474019..dc07540 100644 Binary files a/db/1.sqlite-shm and b/db/1.sqlite-shm differ diff --git a/db/auth_copy_2.sqlite-shm b/db/auth_copy_2.sqlite-shm new file mode 100644 index 0000000..e184ba9 Binary files /dev/null and b/db/auth_copy_2.sqlite-shm differ diff --git a/db/auth_copy_2.sqlite-wal b/db/auth_copy_2.sqlite-wal new file mode 100644 index 0000000..ba59f84 Binary files /dev/null and b/db/auth_copy_2.sqlite-wal differ diff --git a/src/utils/auth.rs b/src/utils/auth.rs index fb934e9..f7c637c 100644 --- a/src/utils/auth.rs +++ b/src/utils/auth.rs @@ -136,7 +136,8 @@ fn verify_password(password: &str, stored_hash: &str) -> bool { pub struct RegisterValues{ username: String, password: String, - hotel_id: i32, + #[serde(default)] + hotel_ids: Vec, //-> :Vec!<32>, maybe optionnal ? displayname: String, } @@ -167,10 +168,32 @@ pub async fn register_user ( .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB connection error"))?; conn.execute( - "INSERT INTO users (username, password, hotel_id, displayname) VALUES (?1, ?2, ?3, ?4)", - params![payload.username, hashed_password, payload.hotel_id, payload.displayname], + "INSERT INTO users (username, password, displayname) VALUES (?1, ?2, ?3)", + params![payload.username, hashed_password, payload.displayname], ) .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?; + + let user_id = conn.last_insert_rowid(); + for hotel_id in payload.hotel_ids { + + // more logic for security here + //FIXME: needs to be the display name in the DB, scheme is currently wrong + + let hotel_name = conn.execute( + "SELECT hotel_name + FROM hotels + WHERE id = ?1 ", + params![hotel_id], + ).map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?; + + conn.execute( + "INSERT INTO hotel_user_link (user_id, hotel_id, username, hotel_name) VALUES (?1, ?2, ?3, ?4)", + params![user_id, hotel_id, payload.username, hotel_name], + ) + .map_err(|_| (StatusCode::INTERNAL_SERVER_ERROR, "DB insert error"))?; + + } + Ok((StatusCode::CREATED, "User registered successfully")) }