72 lines
1.7 KiB
Rust
72 lines
1.7 KiB
Rust
use chrono::{DateTime, Utc};
|
|
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use super::direction::Direction;
|
|
use crate::domain;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct TransactionRequest {
|
|
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,
|
|
account_id: self.account.id,
|
|
amount: self.amount_cents.try_into()?,
|
|
direction: self.direction.into(),
|
|
date: self.date,
|
|
description: self.description,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct TransactionResponse {
|
|
pub id: Uuid,
|
|
pub account: AccountResponse,
|
|
pub amount_cents: u64,
|
|
pub direction: Direction,
|
|
pub date: DateTime<Utc>,
|
|
pub description: String,
|
|
}
|
|
|
|
impl TransactionResponse {
|
|
pub fn new(transaction: domain::Transaction, account: domain::Account) -> Self {
|
|
Self {
|
|
id: transaction.id,
|
|
account: account.into(),
|
|
amount_cents: transaction.amount.get(),
|
|
direction: transaction.direction.into(),
|
|
date: transaction.date,
|
|
description: transaction.description,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct AccountRequest {
|
|
pub id: Uuid,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct AccountResponse {
|
|
pub id: Uuid,
|
|
pub name: String,
|
|
}
|
|
|
|
impl From<domain::Account> for AccountResponse {
|
|
fn from(account: domain::Account) -> Self {
|
|
Self {
|
|
id: account.id,
|
|
name: account.name,
|
|
}
|
|
}
|
|
}
|