leaderboard work

This commit is contained in:
Mitchell Marino 2023-04-16 17:47:16 -05:00
parent 334ef065af
commit db6a5414d9
4 changed files with 29 additions and 24 deletions

View File

@ -148,6 +148,7 @@ jobs:
- name: build - name: build
plan: plan:
- get: docker-image
- get: repo - get: repo
trigger: true trigger: true
passed: [test] passed: [test]
@ -190,6 +191,7 @@ jobs:
repository: concourse/oci-build-task repository: concourse/oci-build-task
inputs: inputs:
- name: docker-image
- name: repo - name: repo
path: . path: .

View File

@ -51,8 +51,9 @@ pub async fn list_points(
AuthBearer(token): AuthBearer, AuthBearer(token): AuthBearer,
State(app_state): State<AppState>, State(app_state): State<AppState>,
) -> impl IntoResponse { ) -> impl IntoResponse {
if let Err(err) = handle_token(token, &app_state, Role::Student) { let token_data = match handle_token(token, &app_state, Role::Student) {
return err; Err(err) => return err,
Ok(token_data) => token_data,
}; };
let result = query_as!( let result = query_as!(
@ -67,10 +68,13 @@ pub async fn list_points(
ON u.id = ea.user_id AND ea.confirmed = true ON u.id = ea.user_id AND ea.confirmed = true
LEFT JOIN events e LEFT JOIN events e
ON ea.event_id = e.id ON ea.event_id = e.id
WHERE
u.grade = $1
GROUP BY u.id GROUP BY u.id
ORDER BY points DESC ORDER BY points DESC
; ;
"#, "#,
token_data.grade,
) )
.fetch_all(&app_state.db_pool) .fetch_all(&app_state.db_pool)
.await; .await;

View File

@ -154,6 +154,7 @@ pub struct Claims {
pub exp: OffsetDateTime, pub exp: OffsetDateTime,
pub id: i32, pub id: i32,
pub username: String, pub username: String,
pub grade: i32,
pub role: Role, pub role: Role,
} }

View File

@ -64,12 +64,13 @@ pub async fn signin(
let result = sqlx::query!( let result = sqlx::query!(
r#" r#"
SELECT SELECT
id, id,
username, username,
role AS "role!: Role" role AS "role!: Role",
FROM users grade
WHERE username = $1 AND password = $2 FROM users
WHERE username = $1 AND password = $2
"#, "#,
signin.username, signin.username,
pass_hash.as_bytes(), pass_hash.as_bytes(),
@ -82,6 +83,7 @@ pub async fn signin(
let claims = Claims { let claims = Claims {
exp: OffsetDateTime::now_utc() + JWT_LIFETIME, exp: OffsetDateTime::now_utc() + JWT_LIFETIME,
id: user.id, id: user.id,
grade: user.grade,
username: user.username, username: user.username,
role: user.role, role: user.role,
}; };
@ -101,21 +103,17 @@ pub async fn signin(
(StatusCode::OK, Json(json!({ "data": token }))) (StatusCode::OK, Json(json!({ "data": token })))
} }
Ok(None) => { Ok(None) => (
( StatusCode::UNAUTHORIZED,
StatusCode::UNAUTHORIZED, Json(json!({
Json(json!({ "error": format!("Incorrect username or password")
"error": format!("Incorrect username or password") })),
})), ),
) Err(err) => (
} StatusCode::INTERNAL_SERVER_ERROR,
Err(err) => { Json(json!({
( "error": format!("Unknown error signing in: {:?}", err)
StatusCode::INTERNAL_SERVER_ERROR, })),
Json(json!({ ),
"error": format!("Unknown error signing in: {:?}", err)
})),
)
}
} }
} }