15adaebfcd
DRAFT PR : WIP working through ideas for integration * movearound * attempttwo * openapi * further sanding * fix * start on tests * runthroughsingleconfig * somechanges * reflectissue * removeerrs * mostlyremovepanic * removeenv * noncfgtests * go * repo * fix service config test * add PWD to all * test fix * fix lint * todo for later * passingunittests * alltests * testlogger * testloggername * clean
83 lines
1.6 KiB
Go
83 lines
1.6 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
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
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 entity.Name == nil && entity.CanSync == nil {
|
|
return errors.New("no updates presented")
|
|
}
|
|
|
|
return nil
|
|
}
|