Files
upstream-traggo/tag/create.go
upstream-mirror cda7c1a474
Some checks failed
build / build (push) Failing after 3s
snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
2026-09-16 05:24:45 +05:00

39 lines
1013 B
Go

package tag
import (
"context"
"fmt"
"strings"
"github.com/jinzhu/copier"
"github.com/traggo/server/auth"
"github.com/traggo/server/generated/gqlmodel"
"github.com/traggo/server/model"
)
// CreateTag creates a tag.
func (r *ResolverForTag) CreateTag(ctx context.Context, key string, color string) (*gqlmodel.TagDefinition, error) {
if strings.TrimSpace(key) == "" {
return nil, fmt.Errorf("tag must not be empty")
}
if strings.Contains(key, " ") {
return nil, fmt.Errorf("tag must not contain spaces")
}
userID := auth.GetUser(ctx).ID
definition := &model.TagDefinition{
Key: strings.ToLower(key),
Color: color,
UserID: userID,
}
if !r.DB.Where("user_id = ?", userID).Where("key = ?", strings.ToLower(key)).Find(new(model.TagDefinition)).RecordNotFound() {
return nil, fmt.Errorf("tag with key '%s' does already exist", definition.Key)
}
create := r.DB.Create(&definition)
gqlTag := &gqlmodel.TagDefinition{}
copier.Copy(gqlTag, definition)
return gqlTag, create.Error
}