61 lines
1.5 KiB
Rust
61 lines
1.5 KiB
Rust
|
|
use crate::AppState;
|
||
|
|
use axum::{
|
||
|
|
extract::{Path, Query, State},
|
||
|
|
http::StatusCode,
|
||
|
|
response::IntoResponse,
|
||
|
|
routing::get,
|
||
|
|
Json, Router,
|
||
|
|
};
|
||
|
|
use contracts::auth_middleware::AuthUser;
|
||
|
|
use serde::{Deserialize, Serialize};
|
||
|
|
use uuid::Uuid;
|
||
|
|
|
||
|
|
pub fn router() -> Router<AppState> {
|
||
|
|
Router::new()
|
||
|
|
.route("/", get(list_companies))
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Deserialize)]
|
||
|
|
pub struct ListQuery {
|
||
|
|
pub q: Option<String>,
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(Serialize)]
|
||
|
|
pub struct AdminCompanyRow {
|
||
|
|
pub id: Uuid,
|
||
|
|
pub user_id: Uuid,
|
||
|
|
pub company_name: String,
|
||
|
|
pub registration_number: Option<String>,
|
||
|
|
pub industry: Option<String>,
|
||
|
|
pub status: String,
|
||
|
|
pub created_at: chrono::DateTime<chrono::Utc>,
|
||
|
|
pub updated_at: chrono::DateTime<chrono::Utc>,
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn list_companies(
|
||
|
|
_auth: AuthUser,
|
||
|
|
State(state): State<AppState>,
|
||
|
|
Query(q): Query<ListQuery>,
|
||
|
|
) -> Result<impl IntoResponse, (StatusCode, String)> {
|
||
|
|
let search = q.q.as_deref().unwrap_or_default().to_lowercase();
|
||
|
|
|
||
|
|
let companies = sqlx::query_as!(
|
||
|
|
AdminCompanyRow,
|
||
|
|
r#"
|
||
|
|
SELECT
|
||
|
|
id, user_id, company_name, registration_number, industry,
|
||
|
|
status, created_at, updated_at
|
||
|
|
FROM company_profiles
|
||
|
|
WHERE ($1 = '' OR LOWER(company_name) LIKE '%' || $1 || '%')
|
||
|
|
ORDER BY created_at DESC
|
||
|
|
LIMIT 100
|
||
|
|
"#,
|
||
|
|
search
|
||
|
|
)
|
||
|
|
.fetch_all(&state.pool)
|
||
|
|
.await
|
||
|
|
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("DB error: {e}")))?;
|
||
|
|
|
||
|
|
Ok(Json(companies))
|
||
|
|
}
|