feat: API preview endpoint for import sessions
This commit is contained in:
@@ -635,6 +635,70 @@ pub async fn api_put_mapping(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn api_get_preview(
|
||||
State(state): State<AppState>,
|
||||
AuthenticatedUser(user_id): AuthenticatedUser,
|
||||
Path(session_id_str): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
let Ok(session_id) = session_id_str
|
||||
.parse::<uuid::Uuid>()
|
||||
.map(ImportSessionId::from_uuid)
|
||||
else {
|
||||
return (
|
||||
StatusCode::BAD_REQUEST,
|
||||
axum::Json(serde_json::json!({"error": "invalid session id"})),
|
||||
)
|
||||
.into_response();
|
||||
};
|
||||
match state
|
||||
.app_ctx
|
||||
.repos
|
||||
.import_session
|
||||
.get(&session_id, &user_id)
|
||||
.await
|
||||
{
|
||||
Ok(Some(session)) => {
|
||||
let annotated: Vec<domain::models::AnnotatedRow> =
|
||||
session.row_results.unwrap_or_default();
|
||||
let rows: Vec<serde_json::Value> = annotated
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, a)| {
|
||||
use domain::models::import::RowResult;
|
||||
match &a.result {
|
||||
RowResult::Valid(row) => serde_json::json!({
|
||||
"index": i,
|
||||
"status": if a.is_duplicate { "duplicate" } else { "valid" },
|
||||
"title": row.title,
|
||||
"release_year": row.release_year,
|
||||
"director": row.director,
|
||||
"rating": row.rating,
|
||||
"watched_at": row.watched_at,
|
||||
"comment": row.comment,
|
||||
}),
|
||||
RowResult::Invalid { errors, .. } => serde_json::json!({
|
||||
"index": i,
|
||||
"status": "invalid",
|
||||
"errors": errors,
|
||||
}),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
axum::Json(serde_json::json!({ "rows": rows })).into_response()
|
||||
}
|
||||
Ok(None) => (
|
||||
StatusCode::NOT_FOUND,
|
||||
axum::Json(serde_json::json!({"error": "session not found"})),
|
||||
)
|
||||
.into_response(),
|
||||
Err(e) => (
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
axum::Json(serde_json::json!({"error": e.to_string()})),
|
||||
)
|
||||
.into_response(),
|
||||
}
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post, path = "/api/v1/import/sessions/{id}/confirm",
|
||||
params(("id" = String, Path, description = "Import session UUID")),
|
||||
|
||||
Reference in New Issue
Block a user