deposit.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use crate::{Context, Handler};
  2. use actix_web::{post, web, HttpResponse, Responder};
  3. use serde::Deserialize;
  4. use serde_json::json;
  5. use verax::{AccountId, AnyAmount, Status, Tag};
  6. #[derive(Deserialize)]
  7. pub struct Deposit {
  8. pub account: AccountId,
  9. #[serde(flatten)]
  10. pub amount: AnyAmount,
  11. pub memo: String,
  12. pub tags: Vec<Tag>,
  13. pub status: Status,
  14. }
  15. #[async_trait::async_trait]
  16. impl Handler for Deposit {
  17. type Ok = verax::Transaction;
  18. type Err = verax::Error;
  19. async fn handle(self, ctx: &Context) -> Result<Self::Ok, Self::Err> {
  20. ctx.ledger
  21. .deposit(
  22. &self.account,
  23. self.amount.try_into()?,
  24. self.status,
  25. self.tags,
  26. self.memo,
  27. )
  28. .await
  29. }
  30. }
  31. #[post("/deposit")]
  32. pub async fn handler(item: web::Json<Deposit>, ledger: web::Data<Context>) -> impl Responder {
  33. // Insert the item into a database or another data source.
  34. // For this example, we'll just echo the received item.
  35. match item.into_inner().handle(&ledger).await {
  36. Ok(tx) => {
  37. // Insert the item into a database or another data source.
  38. // For this example, we'll just echo the received item.
  39. HttpResponse::Created().json(tx)
  40. }
  41. Err(err) => HttpResponse::BadRequest().json(json!({ "text": err.to_string(), "err": err})),
  42. }
  43. }