-
Notifications
You must be signed in to change notification settings - Fork 0
/
tg_session_repository.go
76 lines (59 loc) · 2.02 KB
/
tg_session_repository.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package redis
import (
"context"
"fmt"
goredis "github.com/redis/go-redis/v9"
"github.com/yaruz/app/internal/pkg/apperror"
"github.com/yaruz/app/internal/pkg/socnets/tg"
tg_session_proto "github.com/yaruz/app/internal/app/proto/tg_session"
)
const (
tgSession_KeyPrefix = "tg_session_"
)
// SessionRepository is a repository for the tg session
type TgSessionRepository struct {
Repository
ID string
}
var _ tg.SessionRepository = (*TgSessionRepository)(nil)
// NewTgSessionRepository creates a new TgSessionRepository
func NewTgSessionRepository(repository Repository) *TgSessionRepository {
return &TgSessionRepository{
Repository: repository,
}
}
func (r *TgSessionRepository) key(ID string) string {
return tgSession_KeyPrefix + ID
}
// Get returns the TgSession with the specified ID.
func (r *TgSessionRepository) Get(ctx context.Context, ID string) (*tg.Session, error) {
res, err := r.DB().Get(ctx, r.key(ID)).Result()
if err != nil {
if err == goredis.Nil {
return nil, tg.ErrNotFound
}
return nil, fmt.Errorf("[%w] Get() error: %s", apperror.ErrInternal, err.Error())
}
entity, err := tg_session_proto.UnmarshalBinary([]byte(res))
if err != nil {
return nil, fmt.Errorf("[%w] tg_session_proto.UnmarshalBinary() error: %s", apperror.ErrInternal, err.Error())
}
return entity, nil
}
func (r *TgSessionRepository) Set(ctx context.Context, entity *tg.Session) error {
data, err := tg_session_proto.MarshalBinary(entity)
if err != nil {
return fmt.Errorf("[%w] tg_session_proto.MarshalBinary() error: %s", apperror.ErrInternal, err.Error())
}
if err := r.DB().Set(ctx, r.key(entity.ID), data, 0).Err(); err != nil {
return fmt.Errorf("[%w] Redis Set() error: %s", apperror.ErrInternal, err.Error())
}
return nil
}
// Delete removes the entity with given ID from the storage.
func (r *TgSessionRepository) Delete(ctx context.Context, ID string) error {
if err := r.DB().Del(ctx, r.key(ID)).Err(); err != nil {
return fmt.Errorf("[%w] Delete error: %s", apperror.ErrInternal, err.Error())
}
return nil
}