use super::*; pub(super) fn router() -> Router { Router::new() .route("/api/records", get(records).post(register)) .route( "/api/records/{id}", patch(update_record).delete(delete_record), ) .route( "/api/records/{id}/photos/{slot}", get(photo).delete(delete_photo), ) .route("/api/records/{id}/archive", get(download_card)) .route("/r/{id}", get(public_card)) .route("/api/resolve", get(resolve)) .route("/api/archive/status", get(archive_status)) .route("/api/archive/retry", post(retry_archive)) .route("/api/export/archive", post(export_archive)) .route("/api/export/excel", post(export_excel)) } async fn records( State(s): State, LocalQuery(query): LocalQuery, ) -> Result> { Ok(Json( s.records(&query.q, query.page.as_deref(), query.page_size.as_deref()) .await?, )) } async fn register( State(s): State, Extension(Payload(body)): Extension, ) -> Result> { Ok(Json(s.register(body).await?)) } async fn update_record( State(s): State, Path(id): Path, Extension(Payload(body)): Extension, ) -> Result> { record_id(&id)?; Ok(Json(s.patch(&id, body).await?)) } async fn delete_record(State(s): State, Path(id): Path) -> Result> { record_id(&id)?; Ok(Json(s.delete(&id, None).await?)) } async fn delete_photo( State(s): State, Path((id, n)): Path<(String, String)>, ) -> Result> { record_id(&id)?; Ok(Json(s.delete(&id, Some(slot(&n)?)).await?)) } async fn photo( State(s): State, Path((id, n)): Path<(String, String)>, ) -> Result { record_id(&id)?; let (mime, bytes) = s.photo(&id, slot(&n)?).await?; Ok(binary(bytes, &mime, None, None)) } async fn download_card(State(s): State, Path(id): Path) -> Result { record_id(&id)?; let (html, name) = s.card(&id, true).await?; Ok(binary( html.into_bytes(), "text/html; charset=utf-8", Some(&name), Some( "default-src 'none'; img-src data:; style-src 'unsafe-inline'; base-uri 'none'; frame-ancestors 'none'", ), )) } async fn public_card(State(s): State, Path(id): Path) -> Result { record_id(&id)?; let (html, _) = s.card(&id, false).await?; Ok(binary( html.into_bytes(), "text/html; charset=utf-8", None, Some( "default-src 'none'; img-src 'self'; style-src 'unsafe-inline'; base-uri 'none'; frame-ancestors 'none'", ), )) } async fn resolve( State(s): State, LocalQuery(query): LocalQuery, ) -> Result> { Ok(Json(s.resolve(query.url.as_deref()).await?)) } async fn archive_status(State(s): State) -> Result> { Ok(Json(s.archive_status().await?)) } async fn retry_archive(State(s): State) -> Result> { Ok(Json(s.sync_archive().await?)) } async fn export_archive( State(s): State, Extension(Payload(body)): Extension, ) -> Result { Ok(binary( s.export(body, false).await?, "application/zip", Some("FPV-RID-archive.zip"), None, )) } async fn export_excel( State(s): State, Extension(Payload(body)): Extension, ) -> Result { Ok(binary( s.export(body, true).await?, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", Some("FPV-RID-records.xlsx"), None, )) }