71f9802e1a
Split Query Running + Debugging Full Flow * completedquerysyncrunner * spliitinglogic * synccomplete * informdependents * only push same collector * deps * livetesting * foundissue * some issues resolved * activeupdate * collectorupdatefixes * fix dbquesries * tests * tests * pollingdebug
87 lines
1.7 KiB
Go
87 lines
1.7 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"queryorchestration/internal/database"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/validation"
|
|
|
|
"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
|
|
}
|
|
|
|
err = s.normalizeUpdateParams(current, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.submitUpdate(ctx, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) submitUpdate(ctx context.Context, entity *Update) error {
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
id := database.MustToDBUUID(entity.ID)
|
|
|
|
if entity.Name != nil {
|
|
err := q.UpdateClient(ctx, &repository.UpdateClientParams{
|
|
ID: id,
|
|
Name: *entity.Name,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if entity.CanSync != nil {
|
|
err := q.AddClientCanSync(ctx, &repository.AddClientCanSyncParams{
|
|
Clientid: id,
|
|
Cansync: *entity.CanSync,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
slog.Debug("client updated", "update", *entity)
|
|
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (s *Service) normalizeUpdateParams(current *Client, entity *Update) error {
|
|
if entity == nil || current == nil || current.ID != entity.ID {
|
|
return errors.New("no updates presented")
|
|
}
|
|
|
|
err := current.normalizeNameUpdate(&entity.Name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
current.normalizeCanSyncUpdate(&entity.CanSync)
|
|
|
|
if validation.AreAllPointersNilExcept(entity) {
|
|
return errors.New("no updates presented")
|
|
}
|
|
|
|
return nil
|
|
}
|