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
2024-11-30 18:54:43 -03:00

72 lines
1.8 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, values)
if err != nil {
return err
}
relationships := make([]*Relationship, 0, len(values))
for _, item := range values {
relation := &Relationship{
_class: "PLATFORM_HAS_COLLECTION",
From: platform.Id,
To: item.Id,
}
err := platform.AddRelationship(relation)
if err != nil {
return err
}
relationships = append(relationships, relation)
}
err = BulkCreateRelationships(platform._conn, relationships)
if err != nil {
return err
}
return nil
}