aboutsummaryrefslogtreecommitdiff
path: root/repo/appRepo.go
blob: 6338c4a2987685890da60cea1e2ed2a2047e9e4b (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
package repo

import (
	"encoding/json"

	"bloat/kv"
	"bloat/model"
)

type appRepo struct {
	db *kv.Database
}

func NewAppRepo(db *kv.Database) *appRepo {
	return &appRepo{
		db: db,
	}
}

func (repo *appRepo) Add(a model.App) (err error) {
	data, err := json.Marshal(a)
	if err != nil {
		return
	}
	err = repo.db.Set(a.InstanceDomain, data)
	return
}

func (repo *appRepo) Get(instanceDomain string) (a model.App, err error) {
	data, err := repo.db.Get(instanceDomain)
	if err != nil {
		err = model.ErrAppNotFound
		return
	}

	err = json.Unmarshal(data, &a)
	if err != nil {
		return
	}

	return
}