76 lines
1.8 KiB
Rust
76 lines
1.8 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use uuid::Uuid;
|
|
|
|
use crate::domain;
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
pub struct AccountRequest {
|
|
pub name: String,
|
|
}
|
|
|
|
impl AccountRequest {
|
|
pub fn into_domain(self, id: Uuid) -> Result<domain::Account, domain::DomainError> {
|
|
Ok(domain::Account {
|
|
id,
|
|
name: self.name,
|
|
})
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Clone, 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,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn request() -> AccountRequest {
|
|
AccountRequest {
|
|
name: "Savings".to_owned(),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn serializes_account_response() {
|
|
let id = Uuid::nil();
|
|
let account = request().into_domain(id).unwrap();
|
|
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
|
|
|
|
assert_eq!(response["id"], id.to_string());
|
|
assert_eq!(response["name"], "Savings");
|
|
}
|
|
|
|
#[test]
|
|
fn deserializes_account_response_as_request() {
|
|
let account = request().into_domain(Uuid::nil()).unwrap();
|
|
let response = serde_json::to_value(AccountResponse::from(account)).unwrap();
|
|
let request: AccountRequest = serde_json::from_value(response).unwrap();
|
|
|
|
assert_eq!(request.name, "Savings");
|
|
}
|
|
|
|
#[test]
|
|
fn converts_between_dto_and_domain() {
|
|
let id = Uuid::new_v4();
|
|
let account = request().into_domain(id).unwrap();
|
|
assert_eq!(account.id, id);
|
|
assert_eq!(account.name, "Savings");
|
|
|
|
let response = AccountResponse::from(account);
|
|
assert_eq!(response.id, id);
|
|
assert_eq!(response.name, "Savings");
|
|
}
|
|
}
|