use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn derive_update(name: &Ident, collection_name: String, _partial_fields: Vec<Ident>) -> TokenStream {
quote! {
async fn update(&self, ctx: &(impl mongo_crud_core::MongoContext + Sync)) -> bool {
let db = mongo_crud_core::MongoContext::get_database(ctx).await;
match db
.collection::<#name>(#collection_name)
.update_one(
mongodb::bson::doc! { "id": &self.id },
mongodb::bson::to_document(self).unwrap(),
None
)
.await
{
Ok(_) => true,
Err(e) => {
log::error!("Failed to update: {:?}", e);
false
}
}
}
}
}