Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sync assets #11

Merged
merged 7 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);
Loading