feat: get amount
This commit is contained in:
@@ -19,6 +19,7 @@ use crate::util;
|
||||
use crate::util::req::CommonResp;
|
||||
use chrono::prelude::*;
|
||||
use tracing::info;
|
||||
use crate::model::req::Params;
|
||||
|
||||
const PAYMENT_STORE_EXPO: i64 = 5;
|
||||
|
||||
@@ -176,6 +177,7 @@ pub async fn create_transaction(
|
||||
|
||||
let amount = db_model::AmountForm {
|
||||
uid: uid,
|
||||
account_id: amount_req.account_id,
|
||||
transaction_id: 0,
|
||||
value: value,
|
||||
expo: expo,
|
||||
@@ -338,5 +340,25 @@ pub async fn get_amounts_by_tid(
|
||||
claims: Claims,
|
||||
Query(params): Query<Params>,
|
||||
) -> Result<Json<Vec<db_model::Amount>>, (StatusCode, String)> {
|
||||
|
||||
info!(params.transaction_id);
|
||||
let tid = match params.transaction_id {
|
||||
None => 0,
|
||||
Some(idx) => idx,
|
||||
};
|
||||
let uid: i64 = claims.uid.clone();
|
||||
let conn = app_state
|
||||
.db
|
||||
.get()
|
||||
.await
|
||||
.map_err(util::req::internal_error)?;
|
||||
let res = conn.interact(move |conn| {
|
||||
schema::amounts::table
|
||||
.filter(schema::amounts::uid.eq(uid))
|
||||
.filter(schema::amounts::transaction_id.eq(tid))
|
||||
.select(db_model::Amount::as_select())
|
||||
.load(conn)
|
||||
}).await
|
||||
.map_err(util::req::internal_error)?
|
||||
.map_err(util::req::internal_error)?;
|
||||
Ok(Json(res))
|
||||
}
|
||||
@@ -1,7 +1,23 @@
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Params {
|
||||
#[serde(default, deserialize_with="empty_string_as_none")]
|
||||
transaction_id: Option<i64>,
|
||||
pub transaction_id: Option<i64>,
|
||||
}
|
||||
|
||||
// Serde deserialization decorator to map empty Strings to None,
|
||||
fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
T: FromStr,
|
||||
T::Err: fmt::Display,
|
||||
{
|
||||
let opt = Option::<String>::deserialize(de)?;
|
||||
match opt.as_deref() {
|
||||
None | Some("") => Ok(None),
|
||||
Some(s) => FromStr::from_str(s).map_err(de::Error::custom).map(Some),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user