-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
62 lines (49 loc) · 1.6 KB
/
template.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
package redis
import (
"context"
"errors"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/elojah/game_02/pkg/entity"
gerrors "github.com/elojah/game_02/pkg/errors"
)
const (
templateKey = "template_entity:"
)
// UpsertTemplate implementation for entity template in redis.
func (s *Store) UpsertTemplate(ctx context.Context, t entity.Template) error {
raw, err := t.Marshal()
if err != nil {
return err
}
if err := s.Set(ctx, templateKey+t.ID.String(), raw, 0).Err(); err != nil {
return fmt.Errorf("upsert template %s: %w", t.ID.String(), err)
}
return nil
}
// FetchTemplate implementation for entity template in redis.
func (s *Store) FetchTemplate(ctx context.Context, filter entity.FilterTemplate) (entity.Template, error) {
val, err := s.Get(ctx, templateKey+filter.ID.String()).Result()
if err != nil {
if !errors.Is(err, redis.Nil) {
return entity.Template{}, fmt.Errorf("fetch template %s: %w", filter.ID.String(), err)
}
return entity.Template{}, fmt.Errorf(
"fetch template %s: %w",
filter.ID.String(),
gerrors.ErrNotFound{Resource: templateKey, Index: filter.ID.String()},
)
}
var t entity.Template
if err := t.Unmarshal([]byte(val)); err != nil {
return entity.Template{}, fmt.Errorf("fetch template %s: %w", filter.ID.String(), err)
}
return t, nil
}
// DeleteTemplate implementation for entity template in redis.
func (s *Store) DeleteTemplate(ctx context.Context, filter entity.FilterTemplate) error {
if err := s.Del(ctx, templateKey+filter.ID.String()).Err(); err != nil {
return fmt.Errorf("remove template %s: %w", filter.ID.String(), err)
}
return nil
}