6648cdf1cb
Client External ID * normalizeexternalid * cleanopenapi * cleanopenapi * changingpublic * noprecommit * testing * precommit * processtest * nodb * tests * tests
65 lines
1.1 KiB
Go
65 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type CreateParams struct {
|
|
Name string
|
|
ExternalId string
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, params CreateParams) (uuid.UUID, error) {
|
|
err := s.normalizeCreate(¶ms)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
id, err := s.cfg.GetDBQueries().CreateClient(ctx, &repository.CreateClientParams{
|
|
Name: params.Name,
|
|
Externalid: params.ExternalId,
|
|
})
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
return database.MustToUUID(id), nil
|
|
}
|
|
|
|
func (s *Service) normalizeCreate(params *CreateParams) error {
|
|
err := normalizeName(¶ms.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = normalizeExternalID(¶ms.ExternalId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func normalizeExternalID(id *string) error {
|
|
if id == nil {
|
|
return nil
|
|
}
|
|
|
|
trim := strings.TrimSpace(*id)
|
|
pattern := regexp.MustCompile(`^[a-zA-Z0-9\_]+$`)
|
|
if !pattern.MatchString(trim) {
|
|
return errors.New("invalid external id")
|
|
}
|
|
|
|
*id = trim
|
|
return nil
|
|
}
|