ducky/subscriptions

Paddy 2015-06-14 Parent:56a2bef197cd Child:61c4ce5850da

1:f1a22fc2321d Go to Latest

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.

History
1 package subscriptions
3 import (
4 "sort"
5 "time"
7 "code.secondbit.org/uuid.hg"
8 )
10 func stripeCustomerInMemstore(stripeCustomer string, m *Memstore) bool {
11 for _, sub := range m.subscriptions {
12 if sub.StripeCustomer == stripeCustomer {
13 return true
14 }
15 }
16 return false
17 }
19 func (m *Memstore) createSubscription(sub Subscription) error {
20 m.subscriptionLock.Lock()
21 defer m.subscriptionLock.Unlock()
23 if _, ok := m.subscriptions[sub.UserID.String()]; ok {
24 return ErrSubscriptionAlreadyExists
25 }
26 if stripeCustomerInMemstore(sub.StripeCustomer, m) {
27 return ErrStripeCustomerAlreadyExists
28 }
29 m.subscriptions[sub.UserID.String()] = sub
30 return nil
31 }
33 func (m *Memstore) updateSubscription(id uuid.ID, change SubscriptionChange) error {
34 if change.IsEmpty() {
35 return ErrSubscriptionChangeEmpty
36 }
38 m.subscriptionLock.Lock()
39 defer m.subscriptionLock.Unlock()
41 s, ok := m.subscriptions[id.String()]
42 if !ok {
43 return ErrSubscriptionNotFound
44 }
45 if change.StripeCustomer != nil {
46 if stripeCustomerInMemstore(*change.StripeCustomer, m) {
47 return ErrStripeCustomerAlreadyExists
48 }
49 }
50 s.ApplyChange(change)
51 m.subscriptions[id.String()] = s
52 return nil
53 }
55 func (m *Memstore) deleteSubscription(id uuid.ID) error {
56 m.subscriptionLock.Lock()
57 defer m.subscriptionLock.Unlock()
59 _, ok := m.subscriptions[id.String()]
60 if !ok {
61 return ErrSubscriptionNotFound
62 }
63 delete(m.subscriptions, id.String())
64 return nil
65 }
67 func (m *Memstore) listSubscriptionsLastChargedBefore(cutoff time.Time) ([]Subscription, error) {
68 m.subscriptionLock.RLock()
69 defer m.subscriptionLock.RUnlock()
71 var result []Subscription
72 for _, s := range m.subscriptions {
73 if cutoff.Before(s.LastCharged) {
74 continue
75 }
76 result = append(result, s)
77 }
79 sorted := ByLastChargeDate(result)
80 sort.Sort(sorted)
81 result = []Subscription(sorted)
83 return result, nil
84 }
86 func (m *Memstore) getSubscriptions(ids []uuid.ID) (map[string]Subscription, error) {
87 if len(ids) < 1 {
88 return map[string]Subscription{}, ErrNoSubscriptionID
89 }
90 m.subscriptionLock.RLock()
91 defer m.subscriptionLock.RUnlock()
93 result := map[string]Subscription{}
95 for _, id := range ids {
96 s, ok := m.subscriptions[id.String()]
97 if !ok {
98 continue
99 }
100 result[s.UserID.String()] = s
101 }
102 return result, nil
103 }
105 func (m *Memstore) getSubscriptionStats() (SubscriptionStats, error) {
106 m.subscriptionLock.RLock()
107 defer m.subscriptionLock.RUnlock()
109 stats := SubscriptionStats{}
111 for _, subscription := range m.subscriptions {
112 stats.Number++
113 stats.TotalAmount += int64(subscription.Amount)
114 }
116 if stats.Number > 0 {
117 stats.MeanAmount = float64(stats.TotalAmount) / float64(stats.Number)
118 }
119 return stats, nil
120 }