aboutsummaryrefslogtreecommitdiff
path: root/repo/sessionRepo.go
blob: 2097c3efcd8e64df6ae4bf4d00f71eb483cd1c46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package repo

import (
	"encoding/json"

	"bloat/util"
	"bloat/model"
)

type sessionRepo struct {
	db *util.Database
}

func NewSessionRepo(db *util.Database) *sessionRepo {
	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
}

func (repo *sessionRepo) Remove(id string) {
	repo.db.Remove(id)
	return
}