ducky/subscriptions

Paddy 2015-09-30 Parent:1ff031bebf9e Child:aab6ba5ae392

14:fb2c0e498e37 Browse Files

Update with comments for all exported functions. We now have golint-approved comments for all the exported functions in the subscriptions package. Next challenge: all the sub-packages!

subscription_memstore.go subscription_postgres.go

     1.1 --- a/subscription_memstore.go	Sun Sep 27 21:20:46 2015 -0700
     1.2 +++ b/subscription_memstore.go	Wed Sep 30 01:03:39 2015 -0700
     1.3 @@ -13,6 +13,11 @@
     1.4  	return false
     1.5  }
     1.6  
     1.7 +// CreateSubscription stores the passed Subscription in the Memstore. If
     1.8 +// a Subscription sharing the same UserID already exists, an
     1.9 +// ErrSubscriptionAlreadyExists error will be returned. If a Subscription
    1.10 +// sharing the same StripeSubscription already exists, an
    1.11 +// ErrStripeSubscriptionAlreadyExists error will be returned.
    1.12  func (m *Memstore) CreateSubscription(sub Subscription) error {
    1.13  	m.subscriptionLock.Lock()
    1.14  	defer m.subscriptionLock.Unlock()
    1.15 @@ -27,6 +32,13 @@
    1.16  	return nil
    1.17  }
    1.18  
    1.19 +// UpdateSubscription applies the SubscriptionChange passed to the Subscription
    1.20 +// stored in the Memstore associated with the passed ID. If change is empty,
    1.21 +// an ErrSubscriptionChangeEmpty error is returned. If no Subscription is found
    1.22 +// in the Memstore with the passed ID, an ErrSubscriptionNotFound error is returned.
    1.23 +// If change is updating the StripeSubscription, and a Subscription in the Memstore
    1.24 +// already has that value set for StripeSubscription, an
    1.25 +// ErrStripeSubscriptionAlreadyExists error is returned.
    1.26  func (m *Memstore) UpdateSubscription(id uuid.ID, change SubscriptionChange) error {
    1.27  	if change.IsEmpty() {
    1.28  		return ErrSubscriptionChangeEmpty
    1.29 @@ -49,6 +61,9 @@
    1.30  	return nil
    1.31  }
    1.32  
    1.33 +// DeleteSubscription removes the Subscription stored in the Memstore associated
    1.34 +// with the passed ID from the Memstore. If no Subscription is found
    1.35 +// in the Memstore with the passed ID, an ErrSubscriptionNotFound error is returned.
    1.36  func (m *Memstore) DeleteSubscription(id uuid.ID) error {
    1.37  	m.subscriptionLock.Lock()
    1.38  	defer m.subscriptionLock.Unlock()
    1.39 @@ -61,6 +76,11 @@
    1.40  	return nil
    1.41  }
    1.42  
    1.43 +// GetSubscriptions retrieves the Subscriptions stored in the Memstore associated
    1.44 +// with the passed IDs. If no IDs are passed, an ErrNoSubscriptionID error is
    1.45 +// returned. No matter how many of the IDs are found (including none), a map is
    1.46 +// returned, with the key being a String()ed version of the ID for the Subscription in
    1.47 +// the value. If no error is returned, the map will represent all of the Subscriptions// matching the passed IDs that exist in the Memstore, even if it's empty.
    1.48  func (m *Memstore) GetSubscriptions(ids []uuid.ID) (map[string]Subscription, error) {
    1.49  	if len(ids) < 1 {
    1.50  		return map[string]Subscription{}, ErrNoSubscriptionID
    1.51 @@ -80,6 +100,11 @@
    1.52  	return result, nil
    1.53  }
    1.54  
    1.55 +// GetSubscriptionStats returns statistics about the subscription data stored in the
    1.56 +// Memstore as a SubscriptionStats variable. The number of Subscriptions, the
    1.57 +// breakdown of how many Subscriptions belong to each plan, the number of
    1.58 +// Subscriptions that are canceling, and the number of Subscriptions whose payment
    1.59 +// information is failing are all tracked.
    1.60  func (m *Memstore) GetSubscriptionStats() (SubscriptionStats, error) {
    1.61  	m.subscriptionLock.RLock()
    1.62  	defer m.subscriptionLock.RUnlock()
     2.1 --- a/subscription_postgres.go	Sun Sep 27 21:20:46 2015 -0700
     2.2 +++ b/subscription_postgres.go	Wed Sep 30 01:03:39 2015 -0700
     2.3 @@ -21,6 +21,9 @@
     2.4  	return query.FlushExpressions(" ")
     2.5  }
     2.6  
     2.7 +// Reset returns the database to its initialised state. It is meant to
     2.8 +// be used during testing to reset the databases to a known state between
     2.9 +// tests. It should never be used in production.
    2.10  func (p Postgres) Reset() error {
    2.11  	query := p.resetSQL()
    2.12  	_, err := p.Exec(query.String(), query.Args...)
    2.13 @@ -39,6 +42,11 @@
    2.14  	return query.FlushExpressions(" ")
    2.15  }
    2.16  
    2.17 +// CreateSubscription stores the passed Subscription in the database. If
    2.18 +// a Subscription sharing the same UserID already exists, an
    2.19 +// ErrSubscriptionAlreadyExists error will be returned. If a Subscription
    2.20 +// sharing the same StripeSubscription already exists, an
    2.21 +// ErrStripeSubscriptionAlreadyExists error will be returned.
    2.22  func (p Postgres) CreateSubscription(sub Subscription) error {
    2.23  	query := p.createSubscriptionSQL(sub)
    2.24  	_, err := p.Exec(query.String(), query.Args...)
    2.25 @@ -71,6 +79,13 @@
    2.26  	return query.FlushExpressions(" ")
    2.27  }
    2.28  
    2.29 +// UpdateSubscription applies the SubscriptionChange passed to the Subscription
    2.30 +// stored in the database associated with the passed ID. If change is empty,
    2.31 +// an ErrSubscriptionChangeEmpty error is returned. If no Subscription is found
    2.32 +// in the database with the passed ID, an ErrSubscriptionNotFound error is returned.
    2.33 +// If change is updating the StripeSubscription and a Subscription in the database
    2.34 +// already has that value set for StripeSubscription, an
    2.35 +// ErrStripeSubscriptionAlreadyExists error is returned.
    2.36  func (p Postgres) UpdateSubscription(id uuid.ID, change SubscriptionChange) error {
    2.37  	if change.IsEmpty() {
    2.38  		return ErrSubscriptionChangeEmpty
    2.39 @@ -101,6 +116,9 @@
    2.40  	return query.FlushExpressions(" ")
    2.41  }
    2.42  
    2.43 +// DeleteSubscription removes the Subscription stored in the database associated
    2.44 +// with the passed ID from the database. If no Subscription is found
    2.45 +// in the database with the passed ID, an ErrSubscriptionNotFound error is returned.
    2.46  func (p Postgres) DeleteSubscription(id uuid.ID) error {
    2.47  	query := p.deleteSubscriptionSQL(id)
    2.48  	res, err := p.Exec(query.String(), query.Args...)
    2.49 @@ -131,6 +149,12 @@
    2.50  	return query.FlushExpressions(" ")
    2.51  }
    2.52  
    2.53 +// GetSubscriptions retrieves the Subscriptions stored in the database associated
    2.54 +// with the passed IDs. If no IDs are passed, an ErrNoSubscriptionID error is
    2.55 +// returned. No matter how many of the IDs are found (including none), a map is
    2.56 +// returned, with the key being a String()ed version of the ID for the Subscription in
    2.57 +// the value. If no error is returned, the map will represent all of the Subscriptions
    2.58 +// matching the passed IDs that exist in the database, even if it's empty.
    2.59  func (p Postgres) GetSubscriptions(ids []uuid.ID) (map[string]Subscription, error) {
    2.60  	results := map[string]Subscription{}
    2.61  	if len(ids) < 1 {
    2.62 @@ -190,6 +214,11 @@
    2.63  	return query.FlushExpressions(" ")
    2.64  }
    2.65  
    2.66 +// GetSubscriptionStats returns statistics about the subscription data stored in the
    2.67 +// database as a SubscriptionStats variable. The number of Subscriptions, the
    2.68 +// breakdown of how many Subscriptions belong to each plan, the number of
    2.69 +// Subscriptions that are canceling, and the number of Subscriptions whose payment
    2.70 +// information is failing are all tracked.
    2.71  func (p Postgres) GetSubscriptionStats() (SubscriptionStats, error) {
    2.72  	stats := SubscriptionStats{
    2.73  		Plans: map[string]int64{},