2020-01-28 18:51:00 +01:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2020-11-22 18:29:58 +01:00
|
|
|
"bloat/util"
|
2020-01-28 18:51:00 +01:00
|
|
|
"bloat/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sessionRepo struct {
|
2020-11-22 18:29:58 +01:00
|
|
|
db *util.Database
|
2020-01-28 18:51:00 +01:00
|
|
|
}
|
|
|
|
|
2020-11-22 18:29:58 +01:00
|
|
|
func NewSessionRepo(db *util.Database) *sessionRepo {
|
2020-01-28 18:51:00 +01:00
|
|
|
return &sessionRepo{
|
|
|
|
db: db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepo) Add(s model.Session) (err error) {
|
|
|
|
data, err := json.Marshal(s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = repo.db.Set(s.ID, data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *sessionRepo) Get(id string) (s model.Session, err error) {
|
|
|
|
data, err := repo.db.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
err = model.ErrSessionNotFound
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &s)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2020-03-04 16:59:59 +01:00
|
|
|
|
|
|
|
func (repo *sessionRepo) Remove(id string) {
|
|
|
|
repo.db.Remove(id)
|
|
|
|
return
|
|
|
|
}
|