058f8dba7f
Can Sync Versioning * started * client * jobcansync * tests
81 lines
1.1 KiB
Go
81 lines
1.1 KiB
Go
package client
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"queryorchestration/internal/database"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Client struct {
|
|
ID uuid.UUID
|
|
Name string
|
|
CanSync bool
|
|
}
|
|
|
|
func (c *Client) normalizeCanSyncUpdate(n **bool) {
|
|
if n == nil || *n == nil {
|
|
return
|
|
}
|
|
|
|
if c.CanSync == **n {
|
|
*n = nil
|
|
}
|
|
}
|
|
|
|
func (c *Client) normalizeNameUpdate(n **string) error {
|
|
if n == nil || *n == nil {
|
|
return nil
|
|
}
|
|
|
|
err := normalizeName(*n)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if c.Name == **n {
|
|
*n = nil
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func normalizeName(name *string) error {
|
|
if name == nil {
|
|
return nil
|
|
}
|
|
|
|
uname := strings.TrimSpace(*name)
|
|
|
|
if uname == "" {
|
|
return errors.New("name required")
|
|
}
|
|
|
|
spacere := regexp.MustCompile(`\s+`)
|
|
uname = spacere.ReplaceAllString(uname, " ")
|
|
|
|
reg := `^[a-zA-Z0-9\_\- ]+$`
|
|
|
|
alphanumeric := regexp.MustCompile(reg)
|
|
if !alphanumeric.MatchString(uname) {
|
|
return fmt.Errorf("name must have regex: %s", reg)
|
|
}
|
|
|
|
*name = uname
|
|
|
|
return nil
|
|
}
|
|
|
|
type Service struct {
|
|
db *database.Connection
|
|
}
|
|
|
|
func New(db *database.Connection) *Service {
|
|
return &Service{
|
|
db,
|
|
}
|
|
}
|