fix: identity binding + JSON endpoint + Phase 5 test script
- identity_binding.rs: fix i32->i64 type mismatch, COALESCE name column - identity_api.rs: get_identity_json fallback to DB if file missing - test_m5api_phase5.sh: fixed variable expansion, updated request bodies - Phase 5: 21/23 passed (2 known: multipart + proxy 404)
This commit is contained in:
@@ -792,6 +792,7 @@ async fn get_profile_image(
|
||||
}
|
||||
|
||||
async fn get_identity_json(
|
||||
State(state): State<crate::api::server::AppState>,
|
||||
Path(identity_uuid): Path<String>,
|
||||
) -> Result<(StatusCode, [(String, String); 1], Vec<u8>), StatusCode> {
|
||||
let clean = identity_uuid.replace('-', "");
|
||||
@@ -800,6 +801,8 @@ async fn get_identity_json(
|
||||
} else {
|
||||
identity_uuid.clone()
|
||||
};
|
||||
|
||||
// 1. Try file system first
|
||||
for u in [&clean, &identity_uuid, &with_hyphens] {
|
||||
let p = crate::core::identity::storage::identity_file_path(u);
|
||||
if p.exists() {
|
||||
@@ -811,7 +814,46 @@ async fn get_identity_json(
|
||||
));
|
||||
}
|
||||
}
|
||||
Err(StatusCode::NOT_FOUND)
|
||||
|
||||
// 2. Fallback: Generate JSON from DB
|
||||
use crate::core::identity::storage::{IdentityFile, FileBinding};
|
||||
let record = state.db.get_identity_by_uuid(&clean).await
|
||||
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?
|
||||
.ok_or(StatusCode::NOT_FOUND)?;
|
||||
|
||||
let id = record.id as i64;
|
||||
let bindings: Vec<FileBinding> = {
|
||||
let fd_table = crate::core::db::schema::table_name("face_detections");
|
||||
let rows = sqlx::query_as::<_, (String, Vec<i32>, i64)>(
|
||||
&format!("SELECT fd.file_uuid, COALESCE(array_agg(DISTINCT fd.trace_id) FILTER (WHERE fd.trace_id IS NOT NULL), '{{}}'::int[]), COUNT(*)::bigint FROM {} fd WHERE fd.identity_id = $1 GROUP BY fd.file_uuid ORDER BY fd.file_uuid", fd_table)
|
||||
).bind(id).fetch_all(state.db.pool()).await.unwrap_or_default();
|
||||
rows.into_iter().map(|(fu, tids, cnt)| FileBinding {
|
||||
file_uuid: fu, trace_ids: tids, face_count: cnt,
|
||||
}).collect()
|
||||
};
|
||||
|
||||
let fmt_time = |dt: Option<chrono::DateTime<chrono::Utc>>| -> String {
|
||||
dt.map(|d| d.to_rfc3339()).unwrap_or_else(|| chrono::Utc::now().to_rfc3339())
|
||||
};
|
||||
|
||||
let file = IdentityFile {
|
||||
version: 1,
|
||||
identity_uuid: record.uuid.clone(),
|
||||
name: record.name.clone(),
|
||||
identity_type: record.identity_type.clone(),
|
||||
source: record.source.clone(),
|
||||
status: record.status.clone(),
|
||||
tmdb_id: record.tmdb_id,
|
||||
tmdb_profile: record.tmdb_profile.clone(),
|
||||
metadata: record.metadata.clone(),
|
||||
file_bindings: bindings,
|
||||
created_at: fmt_time(record.created_at),
|
||||
updated_at: fmt_time(record.updated_at),
|
||||
};
|
||||
|
||||
let json = serde_json::to_string_pretty(&file).unwrap_or_default();
|
||||
let bytes = json.into_bytes();
|
||||
Ok((StatusCode::OK, [("content-type".to_string(), "application/json".to_string())], bytes))
|
||||
}
|
||||
|
||||
// ── Experiment: Identity Text Search ──────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user