Skip to content

Commit 33fee06

Browse files
authored
Merge pull request #11 from Dobefu/feature/sync-assets
Feature/sync assets
2 parents 7d0816f + 85837cc commit 33fee06

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

cmd/cs_sdk/functions/sync.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/Dobefu/csb/cmd/cs_sdk"
1111
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
1212
"github.com/Dobefu/csb/cmd/cs_sdk/utils"
13+
"github.com/Dobefu/csb/cmd/database/assets"
1314
"github.com/Dobefu/csb/cmd/database/query"
1415
db_routes "github.com/Dobefu/csb/cmd/database/routes"
1516
"github.com/Dobefu/csb/cmd/database/state"
@@ -51,6 +52,12 @@ func Sync(reset bool) error {
5152
return err
5253
}
5354

55+
err = addAllAssets(data)
56+
57+
if err != nil {
58+
return err
59+
}
60+
5461
var hasPaginationToken bool
5562

5663
paginationToken, hasPaginationToken = data["pagination_token"].(string)
@@ -86,6 +93,60 @@ func getNewSyncToken(data map[string]interface{}) (string, error) {
8693
return newSyncToken, nil
8794
}
8895

96+
func addAllAssets(data map[string]interface{}) error {
97+
items, hasItems := data["items"].([]interface{})
98+
99+
if !hasItems {
100+
return errors.New("sync data has no items")
101+
}
102+
103+
itemCount := len(items)
104+
105+
for idx, item := range items {
106+
logger.Info("Fetching item data (%d/%d)", (idx + 1), itemCount)
107+
108+
item := item.(map[string]interface{})
109+
110+
if item["content_type_uid"].(string) != "sys_assets" {
111+
continue
112+
}
113+
114+
fmt.Println(item)
115+
assetData := item["data"].(map[string]interface{})
116+
117+
publishDetails, hasPublishDetails := assetData["publish_details"].(map[string]interface{})
118+
119+
if !hasPublishDetails {
120+
publishDetails = map[string]interface{}{
121+
"locale": "",
122+
}
123+
}
124+
125+
parentUid, hasParentUid := assetData["parent_uid"].(string)
126+
127+
if !hasParentUid {
128+
parentUid = ""
129+
}
130+
fmt.Println(assetData)
131+
err := assets.SetAsset(structs.Route{
132+
Uid: assetData["uid"].(string),
133+
Title: assetData["title"].(string),
134+
ContentType: assetData["content_type"].(string),
135+
Locale: publishDetails["locale"].(string),
136+
Url: assetData["url"].(string),
137+
Parent: parentUid,
138+
UpdatedAt: getUpdatedAt(assetData),
139+
Published: item["type"].(string) == "asset_published",
140+
})
141+
142+
if err != nil {
143+
return err
144+
}
145+
}
146+
147+
return nil
148+
}
149+
89150
func addAllRoutes(data map[string]interface{}, routes *map[string]structs.Route) error {
90151
err := addSyncRoutes(data, routes)
91152

@@ -124,7 +185,7 @@ func getSyncData(paginationToken string, reset bool, syncToken string) (map[stri
124185
data, err = cs_sdk.Request(path, "GET", nil, false)
125186
} else if err != nil || reset {
126187
logger.Info("Initialising a fresh sync")
127-
path := "stacks/sync?init=true&type=entry_published,entry_unpublished,entry_deleted"
188+
path := "stacks/sync?init=true&type=entry_published,entry_unpublished,entry_deleted,asset_published,asset_unpublished,asset_deleted"
128189
data, err = cs_sdk.Request(path, "GET", nil, false)
129190
} else {
130191
logger.Info("Syncing data using an existing sync token")
@@ -152,6 +213,11 @@ func addSyncRoutes(data map[string]interface{}, routes *map[string]structs.Route
152213
logger.Info("Fetching item data (%d/%d)", (idx + 1), itemCount)
153214

154215
item := item.(map[string]interface{})
216+
217+
if item["content_type_uid"].(string) == "sys_assets" {
218+
continue
219+
}
220+
155221
data := item["data"].(map[string]interface{})
156222

157223
publishDetails, hasPublishDetails := data["publish_details"].(map[string]interface{})

cmd/database/assets/set-asset.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package assets
2+
3+
import (
4+
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
5+
"github.com/Dobefu/csb/cmd/cs_sdk/utils"
6+
"github.com/Dobefu/csb/cmd/database/query"
7+
db_structs "github.com/Dobefu/csb/cmd/database/structs"
8+
)
9+
10+
func SetAsset(
11+
route structs.Route,
12+
) error {
13+
err := query.Upsert("assets", getAssetValues(route))
14+
15+
if err != nil {
16+
return err
17+
}
18+
19+
return nil
20+
}
21+
22+
func getAssetValues(route structs.Route) []db_structs.QueryValue {
23+
return []db_structs.QueryValue{
24+
{
25+
Name: "id",
26+
Value: utils.GenerateId(route),
27+
},
28+
{
29+
Name: "uid",
30+
Value: route.Uid,
31+
},
32+
{
33+
Name: "title",
34+
Value: route.Title,
35+
},
36+
{
37+
Name: "content_type",
38+
Value: route.ContentType,
39+
},
40+
{
41+
Name: "locale",
42+
Value: route.Locale,
43+
},
44+
{
45+
Name: "slug",
46+
Value: route.Slug,
47+
},
48+
{
49+
Name: "url",
50+
Value: route.Url,
51+
},
52+
{
53+
Name: "parent",
54+
Value: route.Parent,
55+
},
56+
{
57+
Name: "updated_at",
58+
Value: route.UpdatedAt,
59+
},
60+
{
61+
Name: "published",
62+
Value: route.Published,
63+
},
64+
}
65+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE IF EXISTS assets;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE IF NOT EXISTS assets(
2+
id varchar(48) NOT NULL PRIMARY KEY UNIQUE,
3+
uid varchar(32) NOT NULL,
4+
title varchar(255) NOT NULL,
5+
content_type varchar(255) NOT NULL,
6+
locale varchar(16) NOT NULL,
7+
slug varchar(255) NOT NULL,
8+
url varchar(255) NOT NULL,
9+
parent varchar(64),
10+
updated_at timestamp NOT NULL,
11+
published boolean NOT NULL DEFAULT false
12+
);

0 commit comments

Comments
 (0)