Connecting our Transactions to the Core
Now that we have defined the interface of our storage engine using traits, our core can now be agnostic to the storage engine meaning that our core dependencies now have the following:
# nanoservices/to_do/core/Cargo.toml
[dependencies]
dal = { path = "../dal" }
serde = { version = "1.0.197", features = ["derive"] }
glue = { path = "../../../glue"}
We can now redefine our create endpoint with the code below:
// nanoservices/to_do/core/src/api/basic_actions/create.rs
use dal::to_do_items::schema::{NewToDoItem, ToDoItem};
use dal::to_do_items::transactions::create::SaveOne;
use glue::errors::NanoServiceError;
pub async fn create<T: SaveOne>(item: NewToDoItem)
-> Result<ToDoItem, NanoServiceError> {
let created_item = T::save_one(item).await?;
Ok(created_item)
}
Here, we can see that the generic parameter declaring the SaveOne
trait can be directly called without having to pass...