1
//! Template rows as the template forms, natives and pages edit them.
2

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

            
6
use super::{CmdError, CmdResult};
7
use crate::error::ServerError;
8
use crate::template_state::STALE_TEMPLATE;
9
use crate::user::User;
10

            
11
command! {
12
    CreateTemplate {
13
        #[required]
14
        user_id: Uuid,
15
        #[required]
16
        source: String,
17
        #[optional]
18
        name: String,
19
    } => {
20
        let id = User { id: user_id }.create_template(source, name).await?;
21
        Ok(Some(CmdResult::Uuid(id)))
22
    }
23
15478
}
24

            
25
command! {
26
    EditTemplate {
27
        #[required]
28
        user_id: Uuid,
29
        #[required]
30
        template_id: Uuid,
31
        #[required]
32
        source: String,
33
        #[optional]
34
        name: String,
35
        #[required]
36
        revision: String,
37
    } => {
38
        let updated = User { id: user_id }
39
            .update_template(template_id, &revision, name, &source)
40
            .await;
41
        match updated {
42
            Err(ServerError::StaleTemplate) => Err(CmdError::Args(STALE_TEMPLATE.to_string())),
43
808
            other => other.map(|()| Some(CmdResult::Uuid(template_id))).map_err(Into::into),
44
        }
45
    }
46
16128
}
47

            
48
command! {
49
    DeleteTemplate {
50
        #[required]
51
        user_id: Uuid,
52
        #[required]
53
        template_id: Uuid,
54
    } => {
55
        match (User { id: user_id }).delete_template(template_id).await? {
56
            true => Ok(Some(CmdResult::Bool(true))),
57
            false => Err(CmdError::NotFound(format!("no template {template_id}"))),
58
        }
59
    }
60
3894
}