plug-sdk/pkg/plug/source.go
SoXX 76174e3247
All checks were successful
Gitea Build Check / Build (push) Successful in 51s
Gitea Build Check / Build (pull_request) Successful in 51s
feat(dependencies): update to latest version
updated OtterSpaceSDK to v4.0.0
2024-08-29 15:26:58 +02:00

47 lines
1.4 KiB
Go

package plug
import (
"context"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/codes"
)
func upsertSource(ctx context.Context, source models.Source) (models.Source, error) {
ctx, span := tracer.Start(ctx, "upsertSource")
var err error
localSource, err := database.GetSourceByDomain(ctx, source.Domain)
if err != nil {
if err.Error() == "Database error: NoDataFound" {
span.AddEvent("No Source found, initializing source")
log.WithContext(ctx).WithField("source_domain", source.Domain).Info("No Source found, initializing source!")
source, err = database.CreateSource(ctx, source)
if err != nil {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.WithError(err).WithField("source_domain", source.Domain).Error("Failed to create source")
return models.Source{}, err
}
span.AddEvent("Source created")
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).WithField("source_id", source.ID).Info("Source created!")
return source, nil
} else {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
log.WithContext(ctx).WithError(err).WithField("source_domain", source.Domain).Error("Failed to get source by domain")
return models.Source{}, err
}
}
return localSource, nil
}