]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - db/market_config.go
initial commit
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / db / market_config.go
CommitLineData
7a9e5112 1package db
2
3import "github.com/go-pg/pg"
4
5type MarketConfig struct {
6 Id int64
7 MarketName string `sql:",notnull"`
8 UserId int64 `sql:",notnull"`
9 Config map[string]string
10}
11
12func InsertMarketConfig(config *MarketConfig) error {
13 return DB.Insert(config)
14}
15
16func 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
32func 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}