Add filtering options for upstream targets in get_upstreams
This commit is contained in:
@@ -55,7 +55,14 @@ pub async fn get_upstream_list(
|
||||
) -> AxumResult<Json<UpstreamListResponse>, ServiceError> {
|
||||
let upstream_service = &state.service.nginx.get_upstream_service();
|
||||
let upstreams = upstream_service
|
||||
.get_upstreams(Some(pagination.clone().into()), None)
|
||||
.get_upstreams(
|
||||
Some(pagination.clone().into()),
|
||||
Some(GetUpstreamOptions {
|
||||
include_targets: true,
|
||||
filter_by_enabled: false,
|
||||
}),
|
||||
None,
|
||||
)
|
||||
.await?;
|
||||
|
||||
//
|
||||
@@ -93,6 +100,7 @@ pub async fn get_upstream(
|
||||
upstream_id,
|
||||
Some(GetUpstreamOptions {
|
||||
include_targets: true,
|
||||
filter_by_enabled: false,
|
||||
}),
|
||||
None,
|
||||
)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use sea_orm::{
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, DatabaseTransaction, EntityTrait,
|
||||
ModelTrait, QueryFilter, QuerySelect, TransactionTrait,
|
||||
ActiveModelTrait, ColumnTrait, DatabaseConnection, DatabaseTransaction, EntityTrait, ExprTrait,
|
||||
ModelTrait, QueryFilter, QuerySelect, QueryTrait, TransactionTrait,
|
||||
};
|
||||
|
||||
use database::generated::entities::{upstream, upstream_target};
|
||||
@@ -38,6 +38,7 @@ pub trait UpstreamService: Send + Sync {
|
||||
async fn get_upstreams(
|
||||
&self,
|
||||
pagination: Option<PaginationFilter>,
|
||||
options: Option<GetUpstreamOptions>,
|
||||
tx: Option<&mut DatabaseTransaction>,
|
||||
) -> Result<Vec<UpstreamInfo>, ServiceError>;
|
||||
async fn update_upstream(
|
||||
@@ -93,6 +94,7 @@ pub struct UpstreamServiceImpl {
|
||||
#[derive(Default)]
|
||||
pub struct GetUpstreamOptions {
|
||||
pub include_targets: bool,
|
||||
pub filter_by_enabled: bool,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -168,6 +170,9 @@ impl UpstreamService for UpstreamServiceImpl {
|
||||
)))?;
|
||||
let targets = upstream_target::Entity::find()
|
||||
.filter(upstream_target::Column::UpstreamId.eq(upstream_id))
|
||||
.apply_if(Some(concrete_options.filter_by_enabled), |query, _v| {
|
||||
query.filter(upstream_target::Column::Enabled.eq(true))
|
||||
})
|
||||
.all(*conn)
|
||||
.await?;
|
||||
(up, targets)
|
||||
@@ -191,6 +196,7 @@ impl UpstreamService for UpstreamServiceImpl {
|
||||
async fn get_upstreams(
|
||||
&self,
|
||||
pagination: Option<PaginationFilter>,
|
||||
options: Option<GetUpstreamOptions>,
|
||||
tx: Option<&mut DatabaseTransaction>,
|
||||
) -> Result<Vec<UpstreamInfo>, ServiceError> {
|
||||
let r = with_conn!(&*self.connection, tx, conn, {
|
||||
@@ -201,7 +207,24 @@ impl UpstreamService for UpstreamServiceImpl {
|
||||
} else {
|
||||
find_query
|
||||
};
|
||||
find_query.all(*conn).await?
|
||||
let find_query = match options {
|
||||
Some(opts) => {
|
||||
if opts.include_targets && opts.filter_by_enabled {
|
||||
find_query.filter(
|
||||
upstream_target::Column::Enabled
|
||||
.eq(true)
|
||||
.or(upstream_target::Column::Id.is_null()),
|
||||
)
|
||||
} else {
|
||||
find_query
|
||||
}
|
||||
}
|
||||
_ => find_query,
|
||||
};
|
||||
find_query
|
||||
.find_with_related(upstream_target::Entity)
|
||||
.all(*conn)
|
||||
.await?
|
||||
});
|
||||
|
||||
Ok(r.into_iter().map(|m| m.into()).collect())
|
||||
@@ -375,7 +398,9 @@ impl UpstreamService for UpstreamServiceImpl {
|
||||
});
|
||||
let active_model = target.apply_to_model(current_model);
|
||||
|
||||
let r = active_model.update(&*self.connection).await?;
|
||||
let r = with_conn!(&*self.connection, tx, conn, {
|
||||
active_model.update(*conn).await?
|
||||
});
|
||||
Ok(r.into())
|
||||
}
|
||||
|
||||
@@ -505,6 +530,7 @@ mod tests {
|
||||
up_id,
|
||||
Some(GetUpstreamOptions {
|
||||
include_targets: true,
|
||||
filter_by_enabled: false,
|
||||
}),
|
||||
None,
|
||||
)
|
||||
@@ -559,7 +585,7 @@ mod tests {
|
||||
|
||||
let svc = UpstreamServiceImpl::new(Arc::new(db));
|
||||
|
||||
let res = svc.get_upstreams(None, None).await;
|
||||
let res = svc.get_upstreams(None, None, None).await;
|
||||
assert!(res.is_ok());
|
||||
let list = res.expect("Failed to get upstreams");
|
||||
assert_eq!(list.len(), 2);
|
||||
|
||||
Reference in New Issue
Block a user