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
|
package db
import (
"github.com/go-pg/pg"
)
type MarketConfigStatus string
const MarketConfigEnabled = "enabled"
const MarketConfigDisabled = "disabled"
const MarketConfigInvalidCredentials = "invalid_credentials"
type MarketConfig struct {
Id int64
MarketName string
UserId int64
Status MarketConfigStatus
Config map[string]string
// Will be expanded by pg
User User
}
func InsertMarketConfig(config *MarketConfig) error {
return DB.Insert(config)
}
func GetUserMarketConfig(userId int64, market string) (*MarketConfig, error) {
var config MarketConfig
err := DB.Model(&config).Where("user_id = ?", userId).Where("market_name = ?", market).First()
if err != nil && err != pg.ErrNoRows {
return nil, err
}
if err == pg.ErrNoRows {
return nil, nil
} else {
return &config, nil
}
}
func SetUserMarketConfig(userId int64, market string, newConfig map[string]string) (*MarketConfig, error) {
config := MarketConfig{
UserId: userId,
MarketName: market,
Config: newConfig,
}
_, err := DB.Model(&config).
OnConflict("(user_id, market_name) DO UPDATE").
Set("config = ?", newConfig).
Insert()
return &config, err
}
func SetMarketConfigStatus(marketConfig MarketConfig, status MarketConfigStatus) (*MarketConfig, error) {
marketConfig.Status = status
_, err := DB.Model(&marketConfig).
OnConflict("(user_id, market_name) DO UPDATE").
Set("status = ?", status).
Insert()
return &marketConfig, err
}
func GetActiveUsers() ([]MarketConfig, error) {
var configs []MarketConfig
err := DB.Model(&configs).Column("User").Where("market_config.status=?", "enabled").Select()
if err != nil {
return nil, err
}
return configs, nil
}
|