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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use serde_with::skip_serializing_none;

use serde::{Deserialize, Serialize};

use crate::product_attributes::{AttributeSortOrder, AttributeType};

#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeCreate {
    name: String,
    slug: Option<String>,
    #[serde(rename = "type")]
    attribute_type: Option<AttributeType>,
    order_by: Option<AttributeSortOrder>,
    has_archives: Option<bool>,
}
#[derive(Default)]
pub struct AttributeCreateBuilder<N> {
    name: N,
    slug: Option<String>,
    attribute_type: Option<AttributeType>,
    order_by: Option<AttributeSortOrder>,
    has_archives: Option<bool>,
}
#[derive(Default)]
pub struct WithName(String);
#[derive(Default)]
pub struct NoName;
impl<N> AttributeCreateBuilder<N> {
    /// Attribute name.
    pub fn name(self, name: impl Into<String>) -> AttributeCreateBuilder<WithName> {
        AttributeCreateBuilder {
            name: WithName(name.into()),
            slug: self.slug,
            attribute_type: self.attribute_type,
            order_by: self.order_by,
            has_archives: self.has_archives,
        }
    }
    /// 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
    }
    /// Type of attribute. By default only select is supported.
    pub fn attribute_type(mut self, attribute_type: AttributeType) -> Self {
        let _ = self.attribute_type.insert(attribute_type);
        self
    }
    /// Default sort order. Options: menu_order, name, name_num and id. Default is menu_order.
    pub fn order_by(mut self, order_by: AttributeSortOrder) -> Self {
        let _ = self.order_by.insert(order_by);
        self
    }
    /// Enable/Disable attribute archives. Default is false.
    pub fn enable_archives(mut self) -> Self {
        let _ = self.has_archives.insert(true);
        self
    }
}
impl AttributeCreateBuilder<WithName> {
    pub fn build(self) -> AttributeCreate {
        AttributeCreate {
            name: self.name.0,
            slug: self.slug,
            attribute_type: self.attribute_type,
            order_by: self.order_by,
            has_archives: self.has_archives,
        }
    }
}
#[skip_serializing_none]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttributeUpdate {
    id: Option<i32>,
    name: Option<String>,
    slug: Option<String>,
    #[serde(rename = "type")]
    attribute_type: Option<AttributeType>,
    order_by: Option<AttributeSortOrder>,
    has_archives: Option<bool>,
}
#[derive(Default)]
pub struct AttributeUpdateBuilder {
    id: Option<i32>,
    name: Option<String>,
    slug: Option<String>,
    attribute_type: Option<AttributeType>,
    order_by: Option<AttributeSortOrder>,
    has_archives: Option<bool>,
}
impl AttributeUpdateBuilder {
    /// Unique identifier for the resource.
    pub fn id(&mut self, id: i32) -> &mut Self {
        let _ = self.id.insert(id);
        self
    }
    /// Attribute 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>) -> &mut Self {
        let _ = self.slug.insert(slug.into());
        self
    }
    /// Type of attribute. By default only select is supported.
    pub fn attribute_type(&mut self, attribute_type: AttributeType) -> &mut Self {
        let _ = self.attribute_type.insert(attribute_type);
        self
    }
    /// Default sort order. Options: menu_order, name, name_num and id. Default is menu_order.
    pub fn order_by(&mut self, order_by: AttributeSortOrder) -> &mut Self {
        let _ = self.order_by.insert(order_by);
        self
    }
    /// Enable attribute archives.
    pub fn enable_archives(&mut self) -> &mut Self {
        let _ = self.has_archives.insert(true);
        self
    }
    /// Disable attribute archives.
    pub fn disable_archives(&mut self) -> &mut Self {
        let _ = self.has_archives.insert(false);
        self
    }
    pub fn build(&self) -> AttributeUpdate {
        AttributeUpdate {
            id: self.id,
            name: self.name.to_owned(),
            slug: self.slug.to_owned(),
            attribute_type: self.attribute_type.to_owned(),
            order_by: self.order_by.to_owned(),
            has_archives: self.has_archives,
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        product_attributes::{Attribute, AttributeSortOrder},
        ApiClient, BatchObject, Entity,
    };

    #[tokio::test]
    async fn test_list_all_attributes() {
        let client = ApiClient::from_env().unwrap();
        let result = client
            .list_all::<Attribute>(Entity::ProductAttribute)
            .await
            .unwrap();
        assert!(!result.is_empty());
    }
    #[tokio::test]
    async fn test_retrieve_attribute() {
        let client = ApiClient::from_env().unwrap();
        let result = client
            .retrieve::<Attribute>(Entity::ProductAttribute, 4)
            .await
            .unwrap();
        assert_eq!("Цвет", result.name);
    }
    #[tokio::test]
    async fn test_create_attribute() {
        let client = ApiClient::from_env().unwrap();
        let create = Attribute::create()
            .name("test attribute")
            .enable_archives()
            .build();
        let created = client
            .create::<Attribute>(Entity::ProductAttribute, create)
            .await
            .unwrap();
        assert_eq!(created.name, "test attribute");
        let _deleted: Attribute = client
            .delete(Entity::ProductAttribute, created.id)
            .await
            .unwrap();
    }
    #[tokio::test]
    async fn test_update_attribute() {
        let client = ApiClient::from_env().unwrap();
        let update = Attribute::update()
            .order_by(AttributeSortOrder::Name)
            .build();
        let updated = client
            .update::<Attribute>(Entity::ProductAttribute, 2, update)
            .await
            .unwrap();
        assert_eq!(updated.order_by, AttributeSortOrder::Name)
    }
    #[tokio::test]
    async fn test_batch_update_attribute() {
        let client = ApiClient::from_env().unwrap();
        let update = Attribute::update()
            .id(2)
            .order_by(AttributeSortOrder::Name)
            .build();
        let batch = BatchObject::builder().add_update(update).build();
        let updated: BatchObject<Attribute> = client
            .batch_update(Entity::ProductAttribute, batch)
            .await
            .unwrap();
        assert!(updated.update.is_some());
    }
}