This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
otter-space-sdk/internal/postgres/source.go

71 lines
1.7 KiB
Go

package postgres
import (
"context"
"fmt"
"git.dragse.it/anthrove/otter-space-sdk/pkg/models/pgModels"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
)
// CreateSourceNode creates a pgModels.Source
func CreateSourceNode(ctx context.Context, db *gorm.DB, anthroveSource *pgModels.Source) error {
if anthroveSource.Domain == "" {
return fmt.Errorf("anthroveSource domain is required")
}
result := db.WithContext(ctx).Create(anthroveSource)
if result.Error != nil {
return result.Error
}
log.WithFields(log.Fields{
"node_source_url": anthroveSource.Domain,
"node_source_displayName": anthroveSource.DisplayName,
"node_source_icon": anthroveSource.Icon,
}).Trace("database: created source node")
return nil
}
// GetAllSourceNodes returns a list of all pgModels.Source
func GetAllSourceNodes(ctx context.Context, db *gorm.DB) ([]pgModels.Source, error) {
var sources []pgModels.Source
result := db.WithContext(ctx).Find(&sources)
if result.Error != nil {
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": result.RowsAffected,
}).Trace("database: get all source nodes")
return sources, nil
}
// GetSourceNodesByURL returns the first source it finds based on the domain
func GetSourceNodesByURL(ctx context.Context, db *gorm.DB, domain string) (*pgModels.Source, error) {
var sources pgModels.Source
if domain == "" {
return nil, fmt.Errorf("domain is required")
}
result := db.WithContext(ctx).Where("domain = ?", domain).First(&sources)
if result.Error != nil {
return nil, result.Error
}
log.WithFields(log.Fields{
"tag_amount": result.RowsAffected,
}).Trace("database: get all source nodes")
return &sources, nil
}