Skip to content

Commit

Permalink
feat: add BuyProduct parameter to BuyProduct() API (#139)
Browse files Browse the repository at this point in the history
* fix: fix buy-product api totally unusable

* fix: rename bytes to resp
  • Loading branch information
dacongda authored Oct 15, 2024
1 parent 0c7a4be commit fbe9484
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
17 changes: 11 additions & 6 deletions casdoorsdk/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,28 @@ func (c *Client) DeleteProduct(product *Product) (bool, error) {
return affected, err
}

func (c *Client) BuyProduct(name string, providerName string) (*Product, error) {
func (c *Client) BuyProduct(name string, providerName string, userName string) (*Product, error) {
queryMap := map[string]string{
"id": fmt.Sprintf("%s/%s", c.OrganizationName, name),
"providerName": providerName,
"userName": userName,
}

url := c.GetUrl("buy-product", queryMap)
resp, err := c.DoPost("buy-product", queryMap, []byte(""), false, false)
if err != nil {
return nil, err
}

bytes, err := c.DoGetBytes(url)
productJson, err := json.Marshal(resp.Data)
if err != nil {
return nil, err
}

var product *Product
err = json.Unmarshal(bytes, &product)
var product Product
err = json.Unmarshal(productJson, &product)
if err != nil {
return nil, err
}
return product, nil

return &product, nil
}
4 changes: 2 additions & 2 deletions casdoorsdk/product_global.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ func DeleteProduct(product *Product) (bool, error) {
return globalClient.DeleteProduct(product)
}

func BuyProduct(name string, providerName string) (*Product, error) {
return globalClient.BuyProduct(name, providerName)
func BuyProduct(name string, providerName string, userName string) (*Product, error) {
return globalClient.BuyProduct(name, providerName, userName)
}
15 changes: 12 additions & 3 deletions casdoorsdk/product_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ func TestProduct(t *testing.T) {
Description: "Casdoor Website",
Tag: "auto_created_product_for_plan",

Quantity: 999,
Sold: 0,
State: "Published",
Quantity: 999,
Sold: 0,
State: "Published",
Providers: []string{"provider_payment_dummy"},
}
_, err := AddProduct(product)
if err != nil {
Expand Down Expand Up @@ -85,6 +86,14 @@ func TestProduct(t *testing.T) {
t.Fatalf("Failed to update object, description mismatch: %s != %s", updatedProduct.Description, updatedDescription)
}

boughtProduct, err := BuyProduct(name, "provider_payment_dummy", "admin")
if err != nil {
t.Fatalf("Failed to buy product: %v", err)
}
if boughtProduct == nil {
t.Fatalf("Failed to buy product: nil response")
}

// Delete the object
_, err = DeleteProduct(product)
if err != nil {
Expand Down

0 comments on commit fbe9484

Please sign in to comment.