Adding user IDs to data access transactions
For our transactions, we can start with the create with the following code:
// nanoservices/to_do/dal/src/to_do_items/transactions/create.rs
pub type SaveOneResponse = Result<ToDoItem, NanoServiceError>;
pub trait SaveOne {
    fn save_one(item: NewToDoItem, user_id: i32)
    -> impl Future<Output = SaveOneResponse> + Send;
}
#[cfg(feature = "sqlx-postgres")]
impl SaveOne for SqlxPostGresDescriptor {
    fn save_one(item: NewToDoItem, user_id: i32)
    -> impl Future<Output = SaveOneResponse> + Send {
        sqlx_postgres_save_one(item, user_id)
    }
}
#[cfg(feature = "json-file")]
impl SaveOne for JsonFileDescriptor {
    fn save_one(item: NewToDoItem, user_id: i32)
    -> impl Future<Output = SaveOneResponse> + Send {
        json_file_save_one(item, user_id)
    }
}Here, we can see that we merely pass in the user ID into the functions. For our sqlx_postgres_save_one function, we now...
 
                                             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
             
     
         
                 
                 
                 
                 
                 
                 
                 
                 
                