Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add scan when registering, return name #83

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading