2019-12-13 18:08:26 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2019-12-21 09:56:18 +00:00
|
|
|
"encoding/json"
|
2020-01-01 15:58:27 +00:00
|
|
|
|
|
|
|
"bloat/kv"
|
|
|
|
"bloat/model"
|
2019-12-13 18:08:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type appRepository struct {
|
2019-12-17 20:17:25 +00:00
|
|
|
db *kv.Database
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
func NewAppRepository(db *kv.Database) *appRepository {
|
2019-12-13 18:08:26 +00:00
|
|
|
return &appRepository{
|
|
|
|
db: db,
|
2019-12-17 20:17:25 +00:00
|
|
|
}
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *appRepository) Add(a model.App) (err error) {
|
2019-12-21 09:56:18 +00:00
|
|
|
data, err := json.Marshal(a)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repo.db.Set(a.InstanceDomain, data)
|
2019-12-13 18:08:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
func (repo *appRepository) Get(instanceDomain string) (a model.App, err error) {
|
|
|
|
data, err := repo.db.Get(instanceDomain)
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
err = model.ErrAppNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-21 09:56:18 +00:00
|
|
|
err = json.Unmarshal(data, &a)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-13 18:08:26 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|