feat(issues): add label usecase list
zhenyi committed
Added lib/issues/features/label/usecase/list.rs lib/issues/features/label/usecase/list.rs +44 −0 View lib/issues/features/label/usecase/list.rs
@@ -0,0 +1,44 @@
1use db::conn::sea_orm::*;
2use db::issue::issue_label as label_entity;
3use uuid::Uuid;
4
5use crate::error::{IssueError, IssueResult};
6use crate::features::IssueService;
7
8use super::super::types::{LabelPage, LabelView, ListLabelQuery};
9use super::helpers::require_authenticated;
10
11pub async fn list_labels(
12 svc: &IssueService,
13 actor: Uuid,
14 channel_id: Uuid,
15 q: ListLabelQuery,
16) -> IssueResult<LabelPage> {
17 require_authenticated(actor)?;
18
19 let limit = q.limit.unwrap_or(svc.config.default_pagination);
20 let offset = q.offset.unwrap_or(0);
21 if limit == 0 || limit > svc.config.max_pagination {
22 return Err(IssueError::BadRequest(format!(
23 "limit must be in 1..={}",
24 svc.config.max_pagination
25 )));
26 }
27
28 let db = svc.db.reader();
29 let finder = label_entity::Entity::find()
30 .filter(label_entity::COLUMN.channel_id.eq(channel_id))
31 .apply_if(q.q, |f, s| f.filter(label_entity::COLUMN.name.contains(&s)))
32 .apply_if(q.is_archived, |f, arch| f.filter(label_entity::COLUMN.is_archived.eq(arch)));
33
34 let total = finder.clone().count(&db).await? as i64;
35 let rows = finder
36 .order_by_asc(label_entity::COLUMN.name)
37 .limit(limit)
38 .offset(offset)
39 .all(&db)
40 .await?;
41
42 let items = rows.into_iter().map(LabelView::from).collect();
43 Ok(LabelPage { items, total })
44}
No newline at end of file