coupons

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

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) Close

func (c *Client) Close() error

Closes closes Firestore client.

func (*Client) Create

func (c *Client) Create(ctx context.Context, coupon *Coupon) (string, error)

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

func (c *Client) GetAll(ctx context.Context) (string, error)

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

func (c *Client) GetOne(ctx context.Context, cID string) (string, error)

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

func (c *Client) Redeem(ctx context.Context, cID string) error

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
}

type Coupon

type Coupon interface{}

func NewCoupon

func NewCoupon(title string, description string, maxAllowed int) (Coupon, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL