mirror of
https://git.phreedom.club/localhost_frssoft/bloat.git
synced 2024-11-22 20:11:29 +00:00
64 lines
989 B
Go
64 lines
989 B
Go
package repository
|
|
|
|
import (
|
|
"encoding/json"
|
|
"web/kv"
|
|
"web/model"
|
|
)
|
|
|
|
type sessionRepository struct {
|
|
db *kv.Database
|
|
}
|
|
|
|
func NewSessionRepository(db *kv.Database) *sessionRepository {
|
|
return &sessionRepository{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
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)
|
|
return
|
|
}
|
|
|
|
func (repo *sessionRepository) Update(id string, accessToken string) (err error) {
|
|
data, err := repo.db.Get(id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
var s model.Session
|
|
err = json.Unmarshal(data, &s)
|
|
if err != nil {
|
|
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)
|
|
if err != nil {
|
|
err = model.ErrSessionNotFound
|
|
return
|
|
}
|
|
|
|
err = json.Unmarshal(data, &s)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|