feat: transaction time
This commit is contained in:
@@ -9,6 +9,7 @@ use axum_macros::debug_handler;
|
||||
use diesel::dsl::exists;
|
||||
use diesel::prelude::*;
|
||||
use std::fmt;
|
||||
use chrono::ParseResult;
|
||||
// use diesel::update;
|
||||
use serde::{Deserialize, Serialize};
|
||||
// use serde_json::to_string;
|
||||
@@ -29,7 +30,7 @@ pub struct SubmitTransactionRequest {
|
||||
book_id: i64,
|
||||
category_id: i64,
|
||||
tag_ids: Vec<i64>,
|
||||
time: String,
|
||||
time: String, // RFC 3339 "2020-04-12T22:10:57+02:00"
|
||||
amounts: Vec<SubmitTransactionAmountRequest>,
|
||||
}
|
||||
|
||||
@@ -192,6 +193,14 @@ pub async fn create_transaction(
|
||||
}
|
||||
|
||||
// 2. build and insert into db
|
||||
let datetime_tz = chrono::DateTime::parse_from_rfc3339(payload.time.as_str());
|
||||
let datetime = match datetime_tz {
|
||||
Ok(dt) => dt,
|
||||
Err(_) => {
|
||||
return Err((StatusCode::BAD_REQUEST, "invalid datetime, must be RFC 3339".to_string()))
|
||||
}
|
||||
};
|
||||
let datetime_utc = datetime.with_timezone(&Utc);
|
||||
|
||||
let mut transaction_resp: CreateTransactionResponse;
|
||||
let mut amount_ids: Vec<i64> = Vec::new();
|
||||
@@ -206,7 +215,7 @@ pub async fn create_transaction(
|
||||
description: payload.description,
|
||||
category_id: payload.category_id,
|
||||
// time: payload
|
||||
time: Utc::now(),
|
||||
time: datetime_utc,
|
||||
};
|
||||
let inserted_transactions = diesel::insert_into(schema::transactions::table)
|
||||
.values(&new_transaction)
|
||||
@@ -268,13 +277,23 @@ pub async fn update_transaction(
|
||||
.await
|
||||
.map_err(util::req::internal_error)?;
|
||||
let now = Utc::now().naive_utc();
|
||||
let datetime_tz = chrono::DateTime::parse_from_rfc3339(payload.time.as_str());
|
||||
let datetime = match datetime_tz {
|
||||
Ok(dt) => dt,
|
||||
Err(_) => {
|
||||
return Err((StatusCode::BAD_REQUEST, "invalid datetime, must be RFC 3339".to_string()))
|
||||
}
|
||||
};
|
||||
let datetime_utc = datetime.with_timezone(&Utc);
|
||||
let res = conn
|
||||
.interact(move |conn| {
|
||||
diesel::update(schema::transactions::table)
|
||||
.filter(schema::transactions::id.eq(id))
|
||||
.filter(schema::transactions::uid.eq(uid))
|
||||
.set((
|
||||
schema::transactions::category_id.eq(payload.category_id),
|
||||
schema::transactions::description.eq(payload.description),
|
||||
schema::transactions::time.eq(datetime_utc),
|
||||
schema::transactions::update_at.eq(now),
|
||||
))
|
||||
.execute(conn)
|
||||
|
||||
Reference in New Issue
Block a user