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 sessionRepository 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 NewSessionRepository(db *kv.Database) *sessionRepository {
|
2019-12-13 18:08:26 +00:00
|
|
|
return &sessionRepository{
|
|
|
|
db: db,
|
2019-12-17 20:17:25 +00:00
|
|
|
}
|
2019-12-13 18:08:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepository) Add(s model.Session) (err error) {
|
2019-12-21 09:56:18 +00:00
|
|
|
data, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repo.db.Set(s.ID, data)
|
2019-12-13 18:08:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
func (repo *sessionRepository) Update(id string, accessToken string) (err error) {
|
|
|
|
data, err := repo.db.Get(id)
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
var s model.Session
|
2019-12-21 09:56:18 +00:00
|
|
|
err = json.Unmarshal(data, &s)
|
2019-12-17 20:17:25 +00:00
|
|
|
if err != nil {
|
2019-12-13 18:08:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 20:17:25 +00:00
|
|
|
s.AccessToken = accessToken
|
|
|
|
|
2019-12-21 09:56:18 +00:00
|
|
|
data, err = json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.db.Set(id, data)
|
2019-12-17 20:17:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepository) Get(id string) (s model.Session, err error) {
|
|
|
|
data, err := repo.db.Get(id)
|
2019-12-13 18:08:26 +00:00
|
|
|
if err != nil {
|
2019-12-17 20:17:25 +00:00
|
|
|
err = model.ErrSessionNotFound
|
2019-12-13 18:08:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-21 09:56:18 +00:00
|
|
|
err = json.Unmarshal(data, &s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-17 20:17:25 +00:00
|
|
|
|
2019-12-13 18:08:26 +00:00
|
|
|
return
|
|
|
|
}
|