Documentation ¶
Index ¶
- type Client
- func (c *Client) Close() error
- func (c *Client) Create(ctx context.Context, coupon *Coupon) (string, error)
- func (c *Client) GetAll(ctx context.Context) (string, error)
- func (c *Client) GetOne(ctx context.Context, cID string) (string, error)
- func (c *Client) Redeem(ctx context.Context, cID string) error
- type ClientOpts
- type Coupon
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func NewClient ¶
func NewClient(ctx context.Context, opts *ClientOpts) (*Client, error)
NewClient creates a client with Firestore integration to support storage of coupons.
Example ¶
ctx := context.Background() client, err := NewClient(ctx, &ClientOpts{ ProjectID: "project-id", }) if err != nil { // TODO: Handle error } defer client.Close() // Close client when done _ = client // TODO: Use client
Output:
func (*Client) Create ¶
Create creates a new coupon in Firestore and returns ID.
Example ¶
ctx := context.Background() client, err := NewClient(ctx, &ClientOpts{ ProjectID: "project-id", }) if err != nil { // TODO: Handle error } defer client.Close() // Close client when done // Create coupon struct and specifiy name, description, and maxAllowed values coupon, err := NewCoupon("coupon_name", "coupon_desc", 2) // Persist coupon in Firestore collection couponId, err := client.Create(ctx, &coupon) _ = couponId // TODO: Use coupon ID later
Output:
func (*Client) GetAll ¶
GetAll returns all coupons available in Firestore.
Example ¶
ctx := context.Background() client, err := NewClient(ctx, &ClientOpts{ ProjectID: "project-id", }) if err != nil { // TODO: Handle error } defer client.Close() // Close client when done // Persist coupon in Firestore collection json, err := client.GetAll(ctx) _ = json // [{"created_at":"2021-04-21T13:44:04.447064Z","description":"str2","id":"2uIvxCZpEA","max_allowed":200,"purchases":[],"title":"str"},{"created_at":"2021-04-21T15:04:03.414614Z","description":"str2qdq","id":"B9qqp2Dqv7","max_allowed":25,"purchases":[],"title":"strw3"},{"created_at":"2021-04-22T09:01:31.670615Z","description":"desc","id":"VSciUPTPGI","max_allowed":2,"purchases":["2021-04-22T09:01:57.613793Z","2021-04-22T09:02:07.323676Z"],"title":"test_coupon"}]
Output:
func (*Client) GetOne ¶
GetOne returns coupon from Firestore in JSON format.
Example ¶
ctx := context.Background() client, err := NewClient(ctx, &ClientOpts{ ProjectID: "project-id", }) if err != nil { // TODO: Handle error } defer client.Close() // Close client when done // Persist coupon in Firestore collection json, err := client.GetOne(ctx, "coupon-id") _ = json // {"created_at":"2021-04-21T13:44:04.447064Z","description":"str2","id":"2uIvxCZpEA","max_allowed":200,"purchases":[],"title":"str"}
Output:
func (*Client) Redeem ¶
Redeem redeems given coupon. If max number of redeems reached - throws error.
Example ¶
ctx := context.Background() client, err := NewClient(ctx, &ClientOpts{ ProjectID: "project-id", }) if err != nil { // TODO: Handle error } defer client.Close() // Close client when done // Redeem coupon and add new entry to "purchases" path. // If redeem limit reached - throw error. err = client.Redeem(ctx, "VSciUPTPGI") if err != nil { // TODO: handle error }
Output:
type ClientOpts ¶
type ClientOpts struct {
ProjectID string
}
Click to show internal directories.
Click to hide internal directories.