snapshot of https://github.com/traggo/server.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
build / build (push) Failing after 3s
Some checks failed
build / build (push) Failing after 3s
This commit is contained in:
38
tag/create.go
Normal file
38
tag/create.go
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user