108 lines
3.2 KiB
Rust
108 lines
3.2 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::{domain, routes::dtos::direction::Direction};
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct TransactionRequest {
|
|
pub description: String,
|
|
pub amount_cents: u64,
|
|
pub direction: Direction,
|
|
pub date: DateTime<Utc>,
|
|
}
|
|
|
|
impl TransactionRequest {
|
|
pub fn into_domain(self, id: Uuid) -> Result<domain::Transaction, domain::ValidationError> {
|
|
Ok(domain::Transaction {
|
|
id,
|
|
description: self.description,
|
|
amount: domain::Cents::try_from(self.amount_cents)?,
|
|
direction: self.direction.into(),
|
|
date: self.date,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct TransactionResponse {
|
|
pub id: Uuid,
|
|
pub description: String,
|
|
pub amount_cents: u64,
|
|
pub direction: Direction,
|
|
pub date: DateTime<Utc>,
|
|
}
|
|
|
|
impl From<domain::Transaction> for TransactionResponse {
|
|
fn from(transaction: domain::Transaction) -> Self {
|
|
Self {
|
|
id: transaction.id,
|
|
description: transaction.description,
|
|
amount_cents: transaction.amount.get(),
|
|
direction: transaction.direction.into(),
|
|
date: transaction.date,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn request(amount_cents: u64) -> TransactionRequest {
|
|
TransactionRequest {
|
|
description: "Groceries".to_string(),
|
|
amount_cents,
|
|
direction: Direction::Out,
|
|
date: Utc::now(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn converts_between_dto_and_domain() {
|
|
let id = Uuid::new_v4();
|
|
let transaction = request(4250).into_domain(id).unwrap();
|
|
assert_eq!(transaction.id, id);
|
|
assert_eq!(transaction.amount.get(), 4250);
|
|
assert_eq!(transaction.direction, domain::Direction::Out);
|
|
|
|
let response = TransactionResponse::from(transaction);
|
|
assert_eq!(response.id, id);
|
|
assert_eq!(response.description, "Groceries");
|
|
assert_eq!(response.amount_cents, 4250);
|
|
}
|
|
|
|
#[test]
|
|
fn rejects_amounts_that_do_not_fit_in_database() {
|
|
let error = request(i64::MAX as u64 + 1)
|
|
.into_domain(Uuid::nil())
|
|
.unwrap_err();
|
|
assert!(matches!(
|
|
error,
|
|
domain::ValidationError::Amount(domain::CentsConversionError(value))
|
|
if value == i64::MAX as i128 + 1
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn keeps_transaction_json_flat() {
|
|
let response = TransactionResponse {
|
|
id: Uuid::nil(),
|
|
description: "Groceries".to_owned(),
|
|
amount_cents: 4250,
|
|
direction: Direction::Out,
|
|
date: Utc::now(),
|
|
};
|
|
|
|
let response = serde_json::to_value(response).unwrap();
|
|
assert_eq!(response["id"], Uuid::nil().to_string());
|
|
assert_eq!(response["amount_cents"], 4250);
|
|
assert_eq!(response["direction"], "out");
|
|
assert_eq!(response["description"], "Groceries");
|
|
|
|
let request: TransactionRequest = serde_json::from_value(response).unwrap();
|
|
assert_eq!(request.description, "Groceries");
|
|
assert_eq!(request.amount_cents, 4250);
|
|
}
|
|
}
|