feat(issues): add label usecase update
zhenyi committed
Added lib/issues/features/label/usecase/update.rs lib/issues/features/label/usecase/update.rs +57 −0 View lib/issues/features/label/usecase/update.rs
@@ -0,0 +1,57 @@
1use db::conn::sea_orm::*;
2use time::OffsetDateTime;
3use uuid::Uuid;
4
5use crate::error::{IssueError, IssueResult};
6use crate::features::IssueService;
7
8use super::super::types::{LabelView, UpdateLabelOption};
9use super::helpers::{load_label, require_authenticated};
10
11pub async fn update_label(
12 svc: &IssueService,
13 actor: Uuid,
14 channel_id: Uuid,
15 label_id: Uuid,
16 input: UpdateLabelOption,
17) -> IssueResult<LabelView> {
18 require_authenticated(actor)?;
19 let cur = load_label(svc, channel_id, label_id).await?;
20
21 if let Some(ref n) = input.name {
22 let trimmed = n.trim().to_string();
23 if trimmed.is_empty() || trimmed.len() > svc.config.max_label_name_len {
24 return Err(IssueError::BadRequest("invalid label name".into()));
25 }
26 }
27 if let Some(ref c) = input.color
28 && c.len() > svc.config.max_label_color_len
29 {
30 return Err(IssueError::BadRequest("invalid label color".into()));
31 }
32
33 let db = svc.db.writer();
34 let mut active: db::issue::issue_label::ActiveModel = cur.into();
35 if let Some(n) = input.name {
36 active.name = Set(n.trim().to_string());
37 }
38 if let Some(c) = input.color {
39 active.color = Set(c);
40 }
41 if let Some(e) = input.exclusive {
42 active.exclusive = Set(e);
43 }
44 if let Some(o) = input.exclusive_order {
45 active.exclusive_order = Set(o);
46 }
47 if let Some(opt) = input.description {
48 active.description = Set(opt);
49 }
50 if let Some(a) = input.is_archived {
51 active.is_archived = Set(a);
52 }
53 active.updated_at = Set(Some(OffsetDateTime::now_utc()));
54
55 let saved = active.update(&db).await?;
56 Ok(LabelView::from(saved))
57}
No newline at end of file