1
+ // Package categories provides functions for interacting with Magento categories.
1
2
package categories
2
3
3
4
import (
4
5
"encoding/json"
5
6
"errors"
6
- "fmt"
7
- "net/http"
8
7
"net/url"
8
+ "strconv"
9
9
10
+ "github.com/go-resty/resty/v2"
10
11
"github.com/web-seven/provider-magento/apis/category/v1alpha1"
11
12
12
13
magento "github.com/web-seven/provider-magento/internal/client"
13
14
)
14
15
16
+ // Client is a struct that embeds the magento.Client struct.
15
17
type Client struct {
16
18
magento.Client
17
19
}
18
20
19
- func GetCategoryByName ( c * magento. Client , categoryName string ) ( * v1alpha1. Category , error ) {
20
- path := fmt . Sprintf ( "%s /rest/all /V1/categories/list" , c . BaseURL )
21
+ // categoriesPath is the path for the Magento categories API.
22
+ const categoriesPath = " /rest/default /V1/categories/"
21
23
24
+ // GetCategoryByID retrieves a category by its ID.
25
+ func GetCategoryByID (c * magento.Client , id string ) (* v1alpha1.Category , error ) {
22
26
queryParams := map [string ]string {
23
- "searchCriteria[filterGroups][0][filters][0][field]" : "name " ,
24
- "searchCriteria[filterGroups][0][filters][0][value]" : categoryName ,
27
+ "searchCriteria[filterGroups][0][filters][0][field]" : "entity_id " ,
28
+ "searchCriteria[filterGroups][0][filters][0][value]" : id ,
25
29
}
26
-
27
30
query := url.Values {}
28
31
for key , value := range queryParams {
29
32
query .Add (key , value )
30
33
}
34
+ resp , err := c .Create ().R ().SetQueryParams (queryParams ).Get (categoriesPath + "list" )
31
35
32
- restyClient , err := c .CreateRestyClient ()
33
36
if err != nil {
34
37
return nil , err
35
38
}
36
39
37
- resp , err := restyClient .R ().
38
- SetHeader ("Authorization" , "Bearer " + c .AccessToken ).
39
- SetQueryParams (queryParams ).
40
- Get (path )
41
- if err != nil {
42
- return nil , err
43
- }
44
40
var searchResults struct {
45
41
Items []v1alpha1.CategoryParameters `json:"items"`
46
42
}
@@ -61,106 +57,57 @@ func GetCategoryByName(c *magento.Client, categoryName string) (*v1alpha1.Catego
61
57
}
62
58
}
63
59
64
- func CreateCategory ( c * magento. Client , category * v1alpha1. Category ) ( * v1alpha1. Category , error ) {
65
- path := c . BaseURL + "/rest/default/V1/categories"
60
+ // CreateCategory creates a new category.
61
+ func CreateCategory ( c * magento. Client , category * v1alpha1. Category ) ( * v1alpha1. CategoryObservation , * resty. Response , error ) {
66
62
requestBody := map [string ]interface {}{
67
63
"category" : category .Spec .ForProvider ,
68
64
}
69
- restyClient , err := c .CreateRestyClient ()
70
- if err != nil {
71
- return nil , err
72
- }
73
-
74
- resp , err := restyClient .R ().
75
- SetHeader ("Authorization" , "Bearer " + c .AccessToken ).
76
- SetHeader ("Content-Type" , "application/json" ).
77
- SetBody (requestBody ).
78
- Post (path )
79
65
66
+ resp , err := c .Create ().R ().SetHeader ("Content-Type" , "application/json" ).SetBody (requestBody ).Post (categoriesPath )
80
67
if err != nil {
81
- return nil , errors .New ("failed to create category: " + err .Error ())
82
- }
83
-
84
- if resp .StatusCode () != http .StatusOK {
85
- return nil , errors .New ("unexpected status code: " + resp .Status () + ": " + string (resp .Body ()))
68
+ return nil , nil , err
86
69
}
87
70
88
- var newCategory * v1alpha1.Category
89
- err = json .Unmarshal (resp .Body (), & newCategory )
71
+ var categoryObservation * v1alpha1.CategoryObservation
72
+ err = json .Unmarshal (resp .Body (), & categoryObservation )
90
73
if err != nil {
91
- return nil , errors . New ( "failed to parse response: " + err . Error ())
74
+ return nil , nil , err
92
75
}
93
76
94
- return newCategory , nil
77
+ return categoryObservation , resp , nil
95
78
}
96
79
97
- func UpdateCategoryByName (c * magento.Client , categoryName string , observed * v1alpha1.Category ) error {
98
- category , err := GetCategoryByName (c , categoryName )
99
- if err != nil {
100
- return err
101
- }
102
- path := fmt .Sprintf ("%s/rest/all/V1/categories/%d" , c .BaseURL , category .Spec .ForProvider .ID )
103
-
104
- restyClient , err := c .CreateRestyClient ()
105
- if err != nil {
106
- return err
107
- }
108
-
80
+ // UpdateCategoryByID updates a category by its ID.
81
+ func UpdateCategoryByID (c * magento.Client , id int , observed * v1alpha1.Category ) error {
109
82
requestBody := map [string ]interface {}{
110
83
"category" : observed .Spec .ForProvider ,
111
84
}
112
85
113
- resp , err := restyClient .R ().
114
- SetHeader ("Authorization" , "Bearer " + c .AccessToken ).
115
- SetHeader ("Content-Type" , "application/json" ).
116
- SetBody (requestBody ).
117
- Put (path )
86
+ _ , err := c .Create ().R ().SetBody (requestBody ).Put (categoriesPath + strconv .Itoa (id ))
118
87
if err != nil {
119
88
return err
120
89
}
121
90
122
- if resp .StatusCode () != http .StatusOK {
123
- return errors .New ("failed to update category" )
124
- }
125
-
126
91
return nil
127
92
}
128
93
129
- func DeleteCategoryByName (c * magento.Client , categoryName string ) error {
130
- category , err := GetCategoryByName (c , categoryName )
131
- if err != nil {
132
- return err
133
- }
134
-
135
- path := fmt .Sprintf ("%s/rest/all/V1/categories/%d" , c .BaseURL , category .Spec .ForProvider .ID )
136
-
137
- restyClient , err := c .CreateRestyClient ()
138
- if err != nil {
139
- return err
140
- }
141
-
142
- resp , err := restyClient .R ().
143
- SetHeader ("Authorization" , "Bearer " + c .AccessToken ).
144
- Delete (path )
145
- if err != nil {
146
- return err
147
- }
148
-
149
- if resp .StatusCode () != http .StatusOK {
150
- return errors .New ("failed to delete category" )
151
- }
152
-
153
- return nil
94
+ // DeleteCategoryByID deletes a category by its ID.
95
+ func DeleteCategoryByID (c * magento.Client , id int ) error {
96
+ _ , err := c .Create ().R ().Delete (categoriesPath + strconv .Itoa (id ))
97
+ return err
154
98
}
155
99
156
- func IsCategoryUpToDate (name string , observed * v1alpha1.Category , desired * v1alpha1.Category ) (bool , error ) {
100
+ // IsUpToDate checks if the observed category is up to date with the desired category.
101
+ func IsUpToDate (observed * v1alpha1.Category , desired * v1alpha1.Category ) (bool , error ) {
157
102
if observed == nil || desired == nil {
158
103
return false , errors .New ("observed or desired category is nil" )
159
104
}
160
105
161
106
if observed .Spec .ForProvider .Name != desired .Spec .ForProvider .Name ||
162
- observed .Spec .ForProvider .IsActive != desired .Spec .ForProvider .IsActive ||
163
- observed .Spec .ForProvider .IncludeInMenu != desired .Spec .ForProvider .IncludeInMenu {
107
+ observed .Spec .ForProvider .IncludeInMenu != desired .Spec .ForProvider .IncludeInMenu ||
108
+ observed .Spec .ForProvider .ParentID != desired .Spec .ForProvider .ParentID ||
109
+ observed .Spec .ForProvider .Position != desired .Spec .ForProvider .Position ||
110
+ observed .Spec .ForProvider .Level != desired .Spec .ForProvider .Level {
164
111
return false , nil
165
112
}
166
113
0 commit comments