Skip to content

Commit

Permalink
+ added ..Bmc::first (base and gen crud macro
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremychone committed Feb 1, 2024
1 parent 7bfb282 commit c63a597
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
30 changes: 30 additions & 0 deletions crates/libs/lib-core/src/model/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,36 @@ mod tests {
Ok(())
}

#[serial]
#[tokio::test]
async fn test_first_ok() -> Result<()> {
// -- Setup & Fixtures
let mm = _dev_utils::init_test().await;
let ctx = Ctx::root_ctx();

let fx_agent_names = &["test_first_ok agent 01", "test_first_ok agent 02"];
seed_agents(&ctx, &mm, fx_agent_names).await?;

// -- Exec
let agent_filter: AgentFilter = serde_json::from_value(json!(
{
"name": {"$startsWith": "test_first_ok agent"}
}
))?;
let agent =
AgentBmc::first(&ctx, &mm, Some(vec![agent_filter]), None).await?;

// -- Check
let agent = agent.ok_or("No Agent Returned (should have returned one")?;
assert_eq!(agent.name, fx_agent_names[0]);

// -- Clean
let count = clean_agents(&ctx, &mm, "test_first_ok agent").await?;
assert_eq!(count, 2, "Should have cleaned 2 agents");

Ok(())
}

#[serial]
#[tokio::test]
async fn test_list_ok() -> Result<()> {
Expand Down
37 changes: 37 additions & 0 deletions crates/libs/lib-core/src/model/base/crud_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,43 @@ where
Ok(entity)
}

pub async fn first<MC, E, F>(
ctx: &Ctx,
mm: &ModelManager,
filter: Option<F>,
list_options: Option<ListOptions>,
) -> Result<Option<E>>
where
MC: DbBmc,
F: Into<FilterGroups>,
E: for<'r> FromRow<'r, PgRow> + Unpin + Send,
E: HasFields,
{
let list_options = match list_options {
Some(mut list_options) => {
// Reset the offset/limit
list_options.offset = None;
list_options.limit = Some(1);

// Don't change order_bys if not empty,
// otherwise, set it to id (creation asc order)
list_options.order_bys =
list_options.order_bys.or_else(|| Some("id".into()));

list_options
}
None => ListOptions {
limit: Some(1),
offset: None,
order_bys: Some("id".into()), // default id asc
},
};

list::<MC, E, F>(ctx, mm, filter, Some(list_options))
.await
.map(|item| item.into_iter().next())
}

pub async fn list<MC, E, F>(
_ctx: &Ctx,
mm: &ModelManager,
Expand Down
9 changes: 9 additions & 0 deletions crates/libs/lib-core/src/model/base/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ macro_rules! generate_common_bmc_fns {
base::get::<Self, _>(ctx, mm, id).await
}

pub async fn first(
ctx: &Ctx,
mm: &ModelManager,
filter: Option<Vec<$filter>>,
list_options: Option<ListOptions>,
) -> Result<Option<$entity>> {
base::first::<Self, _, _>(ctx, mm, filter, list_options).await
}

pub async fn list(
ctx: &Ctx,
mm: &ModelManager,
Expand Down

0 comments on commit c63a597

Please sign in to comment.