Added accounts API.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
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 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");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keeps_account_json_flat() {
|
||||
let response = AccountResponse {
|
||||
id: Uuid::nil(),
|
||||
name: "Savings".to_owned(),
|
||||
};
|
||||
|
||||
let response = serde_json::to_value(response).unwrap();
|
||||
assert_eq!(response["id"], Uuid::nil().to_string());
|
||||
assert_eq!(response["name"], "Savings");
|
||||
|
||||
let request: AccountRequest = serde_json::from_value(response).unwrap();
|
||||
assert_eq!(request.name, "Savings");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user