Made scheduled transactions more generic.
This commit is contained in:
@@ -3,23 +3,28 @@ use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::dtos::transaction::TransactionRow;
|
||||
use crate::routes::dtos::direction::{TransactionDirection, TransactionDirectionConversionError};
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TransactionResponse {
|
||||
pub id: Uuid,
|
||||
pub description: String,
|
||||
pub amount: f64,
|
||||
pub direction: TransactionDirection,
|
||||
pub date: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl From<TransactionRow> for TransactionResponse {
|
||||
fn from(row: TransactionRow) -> Self {
|
||||
Self {
|
||||
impl TryFrom<TransactionRow> for TransactionResponse {
|
||||
type Error = TransactionDirectionConversionError;
|
||||
|
||||
fn try_from(row: TransactionRow) -> Result<Self, Self::Error> {
|
||||
Ok(Self {
|
||||
id: row.id,
|
||||
description: row.description,
|
||||
amount: row.amount_cents as f64 / 100.0,
|
||||
direction: TransactionDirection::try_from(row.direction)?,
|
||||
date: row.date,
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,13 +36,16 @@ mod tests {
|
||||
fn converts_cents_to_major_currency_units() {
|
||||
let id = Uuid::new_v4();
|
||||
let date = Utc::now();
|
||||
let response = TransactionResponse::from(TransactionRow {
|
||||
let response = TransactionResponse::try_from(TransactionRow {
|
||||
id,
|
||||
description: "Groceries".to_string(),
|
||||
amount_cents: 4250,
|
||||
direction: "out".to_string(),
|
||||
date,
|
||||
});
|
||||
|
||||
let response = response.expect("valid direction");
|
||||
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
assert_eq!(response.amount, 42.5);
|
||||
|
||||
Reference in New Issue
Block a user