ducky/subscriptions
ducky/subscriptions/subscription_memstore.go
Implement PostgreSQL support, drop subscription IDs. Create a Postgres object that wraps database/sql, so we can attach methods to it and fulfill interfaces. Create a postgres_init.sql script that will create the subscriptions table in a PostgreSQL database. Make our period type fulfill the driver.Valuer and driver.Scanner types, so it can be stored in and retrieved from SQL databases. Create a SubscriptionStats type, and add a method to our subscriptionStore interface that will allow us to retrieve current stats about the Subscriptions it is storing. Deprecated the ID property of our Subscription type, and use the Subscription.UserID property instead as our primary key. Subscriptions should be unique per user and we generally will want to access Subscriptions in the context of the User they belong to, so the UserID is a better primary key. This also means we removed the getSubscriptionByUserID method (and implementations) from our subscriptionStore, as getSubscriptions now fills that role. Implement our getSubscriptionStats method in the memstore. Implement the subscriptionStore interface on our new Postgres type. Run the subscription store tests on our Postgres type, as well, if the PG_TEST_DB environment variable is set. Round all our timestamps in our tests to the nearest millisecond, as Postgres silently truncates all timestamps to the nearest millisecond, and it was causing false test failures. Remove the tests for our getSubscriptionStoreByUser method, as that was removed.
| paddy@0 | 1 package subscriptions |
| paddy@0 | 2 |
| paddy@0 | 3 import ( |
| paddy@0 | 4 "sort" |
| paddy@0 | 5 "time" |
| paddy@0 | 6 |
| paddy@0 | 7 "code.secondbit.org/uuid.hg" |
| paddy@0 | 8 ) |
| paddy@0 | 9 |
| paddy@0 | 10 func stripeCustomerInMemstore(stripeCustomer string, m *Memstore) bool { |
| paddy@0 | 11 for _, sub := range m.subscriptions { |
| paddy@0 | 12 if sub.StripeCustomer == stripeCustomer { |
| paddy@0 | 13 return true |
| paddy@0 | 14 } |
| paddy@0 | 15 } |
| paddy@0 | 16 return false |
| paddy@0 | 17 } |
| paddy@0 | 18 |
| paddy@0 | 19 func (m *Memstore) createSubscription(sub Subscription) error { |
| paddy@0 | 20 m.subscriptionLock.Lock() |
| paddy@0 | 21 defer m.subscriptionLock.Unlock() |
| paddy@0 | 22 |
| paddy@1 | 23 if _, ok := m.subscriptions[sub.UserID.String()]; ok { |
| paddy@0 | 24 return ErrSubscriptionAlreadyExists |
| paddy@0 | 25 } |
| paddy@0 | 26 if stripeCustomerInMemstore(sub.StripeCustomer, m) { |
| paddy@0 | 27 return ErrStripeCustomerAlreadyExists |
| paddy@0 | 28 } |
| paddy@1 | 29 m.subscriptions[sub.UserID.String()] = sub |
| paddy@0 | 30 return nil |
| paddy@0 | 31 } |
| paddy@0 | 32 |
| paddy@0 | 33 func (m *Memstore) updateSubscription(id uuid.ID, change SubscriptionChange) error { |
| paddy@0 | 34 if change.IsEmpty() { |
| paddy@0 | 35 return ErrSubscriptionChangeEmpty |
| paddy@0 | 36 } |
| paddy@0 | 37 |
| paddy@0 | 38 m.subscriptionLock.Lock() |
| paddy@0 | 39 defer m.subscriptionLock.Unlock() |
| paddy@0 | 40 |
| paddy@0 | 41 s, ok := m.subscriptions[id.String()] |
| paddy@0 | 42 if !ok { |
| paddy@0 | 43 return ErrSubscriptionNotFound |
| paddy@0 | 44 } |
| paddy@0 | 45 if change.StripeCustomer != nil { |
| paddy@0 | 46 if stripeCustomerInMemstore(*change.StripeCustomer, m) { |
| paddy@0 | 47 return ErrStripeCustomerAlreadyExists |
| paddy@0 | 48 } |
| paddy@0 | 49 } |
| paddy@0 | 50 s.ApplyChange(change) |
| paddy@0 | 51 m.subscriptions[id.String()] = s |
| paddy@0 | 52 return nil |
| paddy@0 | 53 } |
| paddy@0 | 54 |
| paddy@0 | 55 func (m *Memstore) deleteSubscription(id uuid.ID) error { |
| paddy@0 | 56 m.subscriptionLock.Lock() |
| paddy@0 | 57 defer m.subscriptionLock.Unlock() |
| paddy@0 | 58 |
| paddy@0 | 59 _, ok := m.subscriptions[id.String()] |
| paddy@0 | 60 if !ok { |
| paddy@0 | 61 return ErrSubscriptionNotFound |
| paddy@0 | 62 } |
| paddy@0 | 63 delete(m.subscriptions, id.String()) |
| paddy@0 | 64 return nil |
| paddy@0 | 65 } |
| paddy@0 | 66 |
| paddy@0 | 67 func (m *Memstore) listSubscriptionsLastChargedBefore(cutoff time.Time) ([]Subscription, error) { |
| paddy@0 | 68 m.subscriptionLock.RLock() |
| paddy@0 | 69 defer m.subscriptionLock.RUnlock() |
| paddy@0 | 70 |
| paddy@0 | 71 var result []Subscription |
| paddy@0 | 72 for _, s := range m.subscriptions { |
| paddy@0 | 73 if cutoff.Before(s.LastCharged) { |
| paddy@0 | 74 continue |
| paddy@0 | 75 } |
| paddy@0 | 76 result = append(result, s) |
| paddy@0 | 77 } |
| paddy@0 | 78 |
| paddy@0 | 79 sorted := ByLastChargeDate(result) |
| paddy@0 | 80 sort.Sort(sorted) |
| paddy@0 | 81 result = []Subscription(sorted) |
| paddy@0 | 82 |
| paddy@0 | 83 return result, nil |
| paddy@0 | 84 } |
| paddy@0 | 85 |
| paddy@0 | 86 func (m *Memstore) getSubscriptions(ids []uuid.ID) (map[string]Subscription, error) { |
| paddy@0 | 87 if len(ids) < 1 { |
| paddy@0 | 88 return map[string]Subscription{}, ErrNoSubscriptionID |
| paddy@0 | 89 } |
| paddy@0 | 90 m.subscriptionLock.RLock() |
| paddy@0 | 91 defer m.subscriptionLock.RUnlock() |
| paddy@0 | 92 |
| paddy@0 | 93 result := map[string]Subscription{} |
| paddy@0 | 94 |
| paddy@0 | 95 for _, id := range ids { |
| paddy@0 | 96 s, ok := m.subscriptions[id.String()] |
| paddy@0 | 97 if !ok { |
| paddy@0 | 98 continue |
| paddy@0 | 99 } |
| paddy@1 | 100 result[s.UserID.String()] = s |
| paddy@0 | 101 } |
| paddy@0 | 102 return result, nil |
| paddy@0 | 103 } |
| paddy@0 | 104 |
| paddy@1 | 105 func (m *Memstore) getSubscriptionStats() (SubscriptionStats, error) { |
| paddy@0 | 106 m.subscriptionLock.RLock() |
| paddy@0 | 107 defer m.subscriptionLock.RUnlock() |
| paddy@0 | 108 |
| paddy@1 | 109 stats := SubscriptionStats{} |
| paddy@1 | 110 |
| paddy@0 | 111 for _, subscription := range m.subscriptions { |
| paddy@1 | 112 stats.Number++ |
| paddy@1 | 113 stats.TotalAmount += int64(subscription.Amount) |
| paddy@0 | 114 } |
| paddy@0 | 115 |
| paddy@1 | 116 if stats.Number > 0 { |
| paddy@1 | 117 stats.MeanAmount = float64(stats.TotalAmount) / float64(stats.Number) |
| paddy@1 | 118 } |
| paddy@1 | 119 return stats, nil |
| paddy@0 | 120 } |