Skip to content

Commit

Permalink
Add the tree method for DefaultController
Browse files Browse the repository at this point in the history
  • Loading branch information
photino committed Oct 30, 2023
1 parent 3bf0c3f commit c521ac2
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 9 deletions.
1 change: 0 additions & 1 deletion examples/actix-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false

[dependencies]
actix-web = "4.4.0"
fluent = "0.16.0"
tracing = "0.1.40"

[dependencies.serde]
Expand Down
1 change: 0 additions & 1 deletion examples/actix-app/src/controller/user.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use fluent::fluent_args;
use std::time::Instant;
use zino::{prelude::*, Request, Response, Result};
use zino_model::user::User;
Expand Down
1 change: 1 addition & 0 deletions examples/actix-app/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn tag_router(cfg: &mut ServiceConfig) {
.route("/{id}/update", post().to(Tag::update))
.route("/{id}/view", get().to(Tag::view))
.route("/list", get().to(Tag::list))
.route("/tree", get().to(Tag::tree))
.route("/schema", get().to(Tag::schema)),
);
}
Expand Down
1 change: 0 additions & 1 deletion examples/axum-app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ publish = false

[dependencies]
axum = "0.6.20"
fluent = "0.16.0"
tracing = "0.1.40"

[dependencies.serde]
Expand Down
1 change: 0 additions & 1 deletion examples/axum-app/src/controller/user.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::model::User;
use fluent::fluent_args;
use std::time::Instant;
use zino::{prelude::*, Request, Response, Result};

Expand Down
1 change: 1 addition & 0 deletions examples/axum-app/src/router/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub fn routes() -> Vec<Router> {
.route("/tag/:id/update", post(Tag::update))
.route("/tag/:id/view", get(Tag::view))
.route("/tag/list", get(Tag::list))
.route("/tag/tree", get(Tag::tree))
.route("/tag/schema", get(Tag::schema));
routes.push(router);

Expand Down
6 changes: 3 additions & 3 deletions zino-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ serde_qs = "0.12.0"
sha2 = "0.10.8"
sysinfo = "0.29.10"
task-local-extensions = "0.1.4"
toml = "0.8.5"
toml = "0.8.6"
tracing = "0.1.40"
tracing-appender = "0.2.2"
url = "2.4.1"
Expand All @@ -178,7 +178,7 @@ version = "0.5.2"
features = ["std"]

[dependencies.async-openai]
version = "0.14.3"
version = "0.15.0"
optional = true

[dependencies.chrono]
Expand Down Expand Up @@ -301,7 +301,7 @@ data-encoding = "2.4.0"
libsm = "0.5.1"
sm3 = "0.4.2"
smallvec = "1.11.1"
sonic-rs = "0.1.3"
sonic-rs = "0.2.1"
tinyvec = { version = "1.6.0", features = ["alloc"] }
uuid-simd = "0.8.0"

Expand Down
3 changes: 3 additions & 0 deletions zino-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub mod schedule;
pub mod state;
pub mod trace;

#[doc(no_inline)]
pub use fluent::fluent_args;

#[doc(no_inline)]
pub use serde_json::json;

Expand Down
2 changes: 1 addition & 1 deletion zino-dioxus/src/prelude/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Re-exports of dioxus components and common types.
//! Re-exports of components and common types.

pub use crate::{
class::Class,
Expand Down
55 changes: 55 additions & 0 deletions zino/src/controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub trait DefaultController<K, U = K> {
/// Exports model data.
async fn export(req: Self::Request) -> Self::Result;

/// Gets the tree hierarchy data.
async fn tree(req: Self::Request) -> Self::Result;

/// Gets the model schema.
async fn schema(req: Self::Request) -> Self::Result;
}
Expand Down Expand Up @@ -323,6 +326,57 @@ where
Ok(res.into())
}

async fn tree(req: Self::Request) -> Self::Result {
let mut query = Self::default_list_query();
let extension = req.get_data::<<Self as ModelHooks>::Extension>();
Self::before_list(&mut query, extension.as_ref())
.await
.extract(&req)?;

let mut res = req.query_validation(&mut query)?;
let parent_id = req.get_query("parent_id").unwrap_or("null");
query.add_filter("parent_id", parent_id);

let mut models = if query.populate_enabled() {
Self::fetch(&query).await.extract(&req)?
} else {
let mut models = Self::find(&query).await.extract(&req)?;
let translate_enabled = query.translate_enabled();
for model in models.iter_mut() {
Self::after_decode(model).await.extract(&req)?;
translate_enabled.then(|| Self::translate_model(model));
}
models
};

let primary_key_name = Self::PRIMARY_KEY_NAME;
let values = models
.iter()
.filter_map(|model| model.get(primary_key_name).cloned())
.collect::<Vec<_>>();
let mut query = Self::default_snapshot_query();
query.allow_fields(&["parent_id"]);
query.add_filter("parent_id", Map::from_entry("$in", values));
query.add_filter("status", Map::from_entry("$ne", "Deleted"));
query.set_sort_order("created_at", true);
query.set_limit(0);

let mut children = Self::find::<Map>(&query).await.extract(&req)?;
let total_rows = children.len();
for model in models.iter_mut() {
let model_id = model.get(primary_key_name);
let model_children = children
.extract_if(|child| child.get("parent_id") == model_id)
.collect::<Vec<_>>();
model.upsert("children", model_children);
}

let mut data = Map::data_entries(models);
data.upsert("total_rows", total_rows);
res.set_json_data(data);
Ok(res.into())
}

async fn schema(req: Self::Request) -> Self::Result {
let reader_available = Self::acquire_reader()
.await
Expand All @@ -331,6 +385,7 @@ where
.await
.is_ok_and(|cp| cp.is_available());
let data = json!({
"avro_schema": Self::schema(),
"model_name": Self::model_name(),
"model_namespace": Self::model_namespace(),
"table_name": Self::table_name(),
Expand Down
1 change: 1 addition & 0 deletions zino/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#![forbid(unsafe_code)]
#![feature(async_fn_in_trait)]
#![feature(doc_auto_cfg)]
#![feature(extract_if)]
#![feature(lazy_cell)]
#![feature(let_chains)]

Expand Down
2 changes: 1 addition & 1 deletion zino/src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use zino_core::{
error::Error,
extension::{JsonObjectExt, JsonValueExt, TomlTableExt},
file::NamedFile,
json,
fluent_args, json,
model::{Model, ModelHooks, Mutation, Query, QueryContext},
reject,
request::{RequestContext, Validation},
Expand Down

0 comments on commit c521ac2

Please sign in to comment.