forked from dstpierre/gosaas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
73 lines (65 loc) · 2.3 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package data
import (
"database/sql"
"fmt"
"strconv"
"time"
"github.com/jlb922/gosaas/model"
)
// DB is a database agnostic abstraction that contains a reference to
// the database connection.
//
// At this moment Postgres and an in-memory data provider are supported.
type DB struct {
// DatabaseName is the name of the database used.
DatabaseName string
// Connection is the reference to the database connection.
Connection *sql.DB
// Users contains the data access functions related to account, user and billing.
Users UserServices
// Webhooks contains the data access functions related to managing Webhooks.
Webhooks WebhookServices
}
// UserServices is an interface that contians all functions related to account, user and billing.
type UserServices interface {
SignUp(email, password, first, last string) (*model.Account, error)
StoreTempPassword(id int64, email, password string) error
UpdateLastLogin(id int64) error
ChangePassword(id, accountID int64, passwd string) error
AddToken(accountID, userID int64, name string) (*model.AccessToken, error)
RemoveToken(accountID, userID, tokenID int64) error
Auth(accountID int64, token string, pat bool) (*model.Account, *model.User, error)
GetUserByEmail(email string) (*model.User, error)
GetDetail(id int64) (*model.Account, error)
GetByStripe(stripeID string) (*model.Account, error)
SetSeats(id int64, seats int) error
ConvertToPaid(id int64, stripeID, subID, plan string, yearly bool, seats int) error
ChangePlan(id int64, plan string, yearly bool) error
Cancel(id int64) error
}
// AdminServices TODO: investigate this...
type AdminServices interface {
LogRequests(reqs []model.APIRequest) error
}
// WebhookServices is an interface that contains all functions to manage webhook.
type WebhookServices interface {
Add(accountID int64, events, url string) error
List(accountID int64) ([]model.Webhook, error)
Delete(accountID int64, event, url string) error
AllSubscriptions(event string) ([]model.Webhook, error)
}
// NewID returns a per second unique string based on account and user ids.
func NewID(accountID, userID int64) string {
n := time.Now()
i, _ := strconv.Atoi(
fmt.Sprintf("%d%d%d%d%d%d%d%d",
accountID,
userID,
n.Year()-2000,
int(n.Month()),
n.Day(),
n.Hour(),
n.Minute(),
n.Second()))
return fmt.Sprintf("%x", i)
}