sqlx-record/src/helpers.rs

30 lines
959 B
Rust

#[macro_export]
macro_rules! update_entity_func {
($form_type:ident, $func_name:ident) => {
pub async fn $func_name<'a, E>(executor: E, id: &Uuid, form: $form_type) -> Result<(), RepositoryError>
where
E: sqlx::Executor<'a, Database = $crate::prelude::DB>,
{
//If the section exists, we update it
let result = sqlx::query(
format!(r#"
UPDATE {}
SET {}
WHERE id = ?
"#,
$form_type::table_name(),
form.update_stmt()).as_str())
.bind_form_values(form)
.bind(id)
.execute(executor)
.await;
if let Err(err) = result {
tracing::error!("Error updating entity: {:?}", err);
return Err(RepositoryError::from(err));
}
Ok(())
}
};
}