use proc_macro2::{Ident, TokenStream};
use quote::quote;
pub fn derive_find_one(name: &Ident, collection_name: String, _partial_fields: Vec<Ident>) -> TokenStream {
quote! {
async fn find(&self, ctx: &(impl mongo_crud_core::MongoContext + Sync), id: &str) -> Self {
let db = mongo_crud_core::MongoContext::get_database(ctx).await;
match db
.collection::<#name>(#collection_name)
.find_one(mongodb::bson::doc! { "id": id }, None)
.await
{
Ok(Some(item)) => item,
Ok(None) => panic!("Document not found"),
Err(e) => panic!("Error finding document: {:?}", e)
}
}
}
}