Refactored src and dst account to just account in preparation for a "counter party", instead of a full account.
This commit is contained in:
@@ -7,20 +7,22 @@ use crate::domain;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct TransactionRequest {
|
||||
pub description: String,
|
||||
pub account: AccountRequest,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub date: DateTime<Utc>,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl TransactionRequest {
|
||||
pub fn into_domain(self, id: Uuid) -> Result<domain::Transaction, domain::DomainError> {
|
||||
Ok(domain::Transaction {
|
||||
id,
|
||||
description: self.description,
|
||||
account_id: self.account.id,
|
||||
amount: self.amount_cents.try_into()?,
|
||||
direction: self.direction.into(),
|
||||
date: self.date,
|
||||
description: self.description,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -28,24 +30,40 @@ impl TransactionRequest {
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct TransactionResponse {
|
||||
pub id: Uuid,
|
||||
pub description: String,
|
||||
pub account: AccountResponse,
|
||||
pub amount_cents: u64,
|
||||
pub direction: Direction,
|
||||
pub date: DateTime<Utc>,
|
||||
pub description: String,
|
||||
}
|
||||
|
||||
impl From<domain::Transaction> for TransactionResponse {
|
||||
fn from(transaction: domain::Transaction) -> Self {
|
||||
impl TransactionResponse {
|
||||
pub fn new(transaction: domain::Transaction, account: domain::Account) -> Self {
|
||||
Self {
|
||||
id: transaction.id,
|
||||
description: transaction.description,
|
||||
account: AccountResponse {
|
||||
id: account.id,
|
||||
name: account.name,
|
||||
},
|
||||
amount_cents: transaction.amount.get(),
|
||||
direction: transaction.direction.into(),
|
||||
date: transaction.date,
|
||||
description: transaction.description,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct AccountRequest {
|
||||
pub id: Uuid,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct AccountResponse {
|
||||
pub id: Uuid,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use chrono::TimeZone;
|
||||
@@ -54,10 +72,11 @@ mod tests {
|
||||
|
||||
fn request() -> TransactionRequest {
|
||||
TransactionRequest {
|
||||
description: "Groceries".to_string(),
|
||||
account: AccountRequest { id: Uuid::nil() },
|
||||
amount_cents: 4250,
|
||||
direction: Direction::Out,
|
||||
date: Utc.with_ymd_and_hms(2026, 9, 29, 12, 0, 0).unwrap(),
|
||||
description: "Groceries".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,26 +85,53 @@ mod tests {
|
||||
let id = Uuid::nil();
|
||||
let date = request().date;
|
||||
let transaction = request().into_domain(id).unwrap();
|
||||
let response = serde_json::to_value(TransactionResponse::from(transaction)).unwrap();
|
||||
let account = domain::Account {
|
||||
id: Uuid::nil(),
|
||||
name: "Checking".to_owned(),
|
||||
};
|
||||
let response =
|
||||
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
|
||||
|
||||
assert_eq!(response["id"], id.to_string());
|
||||
assert_eq!(response["description"], "Groceries");
|
||||
assert_eq!(response["account"]["id"], Uuid::nil().to_string());
|
||||
assert_eq!(response["account"]["name"], "Checking");
|
||||
assert_eq!(response["amount_cents"], 4250);
|
||||
assert_eq!(response["direction"], "out");
|
||||
assert_eq!(response["date"], serde_json::to_value(date).unwrap());
|
||||
assert_eq!(response["description"], "Groceries");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deserializes_transaction_response_as_request() {
|
||||
let date = request().date;
|
||||
let transaction = request().into_domain(Uuid::nil()).unwrap();
|
||||
let response = serde_json::to_value(TransactionResponse::from(transaction)).unwrap();
|
||||
let account = domain::Account {
|
||||
id: Uuid::nil(),
|
||||
name: "Checking".to_owned(),
|
||||
};
|
||||
let response =
|
||||
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
|
||||
let request: TransactionRequest = serde_json::from_value(response).unwrap();
|
||||
|
||||
assert_eq!(request.description, "Groceries");
|
||||
assert_eq!(request.amount_cents, 4250);
|
||||
assert_eq!(request.account.id, Uuid::nil());
|
||||
assert!(matches!(request.direction, Direction::Out));
|
||||
assert_eq!(request.date, date);
|
||||
assert_eq!(request.description, "Groceries");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn requires_account_in_transaction_request() {
|
||||
let transaction = request().into_domain(Uuid::nil()).unwrap();
|
||||
let account = domain::Account {
|
||||
id: Uuid::nil(),
|
||||
name: "Checking".to_owned(),
|
||||
};
|
||||
let mut response =
|
||||
serde_json::to_value(TransactionResponse::new(transaction, account)).unwrap();
|
||||
response.as_object_mut().unwrap().remove("account");
|
||||
|
||||
assert!(serde_json::from_value::<TransactionRequest>(response).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -93,18 +139,28 @@ mod tests {
|
||||
let id = Uuid::new_v4();
|
||||
let request = request();
|
||||
let date = request.date;
|
||||
let account_id = request.account.id;
|
||||
|
||||
let transaction = request.into_domain(id).unwrap();
|
||||
assert_eq!(transaction.id, id);
|
||||
assert_eq!(transaction.account_id, account_id);
|
||||
assert_eq!(transaction.amount.get(), 4250);
|
||||
assert_eq!(transaction.direction, domain::Direction::Out);
|
||||
assert_eq!(transaction.date, date);
|
||||
|
||||
let response = TransactionResponse::from(transaction);
|
||||
let response = TransactionResponse::new(
|
||||
transaction,
|
||||
domain::Account {
|
||||
id: account_id,
|
||||
name: "Checking".to_owned(),
|
||||
},
|
||||
);
|
||||
assert_eq!(response.id, id);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
assert_eq!(response.account.id, account_id);
|
||||
assert_eq!(response.account.name, "Checking");
|
||||
assert_eq!(response.amount_cents, 4250);
|
||||
assert!(matches!(response.direction, Direction::Out));
|
||||
assert_eq!(response.date, date);
|
||||
assert_eq!(response.description, "Groceries");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user