2019-12-13 20:08:26 +02:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
2019-12-21 11:56:18 +02:00
|
|
|
"encoding/json"
|
2019-12-17 22:17:25 +02:00
|
|
|
"web/kv"
|
2019-12-13 20:08:26 +02:00
|
|
|
"web/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sessionRepository struct {
|
2019-12-17 22:17:25 +02:00
|
|
|
db *kv.Database
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
2019-12-17 22:17:25 +02:00
|
|
|
func NewSessionRepository(db *kv.Database) *sessionRepository {
|
2019-12-13 20:08:26 +02:00
|
|
|
return &sessionRepository{
|
|
|
|
db: db,
|
2019-12-17 22:17:25 +02:00
|
|
|
}
|
2019-12-13 20:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepository) Add(s model.Session) (err error) {
|
2019-12-21 11:56:18 +02:00
|
|
|
data, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repo.db.Set(s.ID, data)
|
2019-12-13 20:08:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:17:25 +02:00
|
|
|
func (repo *sessionRepository) Update(id string, accessToken string) (err error) {
|
|
|
|
data, err := repo.db.Get(id)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:17:25 +02:00
|
|
|
var s model.Session
|
2019-12-21 11:56:18 +02:00
|
|
|
err = json.Unmarshal(data, &s)
|
2019-12-17 22:17:25 +02:00
|
|
|
if err != nil {
|
2019-12-13 20:08:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-17 22:17:25 +02:00
|
|
|
s.AccessToken = accessToken
|
|
|
|
|
2019-12-21 11:56:18 +02:00
|
|
|
data, err = json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return repo.db.Set(id, data)
|
2019-12-17 22:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepository) Get(id string) (s model.Session, err error) {
|
|
|
|
data, err := repo.db.Get(id)
|
2019-12-13 20:08:26 +02:00
|
|
|
if err != nil {
|
2019-12-17 22:17:25 +02:00
|
|
|
err = model.ErrSessionNotFound
|
2019-12-13 20:08:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-21 11:56:18 +02:00
|
|
|
err = json.Unmarshal(data, &s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-12-17 22:17:25 +02:00
|
|
|
|
2019-12-13 20:08:26 +02:00
|
|
|
return
|
|
|
|
}
|