package database import ( "context" "errors" otterError "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/error" "git.anthrove.art/Anthrove/otter-space-sdk/v2/pkg/models" "gorm.io/gorm" ) func CreateSource(ctx context.Context, source models.Source) (models.Source, error) { if client == nil { return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } result := client.WithContext(ctx).Create(&source) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return models.Source{}, &otterError.Database{Reason: otterError.DuplicateKey} } return models.Source{}, result.Error } return source, nil } func CreateSourceInBatch(ctx context.Context, source []models.Source, batchSize int) error { if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if source == nil { return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} } if len(source) == 0 { return &otterError.EntityValidationFailed{Reason: otterError.SourceListIsEmpty} } if batchSize == 0 { return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty} } result := client.WithContext(ctx).CreateInBatches(&source, batchSize) if result.Error != nil { if errors.Is(result.Error, gorm.ErrDuplicatedKey) { return &otterError.Database{Reason: otterError.DuplicateKey} } return result.Error } return nil } func UpdateSource(ctx context.Context, source models.Source) error { if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(source.ID) == 0 { return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} } updateSource := models.Source{ DisplayName: source.DisplayName, Domain: source.Domain, Icon: source.Icon, } result := client.WithContext(ctx).Model(&updateSource).Update("deleted_at", gorm.DeletedAt{}) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return &otterError.Database{Reason: otterError.NoDataFound} } return result.Error } return nil } func GetSourceByID(ctx context.Context, id models.SourceID) (models.Source, error) { var source models.Source if client == nil { return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(id) == 0 { return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} } if len(id) != 25 { return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} } result := client.WithContext(ctx).First(&source, "id = ?", id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} } return models.Source{}, result.Error } return source, nil } func GetSourceByDomain(ctx context.Context, sourceDomain models.SourceDomain) (models.Source, error) { var source models.Source if client == nil { return models.Source{}, &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(sourceDomain) == 0 { return models.Source{}, &otterError.EntityValidationFailed{Reason: otterError.SourceDomainIsEmpty} } result := client.WithContext(ctx).First(&source, "domain = ?", sourceDomain) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return models.Source{}, &otterError.Database{Reason: otterError.NoDataFound} } return models.Source{}, result.Error } return source, nil } func DeleteSource(ctx context.Context, id models.SourceID) error { var source models.Source if client == nil { return &otterError.Database{Reason: otterError.DatabaseIsNotConnected} } if len(id) == 0 { return &otterError.EntityValidationFailed{Reason: otterError.SourceIDIsEmpty} } if len(id) != 25 { return &otterError.EntityValidationFailed{Reason: otterError.SourceIDToShort} } result := client.WithContext(ctx).Delete(&source, id) if result.Error != nil { if errors.Is(result.Error, gorm.ErrRecordNotFound) { return &otterError.Database{Reason: otterError.NoDataFound} } return result.Error } return nil }