aboutsummaryrefslogtreecommitdiff
path: root/repository/appRepository.go
blob: 1a8f20470e90bb607b34c6c589300f63a0f563ba (plain)
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
package repository

import (
	"database/sql"

	"web/model"
)

type appRepository struct {
	db *sql.DB
}

func NewAppRepository(db *sql.DB) (*appRepository, error) {
	_, err := db.Exec(`CREATE TABLE IF NOT EXISTS app 
		(instance_url varchar, client_id varchar, client_secret varchar)`,
	)
	if err != nil {
		return nil, err
	}

	return &appRepository{
		db: db,
	}, nil
}

func (repo *appRepository) Add(a model.App) (err error) {
	_, err = repo.db.Exec("INSERT INTO app VALUES (?, ?, ?)", a.InstanceURL, a.ClientID, a.ClientSecret)
	return
}

func (repo *appRepository) Update(instanceURL string, clientID string, clientSecret string) (err error) {
	_, err = repo.db.Exec("UPDATE app SET client_id = ?, client_secret = ? where instance_url = ?", clientID, clientSecret, instanceURL)
	return
}

func (repo *appRepository) Get(instanceURL string) (a model.App, err error) {
	rows, err := repo.db.Query("SELECT * FROM app WHERE instance_url = ?", instanceURL)
	if err != nil {
		return
	}
	defer rows.Close()

	if !rows.Next() {
		err = model.ErrAppNotFound
		return
	}

	err = rows.Scan(&a.InstanceURL, &a.ClientID, &a.ClientSecret)
	if err != nil {
		return
	}

	return
}