Added Postgres backend and SQLx.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use serde::Serialize;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::database::dtos::transaction::TransactionRow;
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct TransactionResponse {
|
||||
pub id: Uuid,
|
||||
pub description: String,
|
||||
pub amount: f64,
|
||||
pub date: DateTime<Utc>,
|
||||
}
|
||||
|
||||
impl From<TransactionRow> for TransactionResponse {
|
||||
fn from(row: TransactionRow) -> Self {
|
||||
Self {
|
||||
id: row.id,
|
||||
description: row.description,
|
||||
amount: row.amount_cents as f64 / 100.0,
|
||||
date: row.date,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn converts_cents_to_major_currency_units() {
|
||||
let id = Uuid::new_v4();
|
||||
let date = Utc::now();
|
||||
let response = TransactionResponse::from(TransactionRow {
|
||||
id,
|
||||
description: "Groceries".to_string(),
|
||||
amount_cents: 4250,
|
||||
date,
|
||||
});
|
||||
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
assert_eq!(response.amount, 42.5);
|
||||
assert_eq!(response.date, date);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user