Skip to content

Commit

Permalink
Merge pull request #11 from Dobefu/feature/sync-assets
Browse files Browse the repository at this point in the history
Feature/sync assets
  • Loading branch information
Dobefu authored Jan 13, 2025
2 parents 7d0816f + 85837cc commit 33fee06
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 1 deletion.
68 changes: 67 additions & 1 deletion cmd/cs_sdk/functions/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/Dobefu/csb/cmd/cs_sdk"
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
"github.com/Dobefu/csb/cmd/cs_sdk/utils"
"github.com/Dobefu/csb/cmd/database/assets"
"github.com/Dobefu/csb/cmd/database/query"
db_routes "github.com/Dobefu/csb/cmd/database/routes"
"github.com/Dobefu/csb/cmd/database/state"
Expand Down Expand Up @@ -51,6 +52,12 @@ func Sync(reset bool) error {
return err
}

err = addAllAssets(data)

if err != nil {
return err
}

var hasPaginationToken bool

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

func addAllAssets(data map[string]interface{}) error {
items, hasItems := data["items"].([]interface{})

if !hasItems {
return errors.New("sync data has no items")
}

itemCount := len(items)

for idx, item := range items {
logger.Info("Fetching item data (%d/%d)", (idx + 1), itemCount)

item := item.(map[string]interface{})

if item["content_type_uid"].(string) != "sys_assets" {
continue
}

fmt.Println(item)
assetData := item["data"].(map[string]interface{})

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

if !hasPublishDetails {
publishDetails = map[string]interface{}{
"locale": "",
}
}

parentUid, hasParentUid := assetData["parent_uid"].(string)

if !hasParentUid {
parentUid = ""
}
fmt.Println(assetData)
err := assets.SetAsset(structs.Route{
Uid: assetData["uid"].(string),
Title: assetData["title"].(string),
ContentType: assetData["content_type"].(string),
Locale: publishDetails["locale"].(string),
Url: assetData["url"].(string),
Parent: parentUid,
UpdatedAt: getUpdatedAt(assetData),
Published: item["type"].(string) == "asset_published",
})

if err != nil {
return err
}
}

return nil
}

func addAllRoutes(data map[string]interface{}, routes *map[string]structs.Route) error {
err := addSyncRoutes(data, routes)

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

item := item.(map[string]interface{})

if item["content_type_uid"].(string) == "sys_assets" {
continue
}

data := item["data"].(map[string]interface{})

publishDetails, hasPublishDetails := data["publish_details"].(map[string]interface{})
Expand Down
65 changes: 65 additions & 0 deletions cmd/database/assets/set-asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package assets

import (
"github.com/Dobefu/csb/cmd/cs_sdk/structs"
"github.com/Dobefu/csb/cmd/cs_sdk/utils"
"github.com/Dobefu/csb/cmd/database/query"
db_structs "github.com/Dobefu/csb/cmd/database/structs"
)

func SetAsset(
route structs.Route,
) error {
err := query.Upsert("assets", getAssetValues(route))

if err != nil {
return err
}

return nil
}

func getAssetValues(route structs.Route) []db_structs.QueryValue {
return []db_structs.QueryValue{
{
Name: "id",
Value: utils.GenerateId(route),
},
{
Name: "uid",
Value: route.Uid,
},
{
Name: "title",
Value: route.Title,
},
{
Name: "content_type",
Value: route.ContentType,
},
{
Name: "locale",
Value: route.Locale,
},
{
Name: "slug",
Value: route.Slug,
},
{
Name: "url",
Value: route.Url,
},
{
Name: "parent",
Value: route.Parent,
},
{
Name: "updated_at",
Value: route.UpdatedAt,
},
{
Name: "published",
Value: route.Published,
},
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS assets;
12 changes: 12 additions & 0 deletions cmd/migrate_db/migrations/000004_create_assets_table.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS assets(
id varchar(48) NOT NULL PRIMARY KEY UNIQUE,
uid varchar(32) NOT NULL,
title varchar(255) NOT NULL,
content_type varchar(255) NOT NULL,
locale varchar(16) NOT NULL,
slug varchar(255) NOT NULL,
url varchar(255) NOT NULL,
parent varchar(64),
updated_at timestamp NOT NULL,
published boolean NOT NULL DEFAULT false
);

0 comments on commit 33fee06

Please sign in to comment.