Skip to content

Commit

Permalink
vingo: add scan when registering, return name
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes-dev committed Oct 3, 2024
1 parent 2ec8b49 commit 2655ce8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 14 deletions.
45 changes: 31 additions & 14 deletions vingo/src/routes/scans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ pub async fn add(state: State<AppState>, body: String) -> ResponseResult<String>
Err((StatusCode::UNAUTHORIZED, "invalid key"))?
}

if !serial.chars().all(|ch| char::is_ascii_hexdigit(&ch)) {
return Err((StatusCode::BAD_REQUEST, "not valid hex"));
}

let mut registering = state.registering.lock().await;

// if someone is registering a card
Expand All @@ -66,19 +70,32 @@ pub async fn add(state: State<AppState>, body: String) -> ResponseResult<String>
registering.last_success = db_result.is_ok();

db_result.or_log((StatusCode::INTERNAL_SERVER_ERROR, "failed to insert card"))?;
Ok("card registered".to_string())
} else {
if !serial.chars().all(|ch| char::is_ascii_hexdigit(&ch)) {
return Err((StatusCode::BAD_REQUEST, "not valid hex"));
}
scan::ActiveModel {
card_serial: Set(serial.to_string()),
scan_time: Set(Local::now().fixed_offset()),
..Default::default()
}
.insert(&state.db)
.await
.or_log((StatusCode::INTERNAL_SERVER_ERROR, "failed to insert scan"))?;
Ok("scanned".to_string())
}

scan::ActiveModel {
card_serial: Set(serial.to_string()),
scan_time: Set(Local::now().fixed_offset()),
..Default::default()
}
.insert(&state.db)
.await
.or_log((
StatusCode::INTERNAL_SERVER_ERROR,
"no card with that serial",
))?;

let user = Card::find()
.filter(card::Column::Serial.eq(serial))
.find_also_related(User)
.one(&state.db)
.await
.or_log((
StatusCode::INTERNAL_SERVER_ERROR,
"failed to get user for card",
))?
.ok_or((StatusCode::INTERNAL_SERVER_ERROR, "no card"))?
.1
.ok_or((StatusCode::INTERNAL_SERVER_ERROR, "no user for card"))?;

Ok(user.name)
}
1 change: 1 addition & 0 deletions vingo/src/routes/util/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{

static DEBUG_LOGIN: LazyLock<bool> =
LazyLock::new(|| env::var("DEBUG_LOGIN").unwrap_or("".into()) == "TRUE");

pub enum SessionKeys {
User,
Season,
Expand Down

0 comments on commit 2655ce8

Please sign in to comment.