Parcourir la source

Store remote albums in local db

Neeraj Gupta il y a 1 an
Parent
commit
ed3d4edaad
3 fichiers modifiés avec 24 ajouts et 0 suppressions
  1. 6 0
      pkg/collections.go
  2. 9 0
      pkg/store.go
  3. 9 0
      utils/encoding/encoding.go

+ 6 - 0
pkg/collections.go

@@ -3,6 +3,7 @@ package pkg
 import (
 	debuglog "cli-go/pkg/log"
 	"cli-go/pkg/model"
+	"cli-go/utils/encoding"
 	"context"
 	"fmt"
 	"strconv"
@@ -36,6 +37,11 @@ func (c *ClICtrl) syncRemoteCollections(ctx context.Context, info model.Account)
 		if album.LastUpdatedAt > maxUpdated {
 			maxUpdated = album.LastUpdatedAt
 		}
+		albumJson := encoding.MustMarshalJSON(album)
+		err := c.PutValue(ctx, model.RemoteAlbums, []byte(strconv.FormatInt(album.ID, 10)), albumJson)
+		if err != nil {
+			return err
+		}
 		debuglog.PrintAlbum(album)
 	}
 	if maxUpdated > lastSyncTime {

+ 9 - 0
pkg/store.go

@@ -40,6 +40,15 @@ func (c *ClICtrl) PutConfigValue(ctx context.Context, key string, value []byte)
 		return kvBucket.Put([]byte(key), value)
 	})
 }
+func (c *ClICtrl) PutValue(ctx context.Context, store model.PhotosStore, key []byte, value []byte) error {
+	return c.DB.Update(func(tx *bolt.Tx) error {
+		kvBucket, err := getAccountStore(ctx, tx, store)
+		if err != nil {
+			return err
+		}
+		return kvBucket.Put(key, value)
+	})
+}
 
 func getAccountStore(ctx context.Context, tx *bolt.Tx, storeType model.PhotosStore) (*bolt.Bucket, error) {
 	accountId := ctx.Value("account_id").(string)

+ 9 - 0
utils/encoding/encoding.go

@@ -2,6 +2,7 @@ package encoding
 
 import (
 	"encoding/base64"
+	"encoding/json"
 )
 
 func DecodeBase64(s string) []byte {
@@ -15,3 +16,11 @@ func DecodeBase64(s string) []byte {
 func EncodeBase64(b []byte) string {
 	return base64.StdEncoding.EncodeToString(b)
 }
+
+func MustMarshalJSON(v interface{}) []byte {
+	b, err := json.Marshal(v)
+	if err != nil {
+		panic(err)
+	}
+	return b
+}