ducky/subscriptions
ducky/subscriptions/postgres.go
Export all subscriptionStore methods. We're not going to wrap all our subscriptionStore interactions in a Context type, so we need to expose all the functions so other packages can call them. Also, it's now SubscriptionStore. Also creates a SubscriptionRequest type that is used to create a new Subscription instance, along with a Validate method on it, to detect errors when creating a SubscriptionRequest. Update our CreateSubscription function to be New, and have it take a SubscriptionRequest, a Stripe instance, and a SubscriptionStore as arguments. It'll create the Subscription in the SubscriptionStore, create a customer in Stripe, and create the subscription in Stripe, then associate the Stripe subscription with the created Subscription. Also, fix our StripeSubscriptionChange function to correctly equate a missing/zero Unix timestamp from Stripe with a missing/zero time.Time.
| paddy@1 | 1 package subscriptions |
| paddy@1 | 2 |
| paddy@1 | 3 import ( |
| paddy@1 | 4 "database/sql" |
| paddy@1 | 5 ) |
| paddy@1 | 6 |
| paddy@1 | 7 // NewPostgres returns a usable Postgres instance, if and only |
| paddy@1 | 8 // if error is nil. If error is not nil, the returned Postgres |
| paddy@1 | 9 // instance should not be used. |
| paddy@1 | 10 func NewPostgres(conn string) (Postgres, error) { |
| paddy@1 | 11 db, err := sql.Open("postgres", conn) |
| paddy@1 | 12 if err != nil { |
| paddy@1 | 13 return Postgres{}, err |
| paddy@1 | 14 } |
| paddy@1 | 15 return Postgres{db}, nil |
| paddy@1 | 16 } |
| paddy@1 | 17 |
| paddy@1 | 18 // Postgres represents a thin wrapper around *sql.DB, so we can |
| paddy@1 | 19 // inherit its methods but also define our own (so we can fulfill |
| paddy@1 | 20 // our subscriptionStore interface on Postgres). Note that the |
| paddy@1 | 21 // value of a Postgres variable contains a _pointer_ to an sql.DB |
| paddy@1 | 22 // pool of connections, so it's not necessary to use the address of |
| paddy@1 | 23 // the Postgres variable itself (though there shouldn't be any harm |
| paddy@1 | 24 // in doing so). |
| paddy@1 | 25 type Postgres struct { |
| paddy@1 | 26 *sql.DB |
| paddy@1 | 27 } |