otter-space-sdk/pkg/database/tagGroup.go

81 lines
2.0 KiB
Go

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 CreateTagGroup(ctx context.Context, tagGroupName models.TagGroupName, tagName models.TagName) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
tag := models.TagGroup{
Name: tagGroupName,
TagID: tagName,
}
result := client.WithContext(ctx).Create(&tag)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func CreateTagGroupInBatch(ctx context.Context, tagsGroups []models.TagGroup, batchSize int) error {
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if tagsGroups == nil {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
}
if len(tagsGroups) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupListIsEmpty}
}
if batchSize == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.BatchSizeIsEmpty}
}
result := client.WithContext(ctx).CreateInBatches(&tagsGroups, batchSize)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return &otterError.Database{Reason: otterError.DuplicateKey}
}
return result.Error
}
return nil
}
func DeleteTagGroup(ctx context.Context, tagGroupName models.TagGroupName) error {
var tagGroup models.TagGroup
if client == nil {
return &otterError.Database{Reason: otterError.DatabaseIsNotConnected}
}
if len(tagGroupName) == 0 {
return &otterError.EntityValidationFailed{Reason: otterError.TagGroupNameIsEmpty}
}
result := client.WithContext(ctx).Delete(&tagGroup, tagGroupName)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return &otterError.Database{Reason: otterError.NoDataFound}
}
return result.Error
}
return nil
}