Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BuyProduct parameter to BuyProduct() API #139

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
hsluoyz marked this conversation as resolved.
Show resolved Hide resolved
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
Loading