Skip to content

Commit

Permalink
feat: db page 接口
Browse files Browse the repository at this point in the history
  • Loading branch information
yangjing committed Aug 7, 2024
1 parent f56d984 commit 73ae820
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 35 deletions.
17 changes: 5 additions & 12 deletions ultimates/ultimate-db/src/base/crud_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ use modql::field::{HasSeaFields, SeaField, SeaFields};
use modql::filter::{FilterGroups, ListOptions};
use sea_query::{Condition, Expr, PostgresQueryBuilder, Query};
use sea_query_binder::SqlxBinder;
use serde::Serialize;
use sqlx::postgres::PgRow;
use sqlx::FromRow;
use sqlx::Row;

use crate::base::{prep_fields_for_create, prep_fields_for_update, CommonIden, DbBmc};
use crate::{Error, ForPage, Page, PagePayload, Result};
use crate::{Error, Page, PagePayload, Pagination, Result};
use crate::{Id, ModelManager};

use super::check_number_of_affected;
Expand Down Expand Up @@ -227,25 +226,19 @@ where
Ok(count)
}

pub async fn page<MC, E, F>(mm: &ModelManager, for_page: &dyn ForPage) -> Result<PagePayload<E>>
pub async fn page<MC, E, F>(mm: &ModelManager, pagination: Pagination, filter: Option<F>) -> Result<PagePayload<E>>
where
MC: DbBmc,
F: Into<FilterGroups>,
E: for<'r> FromRow<'r, PgRow> + Unpin + Send,
E: HasSeaFields,
E: Serialize,
{
let filter: Option<FilterGroups> = if for_page.filters().is_empty() {
None
} else {
let filters = for_page.filters().to_vec();
Some(filters.into())
};
let filter: Option<FilterGroups> = filter.map(|f| f.into());

let total_size = count::<MC, _>(mm, filter.clone()).await?;
let records = list::<MC, E, _>(mm, filter, Some(for_page.get_list_options())).await?;
let records = list::<MC, E, _>(mm, filter, Some((&pagination).into())).await?;

Ok(PagePayload::new(Page::new(for_page.page(), total_size), records))
Ok(PagePayload::new(Page::new(&pagination, total_size), records))
}

pub async fn update_by_id<MC, E>(mm: &ModelManager, id: Id, data: E) -> Result<()>
Expand Down
8 changes: 8 additions & 0 deletions ultimates/ultimate-db/src/base/macro_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ macro_rules! generate_common_bmc_fns {
) -> ultimate_db::Result<i64> {
ultimate_db::base::count::<Self, _>(mm, Some(filter)).await
}

pub async fn page(
mm: &ultimate_db::ModelManager,
pagination: ultimate_db::Pagination,
filter: Vec<$filter>,
) -> ultimate_db::Result<ultimate_db::PagePayload<$entity>> {
ultimate_db::base::page::<Self, _, _>(mm, pagination, Some(filter)).await
}
)?

$(
Expand Down
3 changes: 2 additions & 1 deletion ultimates/ultimate-db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ pub mod base;
mod error;
mod id;
mod model_manager;
pub mod modql_utils;
mod modql_utils;
mod page;
pub mod store;

pub use error::{Error, Result};
pub use id::*;
pub use model_manager::*;
pub use modql_utils::*;
pub use page::*;

#[derive(Clone)]
Expand Down
22 changes: 19 additions & 3 deletions ultimates/ultimate-db/src/modql_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,25 @@ pub fn time_to_sea_value(json_value: serde_json::Value) -> modql::filter::SeaRes
}

#[cfg(feature = "utoipa")]
pub fn op_vals_obj_schema() -> utoipa::openapi::Object {
utoipa::openapi:: ObjectBuilder::new()
pub fn op_vals_integer_schema() -> utoipa::openapi::Object {
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::SchemaType::Object)
.description(Some("支持的详细操作条件见:https://github.com/jeremychone/rust-modql?tab=readme-ov-file#opvaltype-conditional-operators"))
.description(Some("opvalfloat64"))
.build()
}

#[cfg(feature = "utoipa")]
pub fn op_vals_string_schema() -> utoipa::openapi::Object {
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::SchemaType::String)
.description(Some("https://github.com/jeremychone/rust-modql?tab=readme-ov-file#opvalstring-operators"))
.build()
}

#[cfg(feature = "utoipa")]
pub fn op_vals_bool_schema() -> utoipa::openapi::Object {
utoipa::openapi::ObjectBuilder::new()
.schema_type(utoipa::openapi::SchemaType::Boolean)
.description(Some("https://github.com/jeremychone/rust-modql?tab=readme-ov-file#opvalbool-operators"))
.build()
}
23 changes: 4 additions & 19 deletions ultimates/ultimate-db/src/page.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,13 @@
use modql::filter::{FilterNode, ListOptions, OrderBy, OrderBys};
use modql::filter::{ListOptions, OrderBy, OrderBys};
use serde::{Deserialize, Serialize};

pub trait ForPage {
fn page(&self) -> &Pagination;

fn filters(&self) -> &[FilterNode];

fn get_list_options(&self) -> ListOptions {
self.page().into()
}
}

pub struct PagePayload<T>
where
T: Serialize,
{
#[derive(Serialize)]
pub struct PagePayload<T> {
pub page: Page,
pub records: Vec<T>,
}

impl<T> PagePayload<T>
where
T: Serialize,
{
impl<T> PagePayload<T> {
pub fn new(page: Page, records: Vec<T>) -> Self {
Self { page, records }
}
Expand Down

0 comments on commit 73ae820

Please sign in to comment.