Skip to content

Commit e7b9969

Browse files
add endpoint for list by tag
1 parent bda7a4c commit e7b9969

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/handlers/http/modal/server.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,13 @@ impl Server {
307307
.authorize(Action::ListDashboard),
308308
),
309309
)
310+
.service(
311+
web::resource("/list_by_tag/{tag}").route(
312+
web::get()
313+
.to(dashboards::list_dashboards_by_tag)
314+
.authorize(Action::ListDashboard),
315+
),
316+
)
310317
.service(
311318
web::scope("/{dashboard_id}")
312319
.service(

src/handlers/http/users/dashboards.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,21 @@ pub async fn list_tags() -> Result<impl Responder, DashboardError> {
204204
Ok((web::Json(tags), StatusCode::OK))
205205
}
206206

207+
pub async fn list_dashboards_by_tag(tag: Path<String>) -> Result<impl Responder, DashboardError> {
208+
let tag = tag.into_inner();
209+
if tag.is_empty() {
210+
return Err(DashboardError::Metadata("Tag cannot be empty"));
211+
}
212+
213+
let dashboards = DASHBOARDS.list_dashboards_by_tag(&tag).await;
214+
let dashboard_summaries = dashboards
215+
.iter()
216+
.map(|dashboard| dashboard.to_summary())
217+
.collect::<Vec<_>>();
218+
219+
Ok((web::Json(dashboard_summaries), StatusCode::OK))
220+
}
221+
207222
#[derive(Debug, thiserror::Error)]
208223
pub enum DashboardError {
209224
#[error("Failed to connect to storage: {0}")]

src/users/dashboards.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,21 @@ impl Dashboards {
345345
tags
346346
}
347347

348+
/// List dashboards by tag
349+
/// This function returns a list of dashboards that have the specified tag
350+
pub async fn list_dashboards_by_tag(&self, tag: &str) -> Vec<Dashboard> {
351+
let dashboards = self.0.read().await;
352+
dashboards
353+
.iter()
354+
.filter(|d| {
355+
d.tags
356+
.as_ref()
357+
.map_or(false, |tags| tags.contains(&tag.to_string()))
358+
})
359+
.cloned()
360+
.collect()
361+
}
362+
348363
/// Ensure the user is the owner of the dashboard
349364
/// This function is called when updating or deleting a dashboard
350365
/// check if the user is the owner of the dashboard

0 commit comments

Comments
 (0)