Added code to update last_login
All checks were successful
Rust Build / Check (pull_request) Successful in 1m0s
Rust Build / Test Suite (pull_request) Successful in 1m18s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Successful in 55s
Rust Build / build (pull_request) Successful in 1m24s

This commit is contained in:
2025-04-10 19:13:09 -04:00
parent 1aeb470fce
commit ce28ff862b
2 changed files with 21 additions and 5 deletions

View File

@@ -51,13 +51,16 @@ pub mod endpoint {
let (token_literal, duration) = token_stuff::create_token(&key).unwrap();
if token_stuff::verify_token(&key, &token_literal) {
let current_time = time::OffsetDateTime::now_utc();
let _ = repo::user::update_last_login(&pool, &user, &current_time).await;
(
StatusCode::OK,
Json(response::Response {
message: String::from("Successful"),
data: vec![icarus_models::login_result::LoginResult {
id: user.id,
username: user.username,
username: user.username.clone(),
token: token_literal,
token_type: String::from(token_stuff::TOKENTYPE),
expiration: duration,

View File

@@ -45,19 +45,32 @@ pub mod user {
pub async fn update_last_login(
pool: &sqlx::PgPool,
user: &icarus_models::user::User,
time: &time::OffsetDateTime,
) -> Result<time::OffsetDateTime, sqlx::Error> {
let result = sqlx::query(
r#"
UPDATE "user" SET last_login = $1 WHERE id = $2
UPDATE "user" SET last_login = $1 WHERE id = $2 RETURNING last_login
"#,
)
.bind(user.last_login)
.bind(time)
.bind(user.id)
.fetch_optional(pool)
.await;
.await
.map_err(|e| {
eprintln!("Error updating time: {}", e);
e
});
match result {
Ok(row) => Ok(user.last_login.unwrap()),
Ok(row) => match row {
Some(r) => {
let last_login: time::OffsetDateTime = r
.try_get("last_login")
.map_err(|_e| sqlx::Error::RowNotFound)?;
Ok(last_login)
}
None => Err(sqlx::Error::RowNotFound),
},
Err(err) => Err(err),
}
}