ducky/subscriptions
ducky/subscriptions/memstore.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@0 | 1 package subscriptions |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "sync" |
| paddy@0 | 5 ) |
| paddy@0 | 6 |
| paddy@0 | 7 // Memstore is an in-memory version of our datastores, useful |
| paddy@0 | 8 // for testing. It should not be used in production. |
| paddy@0 | 9 type Memstore struct { |
| paddy@0 | 10 subscriptions map[string]Subscription |
| paddy@0 | 11 subscriptionLock sync.RWMutex |
| paddy@0 | 12 } |
| paddy@0 | 13 |
| paddy@0 | 14 // NewMemstore returns a pointer to a Memstore object, ready |
| paddy@0 | 15 // to be used as a datastore. |
| paddy@0 | 16 func NewMemstore() *Memstore { |
| paddy@0 | 17 return &Memstore{ |
| paddy@0 | 18 subscriptions: map[string]Subscription{}, |
| paddy@0 | 19 } |
| paddy@0 | 20 } |
| paddy@0 | 21 |
| paddy@3 | 22 func (m *Memstore) Reset() error { |
| paddy@0 | 23 m.subscriptionLock.Lock() |
| paddy@0 | 24 defer m.subscriptionLock.Unlock() |
| paddy@0 | 25 |
| paddy@0 | 26 m.subscriptions = map[string]Subscription{} |
| paddy@0 | 27 return nil |
| paddy@0 | 28 } |