generated from alecodes/base-template
96 lines
2.4 KiB
Go
96 lines
2.4 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
|
|
}
|
|
|
|
func (platform *Platform) AddCollection(
|
|
name string,
|
|
metadata, originalData []byte,
|
|
) (*Collection, error) {
|
|
collection, err := platform._conn.NewCollection(name, metadata, originalData)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
platform.Collections = append(platform.Collections, collection)
|
|
|
|
return collection, nil
|
|
}
|
|
|
|
func (platform *Platform) GetDefaultCollection() (*Collection, error) {
|
|
for _, collection := range platform.Collections {
|
|
if collection.IsDefault() {
|
|
return collection, nil
|
|
}
|
|
}
|
|
|
|
return nil, fmt.Errorf("Default collection not found")
|
|
}
|
|
|
|
// 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
|
|
}
|