1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- use crate::Context;
- use actix_web::{get, web, HttpResponse, Responder};
- use serde_json::json;
- use verax::{AnyId, Filter, Type};
- #[get("/{id}")]
- async fn handler(info: web::Path<AnyId>, ctx: web::Data<Context>) -> impl Responder {
- let (cache_for_ever, filter) = match info.0 {
- AnyId::Account(account_id) => (
- false,
- Filter {
- accounts: vec![account_id],
- typ: vec![Type::Deposit, Type::Withdrawal, Type::Transaction],
- ..Default::default()
- },
- ),
- AnyId::Revision(rev_id) => (
- true,
- Filter {
- revisions: vec![rev_id],
- limit: 1,
- ..Default::default()
- },
- ),
- AnyId::Transaction(transaction_id) => (
- false,
- Filter {
- ids: vec![transaction_id],
- limit: 1,
- ..Default::default()
- },
- ),
- AnyId::Payment(payment_id) => {
- let _ = ctx
- .ledger
- .get_payment_info(&payment_id)
- .await
- .map(|tx| HttpResponse::Ok().json(tx));
- todo!()
- }
- };
- let limit = filter.limit;
- ctx.ledger
- .get_transactions(filter)
- .await
- .map(|results| {
- let json_response = if limit == 1 {
- serde_json::to_value(&results[0])
- } else {
- serde_json::to_value(&results)
- }
- .unwrap();
- if cache_for_ever {
- HttpResponse::Ok()
- .header(
- "Cache-Control",
- "public, max-age=31536000, s-maxage=31536000, immutable",
- )
- .header("Vary", "Accept-Encoding")
- .json(json_response)
- } else {
- HttpResponse::Ok().json(json_response)
- }
- })
- .map_err(|err| {
- HttpResponse::InternalServerError().json(json!({ "text": err.to_string(), "err": err}))
- })
- }
|