2024-06-03 20:14:50 +00:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-06-25 19:55:43 +00:00
|
|
|
"errors"
|
2024-06-26 07:10:04 +00:00
|
|
|
|
2024-06-26 08:02:54 +00:00
|
|
|
otterError "git.dragse.it/anthrove/otter-space-sdk/pkg/error"
|
2024-06-22 20:06:36 +00:00
|
|
|
"git.dragse.it/anthrove/otter-space-sdk/pkg/models"
|
2024-06-04 13:16:37 +00:00
|
|
|
|
2024-06-03 20:14:50 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
// CreateSource creates a pgModels.Source
|
|
|
|
func CreateSource(ctx context.Context, db *gorm.DB, anthroveSource *models.Source) error {
|
2024-06-04 13:16:37 +00:00
|
|
|
|
2024-06-19 22:13:39 +00:00
|
|
|
if anthroveSource.Domain == "" {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityValidationFailed{Reason: "Domain is required"}
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
|
|
|
|
2024-06-22 20:06:36 +00:00
|
|
|
result := db.WithContext(ctx).Where(models.Source{Domain: anthroveSource.Domain}).FirstOrCreate(anthroveSource)
|
2024-06-04 13:16:37 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.EntityAlreadyExists{}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
2024-06-04 13:16:37 +00:00
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
|
2024-06-25 19:55:43 +00:00
|
|
|
if result.RowsAffected == 0 {
|
2024-06-26 08:02:54 +00:00
|
|
|
return &otterError.NoDataWritten{}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-04 13:16:37 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-06-23 19:23:38 +00:00
|
|
|
// GetAllSource returns a list of all pgModels.Source
|
|
|
|
func GetAllSource(ctx context.Context, db *gorm.DB) ([]models.Source, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
var sources []models.Source
|
2024-06-03 20:14:50 +00:00
|
|
|
|
|
|
|
result := db.WithContext(ctx).Find(&sources)
|
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-06-26 08:02:54 +00:00
|
|
|
return nil, &otterError.NoDataFound{}
|
2024-06-26 07:10:04 +00:00
|
|
|
}
|
2024-06-03 20:14:50 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_amount": result.RowsAffected,
|
|
|
|
}).Trace("database: get all source nodes")
|
|
|
|
|
|
|
|
return sources, nil
|
|
|
|
}
|
2024-06-04 13:16:37 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
// GetSourceByDomain returns the first source it finds based on the domain
|
|
|
|
func GetSourceByDomain(ctx context.Context, db *gorm.DB, sourceDomain models.AnthroveSourceDomain) (*models.Source, error) {
|
2024-06-22 20:06:36 +00:00
|
|
|
var sources models.Source
|
2024-06-04 13:16:37 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
if sourceDomain == "" {
|
2024-06-26 08:02:54 +00:00
|
|
|
return nil, &otterError.EntityValidationFailed{Reason: "AnthroveSourceDomain is not set"}
|
2024-06-19 22:13:39 +00:00
|
|
|
}
|
2024-06-04 13:16:37 +00:00
|
|
|
|
2024-06-23 20:35:46 +00:00
|
|
|
result := db.WithContext(ctx).Where("domain = ?", sourceDomain).First(&sources)
|
2024-06-04 13:16:37 +00:00
|
|
|
|
|
|
|
if result.Error != nil {
|
2024-06-25 19:55:43 +00:00
|
|
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
2024-06-26 08:02:54 +00:00
|
|
|
return nil, &otterError.NoDataFound{}
|
2024-06-25 19:55:43 +00:00
|
|
|
}
|
2024-06-04 13:16:37 +00:00
|
|
|
return nil, result.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"tag_amount": result.RowsAffected,
|
|
|
|
}).Trace("database: get all source nodes")
|
|
|
|
|
2024-06-19 22:13:39 +00:00
|
|
|
return &sources, nil
|
2024-06-04 13:16:37 +00:00
|
|
|
}
|