get.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. use crate::Context;
  2. use actix_web::{get, web, HttpResponse, Responder};
  3. use serde_json::json;
  4. use verax::{AnyId, Filter, Type};
  5. #[get("/{id}")]
  6. async fn handler(info: web::Path<AnyId>, ctx: web::Data<Context>) -> impl Responder {
  7. let (cache_for_ever, filter) = match info.0 {
  8. AnyId::Account(account_id) => (
  9. false,
  10. Filter {
  11. accounts: vec![account_id],
  12. typ: vec![Type::Deposit, Type::Withdrawal, Type::Transaction],
  13. ..Default::default()
  14. },
  15. ),
  16. AnyId::Revision(rev_id) => (
  17. true,
  18. Filter {
  19. revisions: vec![rev_id],
  20. limit: 1,
  21. ..Default::default()
  22. },
  23. ),
  24. AnyId::Transaction(transaction_id) => (
  25. false,
  26. Filter {
  27. ids: vec![transaction_id],
  28. limit: 1,
  29. ..Default::default()
  30. },
  31. ),
  32. AnyId::Payment(payment_id) => {
  33. let _ = ctx
  34. .ledger
  35. .get_payment_info(&payment_id)
  36. .await
  37. .map(|tx| HttpResponse::Ok().json(tx));
  38. todo!()
  39. }
  40. };
  41. let limit = filter.limit;
  42. ctx.ledger
  43. .get_transactions(filter)
  44. .await
  45. .map(|results| {
  46. let json_response = if limit == 1 {
  47. serde_json::to_value(&results[0])
  48. } else {
  49. serde_json::to_value(&results)
  50. }
  51. .unwrap();
  52. if cache_for_ever {
  53. HttpResponse::Ok()
  54. .header(
  55. "Cache-Control",
  56. "public, max-age=31536000, s-maxage=31536000, immutable",
  57. )
  58. .header("Vary", "Accept-Encoding")
  59. .json(json_response)
  60. } else {
  61. HttpResponse::Ok().json(json_response)
  62. }
  63. })
  64. .map_err(|err| {
  65. HttpResponse::InternalServerError().json(json!({ "text": err.to_string(), "err": err}))
  66. })
  67. }