04d8eaf52c
Client Entity * repolevel * servicefunctions * openapiclientget * openapiupdate * client * vendor
55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Update struct {
|
|
ID uuid.UUID
|
|
Name *string
|
|
CanSync *bool
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, entity *Update) error {
|
|
current, err := s.Get(ctx, entity.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
params, err := s.getUpdateParams(current, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.db.Queries.UpdateClient(ctx, params)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) getUpdateParams(current *Client, entity *Update) (*repository.UpdateClientParams, error) {
|
|
if entity == nil || current == nil || current.ID != entity.ID {
|
|
return nil, errors.New("no updates presented")
|
|
}
|
|
|
|
name, err := current.getNameUpdate(entity.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
canSync := current.getCanSyncUpdate(entity.CanSync)
|
|
|
|
return &repository.UpdateClientParams{
|
|
ID: database.MustToDBUUID(current.ID),
|
|
Name: name,
|
|
Cansync: canSync,
|
|
}, nil
|
|
}
|