2020-01-28 19:51:00 +02:00
|
|
|
package repo
|
2019-12-13 20:08:26 +02:00
|
|
|
|
|
|
|
import (
|
2019-12-21 11:56:18 +02:00
|
|
|
"encoding/json"
|
2020-01-01 17:58:27 +02:00
|
|
|
|
2020-11-22 19:29:58 +02:00
|
|
|
"bloat/util"
|
2020-01-01 17:58:27 +02:00
|
|
|
"bloat/model"
|
2019-12-13 20:08:26 +02:00
|
|
|
)
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
type appRepo struct {
|
2020-11-22 19:29:58 +02:00
|
|
|
db *util.Database
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-11-22 19:29:58 +02:00
|
|
|
func NewAppRepo(db *util.Database) *appRepo {
|
2020-01-28 19:51:00 +02:00
|
|
|
return &appRepo{
|
2019-12-13 20:08:26 +02:00
|
|
|
db: db,
|
2019-12-17 22:17:25 +02:00
|
|
|
}
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (repo *appRepo) Add(a model.App) (err error) {
|
2019-12-21 11:56:18 +02:00
|
|
|
data, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repo.db.Set(a.InstanceDomain, data)
|
2019-12-13 20:08:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-28 19:51:00 +02:00
|
|
|
func (repo *appRepo) Get(instanceDomain string) (a model.App, err error) {
|
2019-12-17 22:17:25 +02:00
|
|
|
data, err := repo.db.Get(instanceDomain)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
err = model.ErrAppNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-21 11:56:18 +02:00
|
|
|
err = json.Unmarshal(data, &a)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-13 20:08:26 +02:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|