1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- use crate::{Context, Handler};
- use actix_web::{post, web, HttpResponse, Responder};
- use serde::Deserialize;
- use serde_json::json;
- use verax::{AccountId, AnyAmount, Status, Tag};
- #[derive(Deserialize)]
- pub struct Deposit {
- pub account: AccountId,
- #[serde(flatten)]
- pub amount: AnyAmount,
- pub memo: String,
- pub tags: Vec<Tag>,
- pub status: Status,
- }
- #[async_trait::async_trait]
- impl Handler for Deposit {
- type Ok = verax::Transaction;
- type Err = verax::Error;
- async fn handle(self, ctx: &Context) -> Result<Self::Ok, Self::Err> {
- ctx.ledger
- .deposit(
- &self.account,
- self.amount.try_into()?,
- self.status,
- self.tags,
- self.memo,
- )
- .await
- }
- }
- #[post("/deposit")]
- pub async fn handler(item: web::Json<Deposit>, ledger: web::Data<Context>) -> impl Responder {
- // Insert the item into a database or another data source.
- // For this example, we'll just echo the received item.
- match item.into_inner().handle(&ledger).await {
- Ok(tx) => {
- // Insert the item into a database or another data source.
- // For this example, we'll just echo the received item.
- HttpResponse::Created().json(tx)
- }
- Err(err) => HttpResponse::BadRequest().json(json!({ "text": err.to_string(), "err": err})),
- }
- }
|