/
store.go
69 lines (56 loc) · 1.53 KB
/
store.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
/*
Store Endpoint
*/
package query
import (
"context"
"github.com/go-redis/cache/v9"
"github.com/spf13/viper"
"github.com/batazor/shortlink/internal/pkg/db"
"github.com/batazor/shortlink/internal/pkg/logger"
"github.com/batazor/shortlink/internal/pkg/logger/field"
v12 "github.com/batazor/shortlink/internal/services/link/domain/link_cqrs/v1"
"github.com/batazor/shortlink/internal/services/link/infrastructure/store/cqrs/query/postgres"
"github.com/batazor/shortlink/internal/services/link/infrastructure/store/crud/query"
)
// New return implementation of db
func New(ctx context.Context, log logger.Logger, db *db.Store, cache *cache.Cache) (*Store, error) {
s := &Store{
log: log,
cache: cache,
}
// Set configuration
s.setConfig()
var err error
switch s.typeStore {
case "postgres":
fallthrough
default:
s.store, err = postgres.New(ctx, db)
if err != nil {
return nil, err
}
}
log.Info("init queryStore", field.Fields{
"db": s.typeStore,
})
return s, nil
}
func (s *Store) Get(ctx context.Context, id string) (*v12.LinkView, error) {
return s.store.Get(ctx, id)
}
func (s *Store) List(ctx context.Context, filter *query.Filter) (*v12.LinksView, error) {
if filter.Pagination == nil {
filter.Pagination = &query.Pagination{
Page: 0,
Limit: 10, // nolint:gomnd
}
}
return s.store.List(ctx, filter)
}
// setConfig - set configuration
func (s *Store) setConfig() {
viper.AutomaticEnv()
viper.SetDefault("STORE_TYPE", "postgres") // Select: postgres
s.typeStore = viper.GetString("STORE_TYPE")
}