This repository has been archived on 2025-05-15. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
synchronizator-go/pkg/platform.go

68 lines
1.7 KiB
Go

package synchronizator
import (
"context"
"fmt"
"slices"
)
// Platform represents a collection of nodes. It embeds a Node, so all the
// node's functionality is available.
type Platform struct {
Node // Underlying node info
Collections []*Collection // Child nodes
}
// Is a type alias for FetchResponse containing a slice of Collection pointers.
type FetchCollectionResponse = FetchResponse[[]*Collection]
// Fetches collections using the provided fetcher and pagination settings.
// It updates the platform's collections and creates relationships between the platform and the collections.
//
// Parameters:
// - ctx: The context to control cancellation.
// - fetcher: The fetcher function to execute the work.
// - start_pagination: The initial pagination settings.
// - pool_config: The configuration for the worker pool.
//
// Returns:
// - error: The error if any occurred.
func (platform *Platform) FetchCollections(
ctx context.Context,
fetcher Work[Pagination, FetchCollectionResponse],
startPagination Pagination,
poolConfig *WorkConfig,
) error {
values, err := fetchWithPagination(ctx, poolConfig, fetcher, startPagination)
if err != nil {
return err
}
platform.Collections = slices.Concat(platform.Collections, values)
fmt.Printf("Collections: %v\n", len(platform.Collections))
err = BulkCreateNode(platform._conn, platform.Collections)
if err != nil {
return err
}
for _, item := range platform.Collections {
err := platform.AddRelationship(
&Relationship{
_class: "PLATFORM_HAS_COLLECTION",
From: platform.Id,
To: item.Id,
})
if err != nil {
return err
}
}
err = BulkCreateRelationships(platform._conn, platform._relationships)
if err != nil {
return err
}
return nil
}