53ea7d34e6
Start adding Textract + UUID changes * base * startclient * ts * short * tests
100 lines
1.9 KiB
Go
100 lines
1.9 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/validation"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (s *Service) UpdateByExternalId(ctx context.Context, id string, entity *Update) error {
|
|
client, err := s.cfg.GetDBQueries().GetClientByExternalId(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.Update(ctx, client.ID, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
type Update struct {
|
|
Name *string
|
|
CanSync *bool
|
|
}
|
|
|
|
func (s *Service) Update(ctx context.Context, id uuid.UUID, entity *Update) error {
|
|
current, err := s.Get(ctx, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.normalizeUpdateParams(id, current, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = s.submitUpdate(ctx, id, entity)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) submitUpdate(ctx context.Context, id uuid.UUID, entity *Update) error {
|
|
return s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
id := 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(id uuid.UUID, current *Client, entity *Update) error {
|
|
if entity == nil || current == nil || current.ID != 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
|
|
}
|