1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeTermCreate {
    name: String,
    slug: Option<String>,
    description: Option<String>,
    menu_order: Option<i32>,
}
#[derive(Default)]
pub struct AttributeTermCreateBuilder<N> {
    name: N,
    slug: Option<String>,
    description: Option<String>,
    menu_order: Option<i32>,
}
#[derive(Default)]
pub struct WithName(String);
#[derive(Default)]
pub struct NoName;
impl<N> AttributeTermCreateBuilder<N> {
    /// Term name.
    pub fn name(self, name: impl Into<String>) -> AttributeTermCreateBuilder<WithName> {
        AttributeTermCreateBuilder {
            name: WithName(name.into()),
            slug: self.slug,
            description: self.description,
            menu_order: self.menu_order,
        }
    }
    /// An alphanumeric identifier for the resource unique to its type.
    pub fn slug(mut self, slug: impl Into<String>) -> Self {
        let _ = self.slug.insert(slug.into());
        self
    }
    /// HTML description of the resource.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// Menu order, used to custom sort the resource.
    pub fn menu_order(mut self, menu_order: i32) -> Self {
        let _ = self.menu_order.insert(menu_order);
        self
    }
}
impl AttributeTermCreateBuilder<WithName> {
    pub fn build(self) -> AttributeTermCreate {
        AttributeTermCreate {
            name: self.name.0,
            slug: self.slug,
            description: self.description,
            menu_order: self.menu_order,
        }
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeTermUpdate {
    id: Option<i32>,
    name: Option<String>,
    slug: Option<String>,
    description: Option<String>,
    menu_order: Option<i32>,
}
#[derive(Default)]
pub struct AttributeTermUpdateBuilder {
    id: Option<i32>,
    name: Option<String>,
    slug: Option<String>,
    description: Option<String>,
    menu_order: Option<i32>,
}
impl AttributeTermUpdateBuilder {
    /// Unique identifier for the resource.
    pub fn id(&mut self, id: i32) -> &mut Self {
        let _ = self.id.insert(id);
        self
    }
    /// Term name.
    pub fn name(&mut self, name: impl Into<String>) -> &mut Self {
        let _ = self.name.insert(name.into());
        self
    }
    /// An alphanumeric identifier for the resource unique to its type.
    pub fn slug(mut self, slug: impl Into<String>) -> Self {
        let _ = self.slug.insert(slug.into());
        self
    }
    /// HTML description of the resource.
    pub fn description(mut self, description: impl Into<String>) -> Self {
        let _ = self.description.insert(description.into());
        self
    }
    /// Menu order, used to custom sort the resource.
    pub fn menu_order(mut self, menu_order: i32) -> Self {
        let _ = self.menu_order.insert(menu_order);
        self
    }
    pub fn build(&self) -> AttributeTermUpdate {
        AttributeTermUpdate {
            id: self.id,
            name: self.name.to_owned(),
            slug: self.slug.to_owned(),
            description: self.description.to_owned(),
            menu_order: self.menu_order,
        }
    }
}
#[cfg(test)]
mod tests {
    use crate::{product_attribute_terms::AttributeTerm, ApiClient, Entity, SubEntity};

    const ATTRIBUTE_ID: i32 = 2;
    const ATTRIBUTE_TERM_ID: i32 = 240;
    #[tokio::test]
    async fn test_list_all_attribute_terms() {
        let client = ApiClient::from_env().unwrap();
        let terms = client
            .list_all_subentities::<AttributeTerm>(
                Entity::ProductAttribute,
                ATTRIBUTE_ID,
                SubEntity::AttributeTerm,
            )
            .await
            .unwrap();
        assert!(!terms.is_empty());
    }
    #[tokio::test]
    async fn test_retrieve_attribute_term() {
        let client = ApiClient::from_env().unwrap();
        let term: AttributeTerm = client
            .retrieve_subentity(
                Entity::ProductAttribute,
                ATTRIBUTE_ID,
                SubEntity::AttributeTerm,
                ATTRIBUTE_TERM_ID,
            )
            .await
            .unwrap();
        assert_eq!(term.id, ATTRIBUTE_TERM_ID);
    }
    #[tokio::test]
    async fn test_create_attribute_term() {
        let client = ApiClient::from_env().unwrap();
        let create = AttributeTerm::create().name("test term").build();
        let created: AttributeTerm = client
            .create_subentity(
                Entity::ProductAttribute,
                ATTRIBUTE_ID,
                SubEntity::AttributeTerm,
                create,
            )
            .await
            .unwrap();
        assert_eq!(created.name, "test term");
        let _deleted: AttributeTerm = client
            .delete_subentity(
                Entity::ProductAttribute,
                ATTRIBUTE_ID,
                SubEntity::AttributeTerm,
                created.id,
            )
            .await
            .unwrap();
    }
    #[tokio::test]
    async fn test_update_attribute_term() {
        let client = ApiClient::from_env().unwrap();
        let update = AttributeTerm::update().menu_order(0).build();
        let updated: AttributeTerm = client
            .update_subentity(
                Entity::ProductAttribute,
                ATTRIBUTE_ID,
                SubEntity::AttributeTerm,
                ATTRIBUTE_TERM_ID,
                update,
            )
            .await
            .unwrap();
        assert_eq!(updated.menu_order, 0);
    }
}