/
redis.go
99 lines (78 loc) · 2.5 KB
/
redis.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package redis
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
"google.golang.org/protobuf/encoding/protojson"
"github.com/batazor/shortlink/internal/pkg/db"
v1 "github.com/batazor/shortlink/internal/services/link/domain/link/v1"
"github.com/batazor/shortlink/internal/services/link/infrastructure/store/crud/query"
)
// Store implementation of db interface
type Store struct {
client redis.UniversalClient
}
// New store
func New(ctx context.Context, db *db.Store) (*Store, error) {
s := &Store{
client: db.Store.GetConn().(redis.UniversalClient),
}
return s, nil
}
// Get ...
func (s *Store) Get(ctx context.Context, id string) (*v1.Link, error) {
val, err := s.client.Get(ctx, id).Result()
if err != nil {
return nil, &v1.NotFoundError{Link: &v1.Link{Hash: id}, Err: fmt.Errorf("Not found id: %s", id)}
}
var response v1.Link
if err = protojson.Unmarshal([]byte(val), &response); err != nil {
return nil, &v1.NotFoundError{Link: &v1.Link{Hash: id}, Err: fmt.Errorf("Failed parse link: %s", id)}
}
return &response, nil
}
// List ...
func (s *Store) List(ctx context.Context, filter *query.Filter) (*v1.Links, error) {
keys := s.client.Keys(ctx, "*")
links := &v1.Links{
Link: []*v1.Link{},
}
for _, key := range keys.Val() {
var response v1.Link
val, err := s.client.Get(ctx, key).Result()
if err != nil {
return nil, &v1.NotFoundError{Link: &v1.Link{}, Err: fmt.Errorf("Not found links")}
}
if err = protojson.Unmarshal([]byte(val), &response); err != nil {
return nil, &v1.NotFoundError{Link: &v1.Link{}, Err: fmt.Errorf("Not found links")}
}
links.Link = append(links.Link, &response)
}
return links, nil
}
// Add ...
func (s *Store) Add(ctx context.Context, source *v1.Link) (*v1.Link, error) {
err := v1.NewURL(source)
if err != nil {
return nil, err
}
val, err := protojson.Marshal(source)
if err != nil {
return nil, &v1.NotFoundError{Link: source, Err: fmt.Errorf("Failed marsharing link: %s", source.Url)}
}
if err = s.client.Set(ctx, source.Hash, val, 0).Err(); err != nil {
return nil, &v1.NotFoundError{Link: source, Err: fmt.Errorf("Failed save link: %s", source.Url)}
}
return source, nil
}
// Update ...
func (s *Store) Update(_ context.Context, _ *v1.Link) (*v1.Link, error) {
return nil, nil
}
// Delete ...
func (s *Store) Delete(ctx context.Context, id string) error {
if err := s.client.Del(ctx, id).Err(); err != nil {
return &v1.NotFoundError{Link: &v1.Link{Hash: id}, Err: fmt.Errorf("Failed save link: %s", id)}
}
return nil
}