bloat/repository/sessionRepository.go

64 lines
989 B
Go
Raw Normal View History

2019-12-13 20:08:26 +02:00
package repository
import (
"encoding/json"
"web/kv"
2019-12-13 20:08:26 +02:00
"web/model"
)
type sessionRepository struct {
db *kv.Database
2019-12-13 20:08:26 +02:00
}
func NewSessionRepository(db *kv.Database) *sessionRepository {
2019-12-13 20:08:26 +02:00
return &sessionRepository{
db: db,
}
2019-12-13 20:08:26 +02:00
}
func (repo *sessionRepository) Add(s model.Session) (err error) {
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
}
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
}
var s model.Session
err = json.Unmarshal(data, &s)
if err != nil {
2019-12-13 20:08:26 +02:00
return
}
s.AccessToken = accessToken
data, err = json.Marshal(s)
if err != nil {
return
}
return repo.db.Set(id, data)
}
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 {
err = model.ErrSessionNotFound
2019-12-13 20:08:26 +02:00
return
}
err = json.Unmarshal(data, &s)
if err != nil {
return
}
2019-12-13 20:08:26 +02:00
return
}