aboutsummaryrefslogtreecommitdiff
path: root/db/market_config.go
blob: b26c0920bf30bbfcfda36478589c4a57d19437e0 (plain) (blame)
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
package db

import "github.com/go-pg/pg"

type MarketConfig struct {
	Id         int64
	MarketName string `sql:",notnull"`
	UserId     int64  `sql:",notnull"`
	Config     map[string]string
}

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
}