/
links.go
84 lines (70 loc) · 2.21 KB
/
links.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
74
75
76
77
78
79
80
81
82
83
84
package database
import (
"database/sql"
"encoding/json"
"errors"
"github.com/desmos-labs/caerus/types"
)
// SaveCreatedDeepLink allows to save the given link inside the database
func (db *Database) SaveCreatedDeepLink(link *types.CreatedDeepLink) error {
stmt := `
INSERT INTO deep_links (id, application_id, link_url, link_config, creation_time)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT DO NOTHING`
configBz, err := json.Marshal(link.Config)
if err != nil {
return err
}
_, err = db.SQL.Exec(stmt,
link.ID,
link.AppID,
link.URL,
string(configBz),
link.CreationTime,
)
return err
}
// GetDeepLinkConfig allows to return the types.LinkConfig associated to the link having the given URL, if any
func (db *Database) GetDeepLinkConfig(url string) (*types.LinkConfig, error) {
stmt := `SELECT link_config FROM deep_links WHERE link_url = $1`
var configBz []byte
err := db.SQL.Get(&configBz, stmt, url)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}
var config types.LinkConfig
err = json.Unmarshal(configBz, &config)
if err != nil {
return nil, err
}
return &config, nil
}
// --------------------------------------------------------------------------------------------------------------------
// GetAppDeepLinksRateLimit returns the deep links rate limit for the given application.
// If 0 is returned, it means that the application has no rate limit.
func (db *Database) GetAppDeepLinksRateLimit(appID string) (uint64, error) {
stmt := `
SELECT COALESCE(application_subscriptions.deep_links_rate_limit, 0)
FROM applications
LEFT JOIN application_subscriptions ON application_subscriptions.id = applications.subscription_id
WHERE applications.id = $1;`
var limit uint64
err := db.SQL.Get(&limit, stmt, appID)
return limit, err
}
// GetAppDeepLinksCount returns the number of deep links that the given application
// has created during the current day
func (db *Database) GetAppDeepLinksCount(appID string) (uint64, error) {
stmt := `
SELECT COUNT(id)
FROM deep_links
WHERE application_id = $1
AND creation_time >= CURRENT_DATE
AND creation_time < CURRENT_DATE + INTERVAL '1 day'`
var count uint64
err := db.SQL.Get(&count, stmt, appID)
return count, err
}