Passed
Push — main ( b6acc3...d081c0 )
by Acho
01:28
created

lemonsqueezy.*DiscountsService.Create   A

Complexity

Conditions 3

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 21
nop 2
dl 0
loc 32
rs 9.376
c 0
b 0
f 0
1
package lemonsqueezy
2
3
import (
4
	"context"
5
	"encoding/json"
6
	"net/http"
7
)
8
9
// DiscountsService is the API client for the `/v1/discounts` endpoint
10
type DiscountsService service
11
12
// Create a discount.
13
//
14
// https://docs.lemonsqueezy.com/api/discounts#create-a-discount
15
func (service *DiscountsService) Create(ctx context.Context, params *DiscountCreateParams) (*DiscountApiResponse, *Response, error) {
16
	payload := map[string]any{
17
		"data": map[string]any{
18
			"type": "discounts",
19
			"attributes": map[string]any{
20
				"name":        params.Name,
21
				"code":        params.Code,
22
				"amount":      params.Amount,
23
				"amount_type": params.AmountType,
24
			},
25
			"relationships": map[string]any{
26
				"store": map[string]any{
27
					"data": map[string]any{
28
						"type": "stores",
29
						"id":   params.StoreID,
30
					},
31
				},
32
			},
33
		},
34
	}
35
36
	response, err := service.client.do(ctx, http.MethodPost, "/v1/discounts/", payload)
37
	if err != nil {
38
		return nil, response, err
39
	}
40
41
	discount := new(DiscountApiResponse)
42
	if err = json.Unmarshal(*response.Body, discount); err != nil {
43
		return nil, response, err
44
	}
45
46
	return discount, response, nil
47
}
48
49
// Get the discount with the given ID.
50
//
51
// https://docs.lemonsqueezy.com/api/discounts#retrieve-a-discount
52
func (service *DiscountsService) Get(ctx context.Context, discountID string) (*DiscountApiResponse, *Response, error) {
53
	response, err := service.client.do(ctx, http.MethodGet, "/v1/discounts/"+discountID)
54
	if err != nil {
55
		return nil, response, err
56
	}
57
58
	discount := new(DiscountApiResponse)
59
	if err = json.Unmarshal(*response.Body, discount); err != nil {
60
		return nil, response, err
61
	}
62
63
	return discount, response, nil
64
}
65
66
// Delete a discount with the given ID.
67
//
68
// https://docs.lemonsqueezy.com/api/discounts#delete-a-discount
69
func (service *DiscountsService) Delete(ctx context.Context, discountID string) (*Response, error) {
70
	return service.client.do(ctx, http.MethodDelete, "/v1/discounts/"+discountID)
71
}
72
73
// List returns a paginated list of discounts.
74
//
75
// https://docs.lemonsqueezy.com/api/discounts#list-all-discounts
76
func (service *DiscountsService) List(ctx context.Context) (*DiscountsApiResponse, *Response, error) {
77
	response, err := service.client.do(ctx, http.MethodGet, "/v1/discounts")
78
	if err != nil {
79
		return nil, response, err
80
	}
81
82
	discounts := new(DiscountsApiResponse)
83
	if err = json.Unmarshal(*response.Body, discounts); err != nil {
84
		return nil, response, err
85
	}
86
87
	return discounts, response, nil
88
}
89