ducky/subscriptions
2015-09-30
Parent:b240b6123548
ducky/subscriptions/subscription_postgres.go
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!
1.1 --- a/subscription_postgres.go Sun Sep 27 21:20:46 2015 -0700 1.2 +++ b/subscription_postgres.go Wed Sep 30 01:03:39 2015 -0700 1.3 @@ -21,6 +21,9 @@ 1.4 return query.FlushExpressions(" ") 1.5 } 1.6 1.7 +// Reset returns the database to its initialised state. It is meant to 1.8 +// be used during testing to reset the databases to a known state between 1.9 +// tests. It should never be used in production. 1.10 func (p Postgres) Reset() error { 1.11 query := p.resetSQL() 1.12 _, err := p.Exec(query.String(), query.Args...) 1.13 @@ -39,6 +42,11 @@ 1.14 return query.FlushExpressions(" ") 1.15 } 1.16 1.17 +// CreateSubscription stores the passed Subscription in the database. If 1.18 +// a Subscription sharing the same UserID already exists, an 1.19 +// ErrSubscriptionAlreadyExists error will be returned. If a Subscription 1.20 +// sharing the same StripeSubscription already exists, an 1.21 +// ErrStripeSubscriptionAlreadyExists error will be returned. 1.22 func (p Postgres) CreateSubscription(sub Subscription) error { 1.23 query := p.createSubscriptionSQL(sub) 1.24 _, err := p.Exec(query.String(), query.Args...) 1.25 @@ -71,6 +79,13 @@ 1.26 return query.FlushExpressions(" ") 1.27 } 1.28 1.29 +// UpdateSubscription applies the SubscriptionChange passed to the Subscription 1.30 +// stored in the database associated with the passed ID. If change is empty, 1.31 +// an ErrSubscriptionChangeEmpty error is returned. If no Subscription is found 1.32 +// in the database with the passed ID, an ErrSubscriptionNotFound error is returned. 1.33 +// If change is updating the StripeSubscription and a Subscription in the database 1.34 +// already has that value set for StripeSubscription, an 1.35 +// ErrStripeSubscriptionAlreadyExists error is returned. 1.36 func (p Postgres) UpdateSubscription(id uuid.ID, change SubscriptionChange) error { 1.37 if change.IsEmpty() { 1.38 return ErrSubscriptionChangeEmpty 1.39 @@ -101,6 +116,9 @@ 1.40 return query.FlushExpressions(" ") 1.41 } 1.42 1.43 +// DeleteSubscription removes the Subscription stored in the database associated 1.44 +// with the passed ID from the database. If no Subscription is found 1.45 +// in the database with the passed ID, an ErrSubscriptionNotFound error is returned. 1.46 func (p Postgres) DeleteSubscription(id uuid.ID) error { 1.47 query := p.deleteSubscriptionSQL(id) 1.48 res, err := p.Exec(query.String(), query.Args...) 1.49 @@ -131,6 +149,12 @@ 1.50 return query.FlushExpressions(" ") 1.51 } 1.52 1.53 +// GetSubscriptions retrieves the Subscriptions stored in the database associated 1.54 +// with the passed IDs. If no IDs are passed, an ErrNoSubscriptionID error is 1.55 +// returned. No matter how many of the IDs are found (including none), a map is 1.56 +// returned, with the key being a String()ed version of the ID for the Subscription in 1.57 +// the value. If no error is returned, the map will represent all of the Subscriptions 1.58 +// matching the passed IDs that exist in the database, even if it's empty. 1.59 func (p Postgres) GetSubscriptions(ids []uuid.ID) (map[string]Subscription, error) { 1.60 results := map[string]Subscription{} 1.61 if len(ids) < 1 { 1.62 @@ -190,6 +214,11 @@ 1.63 return query.FlushExpressions(" ") 1.64 } 1.65 1.66 +// GetSubscriptionStats returns statistics about the subscription data stored in the 1.67 +// database as a SubscriptionStats variable. The number of Subscriptions, the 1.68 +// breakdown of how many Subscriptions belong to each plan, the number of 1.69 +// Subscriptions that are canceling, and the number of Subscriptions whose payment 1.70 +// information is failing are all tracked. 1.71 func (p Postgres) GetSubscriptionStats() (SubscriptionStats, error) { 1.72 stats := SubscriptionStats{ 1.73 Plans: map[string]int64{},