diff options
author | jloup <jean-loup.jamet@trainline.com> | 2018-02-14 14:19:09 +0100 |
---|---|---|
committer | jloup <jean-loup.jamet@trainline.com> | 2018-02-14 14:19:09 +0100 |
commit | 7a9e5112eaaea58d55f181d3e5296e4ff839921c (patch) | |
tree | 968ed193f42a1fad759cc89ad2f8ad5b0091291e /db/market_config.go | |
download | Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.tar.gz Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.tar.zst Front-7a9e5112eaaea58d55f181d3e5296e4ff839921c.zip |
initial commit
Diffstat (limited to 'db/market_config.go')
-rw-r--r-- | db/market_config.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/db/market_config.go b/db/market_config.go new file mode 100644 index 0000000..b26c092 --- /dev/null +++ b/db/market_config.go | |||
@@ -0,0 +1,45 @@ | |||
1 | package db | ||
2 | |||
3 | import "github.com/go-pg/pg" | ||
4 | |||
5 | type MarketConfig struct { | ||
6 | Id int64 | ||
7 | MarketName string `sql:",notnull"` | ||
8 | UserId int64 `sql:",notnull"` | ||
9 | Config map[string]string | ||
10 | } | ||
11 | |||
12 | func InsertMarketConfig(config *MarketConfig) error { | ||
13 | return DB.Insert(config) | ||
14 | } | ||
15 | |||
16 | func GetUserMarketConfig(userId int64, market string) (*MarketConfig, error) { | ||
17 | var config MarketConfig | ||
18 | |||
19 | err := DB.Model(&config).Where("user_id = ?", userId).Where("market_name = ?", market).First() | ||
20 | |||
21 | if err != nil && err != pg.ErrNoRows { | ||
22 | return nil, err | ||
23 | } | ||
24 | |||
25 | if err == pg.ErrNoRows { | ||
26 | return nil, nil | ||
27 | } else { | ||
28 | return &config, nil | ||
29 | } | ||
30 | } | ||
31 | |||
32 | func SetUserMarketConfig(userId int64, market string, newConfig map[string]string) (*MarketConfig, error) { | ||
33 | config := MarketConfig{ | ||
34 | UserId: userId, | ||
35 | MarketName: market, | ||
36 | Config: newConfig, | ||
37 | } | ||
38 | |||
39 | _, err := DB.Model(&config). | ||
40 | OnConflict("(user_id, market_name) DO UPDATE"). | ||
41 | Set("config = ?", newConfig). | ||
42 | Insert() | ||
43 | |||
44 | return &config, err | ||
45 | } | ||