1
//! Tag rows as the tag forms and natives edit them, over the `User` tag API.
2

            
3
use sqlx::types::Uuid;
4
use supp_macro::command;
5

            
6
use super::{CmdError, CmdResult, FinanceEntity};
7
use crate::error::ServerError;
8
use crate::tag_state::STALE_TAG;
9
use crate::user::User;
10

            
11
command! {
12
    CreateTag {
13
        #[required]
14
        user_id: Uuid,
15
        #[required]
16
        name: String,
17
        #[required]
18
        value: String,
19
        #[optional]
20
        description: String,
21
    } => {
22
        let id = User { id: user_id }
23
            .create_tag(name, value, description)
24
            .await?;
25
        Ok(Some(CmdResult::Uuid(id)))
26
    }
27
28650
}
28

            
29
command! {
30
    EditTag {
31
        #[required]
32
        user_id: Uuid,
33
        #[required]
34
        tag_id: Uuid,
35
        #[required]
36
        name: String,
37
        #[required]
38
        value: String,
39
        #[optional]
40
        description: String,
41
        #[required]
42
        revision: String,
43
    } => {
44
        let updated = User { id: user_id }
45
            .update_tag(tag_id, &revision, name, value, description)
46
            .await;
47
        match updated {
48
            Err(ServerError::StaleTag) => Err(CmdError::Args(STALE_TAG.to_string())),
49
645
            other => other.map(|()| Some(CmdResult::Uuid(tag_id))).map_err(Into::into),
50
        }
51
    }
52
17171
}
53

            
54
command! {
55
    DeleteTag {
56
        #[required]
57
        user_id: Uuid,
58
        #[required]
59
        tag_id: Uuid,
60
    } => {
61
        match (User { id: user_id }).delete_tag(tag_id).await? {
62
            true => Ok(Some(CmdResult::Bool(true))),
63
            false => Err(CmdError::NotFound(format!("no tag {tag_id}"))),
64
        }
65
    }
66
5831
}
67

            
68
command! {
69
    ListTags {
70
        #[required]
71
        user_id: Uuid,
72
    } => {
73
        let tags = User { id: user_id }.list_tags().await?;
74
        Ok(Some(CmdResult::Entities(
75
            tags.into_iter().map(FinanceEntity::Tag).collect(),
76
        )))
77
    }
78
5653
}