Skip to main content

server/command/
template.rs

1//! Template rows as the template forms, natives and pages edit them.
2
3use sqlx::types::Uuid;
4use supp_macro::command;
5
6use super::{CmdError, CmdResult};
7use crate::error::ServerError;
8use crate::template_state::STALE_TEMPLATE;
9use crate::user::User;
10
11command! {
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}
24
25command! {
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            other => other.map(|()| Some(CmdResult::Uuid(template_id))).map_err(Into::into),
44        }
45    }
46}
47
48command! {
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}