iT邦幫忙

2023 iThome 鐵人賽

DAY 13
0
Software Development

當rust 遇上 cqrs & es系列 第 13

D13 測試 reader query

  • 分享至 

  • xImage
  •  

初始化cqrs framework

let store = mem_store::MemStore::<Reader>::default();
let service = ReaderService {};
let readers = Arc::new(Mutex::new(HashMap::new()));
let query = Box::new(ReaderQuery {
    readers: readers.clone(),
    reader_borrowed_records: Arc::new(Mutex::new(vec![])),
});
let cqrs = CqrsFramework::new(
    store.clone(),
    vec![query],
    service.clone());

觀察commit後的資料

let command = ReaderCommand::CreateReader {
    name: "John Smith".to_string(),
};
let id = "test-aggregate-id-C450D1A";
cqrs.execute(&id, command).await.unwrap();
println!("=== commit 後的 readers dto  ===\n{:#?}", readers);
=== commit 後的 readers dto  ===
Mutex {
    data: {
        "test-aggregate-id-C450D1A": ReaderDto {
            id: "9d2e93c8-78de-470b-a08c-a95b7833a51b",
            name: "John Smith",
            books_borrowed: 0,
            next_due_date: None,
        },
    },
    poisoned: false,
    ..
}

再加一筆資料

let id2 = "test-aggregate-id-C450D1B";
let command = ReaderCommand::CreateReader {
    name: "Mary Ann".to_string(),
};
cqrs.execute(&id2, command).await.unwrap();
println!("=== commit 後的 readers dto  ===\n{:#?}", readers);
=== commit 後的 readers dto  ===
Mutex {
    data: {
        "test-aggregate-id-C450D1B": ReaderDto {
            id: "c9ce4332-a082-46e4-8c47-97cf1f9c455f",
            name: "Mary Ann",
            books_borrowed: 0,
            next_due_date: None,
        },
        "test-aggregate-id-C450D1A": ReaderDto {
            id: "11de35f9-7aa9-417b-8372-6f02f2ebc9fd",
            name: "John Smith",
            books_borrowed: 0,
            next_due_date: None,
        },
    },
    poisoned: false,
    ..
}

加入借閱操作

let command = ReaderCommand::BorrowBook {
    book_id: "test-book-id-1".to_string(),
    borrowed_date: Utc::now(),
    due_date: Utc::now().add(chrono::Duration::days(7)),
};

cqrs.execute(&id2, command).await.unwrap();
println!("=== commit 後的 readers dto  ===\n{:#?}", readers);
Mutex {
    data: {
        "test-aggregate-id-C450D1B": ReaderDto {
            id: "c9ce4332-a082-46e4-8c47-97cf1f9c455f",
            name: "Mary Ann",
            books_borrowed: 1,
            next_due_date: Some(
                2023-10-05T14:49:36.832577235Z,
            ),
        },
        "test-aggregate-id-C450D1A": ReaderDto {
            id: "11de35f9-7aa9-417b-8372-6f02f2ebc9fd",
            name: "John Smith",
            books_borrowed: 0,
            next_due_date: None,
        },
    },
    poisoned: false,
    ..
}

在cqrs framework裡加入設定好的query,在指令完成後會觸發query的更新,不過目前的實作只能更新同aggregate所擁有的資料。


上一篇
D12 Query of Reader
下一篇
D14 加persist層
系列文
當rust 遇上 cqrs & es30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言