Ver Fonte

Add API to lock transactions

Cesar Rodas há 9 meses atrás
pai
commit
1a996a81a4
2 ficheiros alterados com 51 adições e 2 exclusões
  1. 47 0
      src/lock.rs
  2. 4 2
      src/update.rs

+ 47 - 0
src/lock.rs

@@ -0,0 +1,47 @@
+use crate::{Context, Handler};
+use actix_web::{post, web, HttpResponse, Responder};
+use serde::{Deserialize, Serialize};
+use serde_json::json;
+use verax::{RevId, TxId};
+
+#[derive(Deserialize)]
+pub struct Lock {
+    id: TxId,
+    client_name: String,
+}
+
+#[derive(Serialize)]
+pub struct Response {
+    id: TxId,
+    #[serde(rename = "_rev")]
+    revision_id: RevId,
+    secret: String,
+}
+
+#[async_trait::async_trait]
+impl Handler for Lock {
+    type Ok = Response;
+    type Err = verax::Error;
+
+    async fn handle(self, ledger: &Context) -> Result<Self::Ok, Self::Err> {
+        let (new_tx, token) = ledger
+            .ledger
+            .lock_transaction(self.id, self.client_name)
+            .await?;
+        Ok(Response {
+            id: new_tx.id,
+            revision_id: new_tx.revision_id,
+            secret: token.to_string(),
+        })
+    }
+}
+
+#[post("/lock")]
+async fn handler(item: web::Json<Lock>, ctx: web::Data<Context>) -> impl Responder {
+    match item.into_inner().handle(&ctx).await {
+        Ok(tx) => HttpResponse::Accepted().json(tx),
+        Err(err) => {
+            HttpResponse::InternalServerError().json(json!({ "text": err.to_string(), "err": err}))
+        }
+    }
+}

+ 4 - 2
src/update.rs

@@ -27,10 +27,12 @@ impl Handler for Update {
     async fn handle(self, ledger: &Context) -> Result<Self::Ok, Self::Err> {
         let id = self.id;
         let memo = self.operation.memo;
+        let update_token = self.operation.update_token;
+
         let id = if let Some(status) = self.operation.status {
             let transaction = ledger
                 .ledger
-                .change_status(id, status, memo.clone(), None)
+                .change_status(id, status, memo.clone(), update_token.clone())
                 .await?;
             transaction.revision_id
         } else {
@@ -38,7 +40,7 @@ impl Handler for Update {
         };
 
         let id = if let Some(tags) = self.operation.tags {
-            let transaction = ledger.ledger.set_tags(id, tags, memo, None).await?;
+            let transaction = ledger.ledger.set_tags(id, tags, memo, update_token).await?;
             transaction.revision_id
         } else {
             id