Issues vscode rreading errors
This commit is contained in:
@@ -2,23 +2,31 @@ use axum::{
|
||||
extract::{Path, State},
|
||||
response::Json,
|
||||
http::StatusCode,
|
||||
Router,
|
||||
routing::get,
|
||||
};
|
||||
use serde::Serialize;
|
||||
use sqlx::PgPool;
|
||||
use crate::auth::structs::AppState; // Import AppState
|
||||
|
||||
use crate::auth::structs::AppState;
|
||||
use crate::state::structs::{County, ErrorResponse};
|
||||
|
||||
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new()
|
||||
.route("/state/:state_abbr", get(get_counties_by_state))
|
||||
.route("/state/:state_abbr/:county_id", get(get_county_by_id))
|
||||
}
|
||||
|
||||
|
||||
|
||||
pub async fn get_counties_by_state(
|
||||
State(app_state): State<AppState>, // Changed from State<PgPool> to State<AppState>
|
||||
State(app_state): State<AppState>,
|
||||
Path(state_abbr): Path<String>,
|
||||
) -> Result<Json<Vec<County>>, (StatusCode, Json<ErrorResponse>)> {
|
||||
let state_abbr_upper = state_abbr.to_uppercase(); // Ensure consistent casing for DB query
|
||||
let state_abbr_upper = state_abbr.to_uppercase();
|
||||
println!("Querying counties for state: {}", state_abbr_upper);
|
||||
|
||||
match sqlx::query_as::<_, County>("SELECT id, name, state FROM county WHERE UPPER(state) = $1 ORDER BY name ASC")
|
||||
.bind(&state_abbr_upper)
|
||||
.fetch_all(&app_state.db) // Use app_state.db to access the pool
|
||||
.fetch_all(&*app_state.db)
|
||||
.await
|
||||
{
|
||||
Ok(counties) => {
|
||||
@@ -55,7 +63,7 @@ pub async fn get_county_by_id(
|
||||
match sqlx::query_as::<_, County>("SELECT id, name, state FROM county WHERE UPPER(state) = $1 AND id = $2")
|
||||
.bind(&state_abbr_upper)
|
||||
.bind(county_id)
|
||||
.fetch_one(&app_state.db)
|
||||
.fetch_one(&*app_state.db)
|
||||
.await
|
||||
{
|
||||
Ok(county) => Ok(Json(county)),
|
||||
@@ -76,3 +84,4 @@ pub async fn get_county_by_id(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user