123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- use crate::{Context, Handler};
- use actix_web::{post, web, HttpResponse, Responder};
- use serde::Deserialize;
- use serde_json::json;
- use verax::{RevId, Status, Tag, TokenPayload};
- #[derive(Deserialize)]
- pub struct UpdateOperation {
- #[serde(default)]
- pub status: Option<Status>,
- #[serde(default)]
- pub tags: Option<Vec<Tag>>,
- pub memo: String,
- pub update_token: Option<TokenPayload>,
- }
- struct Update {
- id: RevId,
- operation: UpdateOperation,
- }
- #[async_trait::async_trait]
- impl Handler for Update {
- type Ok = verax::Transaction;
- type Err = verax::Error;
- 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(), update_token.clone())
- .await?;
- transaction.revision_id
- } else {
- id
- };
- let id = if let Some(tags) = self.operation.tags {
- let transaction = ledger.ledger.set_tags(id, tags, memo, update_token).await?;
- transaction.revision_id
- } else {
- id
- };
- ledger
- .ledger
- .get_transactions(verax::Filter {
- revisions: vec![id],
- limit: 1,
- ..Default::default()
- })
- .await?
- .pop()
- .ok_or(verax::Error::TxNotFound)
- }
- }
- #[post("/{id}")]
- async fn handler(
- info: web::Path<RevId>,
- item: web::Json<UpdateOperation>,
- ctx: web::Data<Context>,
- ) -> impl Responder {
- let update = Update {
- id: info.into_inner(),
- operation: item.into_inner(),
- };
- match update.handle(&ctx).await {
- Ok(tx) => HttpResponse::Accepted().json(tx),
- Err(err) => {
- HttpResponse::InternalServerError().json(json!({ "text": err.to_string(), "err": err}))
- }
- }
- }
|