@@ -6,14 +6,14 @@ import (
66 "encoding/json"
77 "fmt"
88 "path"
9+ "strings"
910
1011 "github.com/hashicorp/consul/api"
1112
1213 "tracker/internal/database"
1314)
1415
15- // UserDatabase implements database.UserDatabase and can be used
16- // to insert user data.
16+ // Database contains methods for getting the data about shows and episodes
1717type Database struct {
1818 kv KV
1919
@@ -39,7 +39,7 @@ func NewDatabase(prefix string, opts ...Option) (*Database, error) {
3939}
4040
4141// get value from the database
42- func (db * Database ) get (ctx context.Context , key string , value interface {} ) error {
42+ func (db * Database ) get (ctx context.Context , key string , value any ) error {
4343 opt := & api.QueryOptions {}
4444 opt = opt .WithContext (ctx )
4545
@@ -60,7 +60,7 @@ func (db *Database) get(ctx context.Context, key string, value interface{}) erro
6060 return nil
6161}
6262
63- func (db * Database ) put (ctx context.Context , key string , value interface {} ) error {
63+ func (db * Database ) put (ctx context.Context , key string , value any ) error {
6464 opt := & api.WriteOptions {}
6565 opt = opt .WithContext (ctx )
6666
@@ -79,6 +79,27 @@ func (db *Database) put(ctx context.Context, key string, value interface{}) erro
7979}
8080
8181// Option allows to set options for the Consul database.type Option func(*Database)
82+ func (db * Database ) list (ctx context.Context , prefix string ) (map [string ][]byte , error ) {
83+ opt := & api.QueryOptions {}
84+ opt = opt .WithContext (ctx )
85+
86+ p := path .Join (db .prefix , prefix )
87+
88+ kvs , _ , err := db .kv .List (p , opt )
89+ if err != nil {
90+ return nil , fmt .Errorf ("unable to list %s: %w" , p , err )
91+ }
92+
93+ m := make (map [string ][]byte , len (kvs ))
94+ for _ , kv := range kvs {
95+ // Remove all the prefixes from the keys.
96+ m [strings .TrimPrefix (kv .Key , p + "/" )] = kv .Value
97+ }
98+
99+ return m , nil
100+ }
101+
102+ // Option allows to set options for the Consul database.
82103type Option func (* Database )
83104
84105func KVClient (kv KV ) Option {
@@ -91,4 +112,5 @@ func KVClient(kv KV) Option {
91112type KV interface {
92113 Put (p * api.KVPair , q * api.WriteOptions ) (* api.WriteMeta , error )
93114 Get (key string , q * api.QueryOptions ) (* api.KVPair , * api.QueryMeta , error )
115+ List (prefix string , q * api.QueryOptions ) (api.KVPairs , * api.QueryMeta , error )
94116}
0 commit comments