teo_runtime/handler/default/
find_many.rs1use key_path::path;
2use crate::request::Request;
3use crate::value::Value;
4use crate::teon;
5use crate::action::action::*;
6use crate::response::Response;
7
8pub async fn find_many(request: Request) -> teo_result::Result<Response> {
9 let model = request.transaction_ctx().namespace().model_at_path(&request.handler_match().unwrap().path()).unwrap().clone();
10 let action = FIND | MANY | ENTRY;
11 let results = request.transaction_ctx().find_many_internal(
12 &model,
13 request.body_value()?,
14 false,
15 action,
16 Some(request.clone()),
17 path![],
18 ).await?;
19 let mut count_input = request.body_value()?.clone();
20 let count_input_obj = count_input.as_dictionary_mut().unwrap();
21 count_input_obj.remove("skip");
22 count_input_obj.remove("take");
23 count_input_obj.remove("pageSize");
24 count_input_obj.remove("pageNumber");
25 let count = request.transaction_ctx().count_objects(&model, &count_input, path![]).await.unwrap();
26 let mut meta = teon!({"count": count});
27 let page_size = request.body_value()?.get("pageSize");
28 if page_size.is_some() {
29 let page_size = page_size.unwrap().to_int64().unwrap();
30 let count = count as i64;
31 let mut number_of_pages = count / page_size;
32 if count % page_size != 0 {
33 number_of_pages += 1;
34 }
35 meta.as_dictionary_mut().unwrap().insert("numberOfPages".to_string(), number_of_pages.into());
36 }
37
38 let mut result_json: Vec<Value> = vec![];
39 for (index, result) in results.iter().enumerate() {
40 match result.to_teon_internal(&path!["data", index]).await {
41 Ok(result) => result_json.push(result),
42 Err(_) => return Err(teo_result::Error::unauthorized_pathed(path!["data", index], "not allowed to read")),
43 }
44 }
45 Ok(Response::data_meta(Value::Array(result_json), meta))
46}