Skip to content

Commit

Permalink
Merge pull request #6 from ashleygeorgeclarke/matrix-dimensions
Browse files Browse the repository at this point in the history
added matrix dimension read, delete
  • Loading branch information
tarmo-randma authored May 13, 2024
2 parents bc534cc + 4b59975 commit 49fe596
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/pim/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

// A Client manages communication with the PIM API.
Expand All @@ -35,6 +36,7 @@ type Client struct {
Categories *Categories
Families *Families
MatrixProducts *MatrixProducts
MatrixDimensions *MatrixDimensions
ExtraFields *ExtraFields
Suppliers *Suppliers
ProductUnits *ProductUnits
Expand Down Expand Up @@ -65,6 +67,7 @@ func NewClient(baseURL *url.URL, httpCli *http.Client) *Client {
c.Categories = (*Categories)(&c.common)
c.Families = (*Families)(&c.common)
c.MatrixProducts = (*MatrixProducts)(&c.common)
c.MatrixDimensions = (*MatrixDimensions)(&c.common)
c.Suppliers = (*Suppliers)(&c.common)
c.ProductUnits = (*ProductUnits)(&c.common)
return c
Expand All @@ -90,6 +93,8 @@ func NewAPIClient(baseURL *url.URL, httpCli *http.Client, userAgent string) *Cli
c.Brands = (*Brands)(&c.common)
c.Categories = (*Categories)(&c.common)
c.Families = (*Families)(&c.common)
c.MatrixProducts = (*MatrixProducts)(&c.common)
c.MatrixDimensions = (*MatrixDimensions)(&c.common)
c.ExtraFields = (*ExtraFields)(&c.common)
c.Suppliers = (*Suppliers)(&c.common)
c.ProductUnits = (*ProductUnits)(&c.common)
Expand Down
48 changes: 48 additions & 0 deletions pkg/pim/matrixDimension.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pim

import (
"context"
"fmt"
"net/http"
)

type (
MatrixDimensions service
MatrixDimension struct {
DimensionID int64 `json:"dimension_id"`
DimensionName map[string]string `json:"dimension_name,omitempty"`
DimensionOrder int `json:"dimension_order"`
Status string `json:"status"`
AddedByChangedBy
}
)

func (s *MatrixDimensions) Read(ctx context.Context, opts *ListOptions) (*[]MatrixDimension, *http.Response, error) {
urlStr := "matrix/dimension"
u, err := addOptions(urlStr, opts)
if err != nil {
return nil, nil, err
}

req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}

dataResp := new([]MatrixDimension)
resp, err := s.client.Do(ctx, req, dataResp)
return dataResp, resp, err
}

func (s *MatrixDimensions) Delete(ctx context.Context, dimensionID int) (*IDResponse, *http.Response, error) {
u := fmt.Sprintf("matrix/dimension/%d", dimensionID)

req, err := s.client.NewRequest(http.MethodDelete, u, nil)
if err != nil {
return nil, nil, err
}

id := new(IDResponse)
resp, err := s.client.Do(ctx, req, id)
return id, resp, err
}
41 changes: 41 additions & 0 deletions pkg/pim/matrixDimension_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pim

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestMatrixDimension_Read(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/matrix/dimension", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
_, err := fmt.Fprint(w, `
[
{
"dimension_order":1
},
{
"dimension_order":2
}
]
`)
assert.NoError(t, err)
})

opts := NewListOptions(nil, nil, nil, false)
result, _, err := client.MatrixDimensions.Read(context.Background(), opts)
if err != nil {
t.Error(err)
}

assert.Equal(t, 2, len(*result))
res := *result
assert.Equal(t, 1, res[0].DimensionOrder)
assert.Equal(t, 2, res[1].DimensionOrder)
}

0 comments on commit 49fe596

Please sign in to comment.