bloat/repository/sessionRepository.go

51 lines
833 B
Go
Raw Normal View History

2019-12-13 20:08:26 +02:00
package repository
import (
"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) {
err = repo.db.Set(s.ID, s.Marshal())
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 = s.Unmarshal(id, data)
if err != nil {
2019-12-13 20:08:26 +02:00
return
}
s.AccessToken = accessToken
return repo.db.Set(id, s.Marshal())
}
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 = s.Unmarshal(id, data)
2019-12-13 20:08:26 +02:00
return
}