720a84be92
integration of background processor * integration part 1 * feature working * fix mimetype issue
118 lines
2.5 KiB
Go
118 lines
2.5 KiB
Go
package documentinit
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"errors"
|
|
"log/slog"
|
|
|
|
docsyncrunner "queryorchestration/api/docSyncRunner"
|
|
"queryorchestration/internal/database/repository"
|
|
"queryorchestration/internal/serviceconfig/objectstore"
|
|
"queryorchestration/internal/serviceconfig/queue"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Create struct {
|
|
Bucket string
|
|
Key objectstore.BucketKey
|
|
Hash string
|
|
}
|
|
|
|
func (s *Service) Create(ctx context.Context, doc *Create) (uuid.UUID, error) {
|
|
params, err := s.getCreateParams(ctx, doc)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
id, err := s.submitCreate(ctx, params)
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
if params.ID == nil {
|
|
err := s.cfg.SendToQueue(ctx, &queue.SendParams{
|
|
QueueURL: s.cfg.GetDocumentSyncURL(),
|
|
Body: docsyncrunner.Body{
|
|
DocumentID: id,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
}
|
|
|
|
return id, nil
|
|
}
|
|
|
|
type createDocumentParams struct {
|
|
ID *uuid.UUID
|
|
Hash string
|
|
Bucket string
|
|
Key objectstore.BucketKey
|
|
}
|
|
|
|
func (s *Service) getCreateParams(ctx context.Context, doc *Create) (*createDocumentParams, error) {
|
|
idbyhash, err := s.cfg.GetDBQueries().GetDocumentIDByHash(ctx, &repository.GetDocumentIDByHashParams{
|
|
Clientid: doc.Key.ClientID,
|
|
Hash: doc.Hash,
|
|
})
|
|
var docID *uuid.UUID
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
|
return nil, err
|
|
} else if err == nil {
|
|
docID = &idbyhash
|
|
}
|
|
|
|
return &createDocumentParams{
|
|
ID: docID,
|
|
Hash: doc.Hash,
|
|
Key: doc.Key,
|
|
Bucket: doc.Bucket,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) submitCreate(ctx context.Context, params *createDocumentParams) (uuid.UUID, error) {
|
|
var id uuid.UUID
|
|
|
|
err := s.cfg.ExecuteDBTransaction(ctx, func(ctx context.Context, q *repository.Queries) error {
|
|
var dbid uuid.UUID
|
|
if params.ID == nil {
|
|
createid, err := s.cfg.GetDBQueries().CreateDocument(ctx, &repository.CreateDocumentParams{
|
|
Clientid: params.Key.ClientID,
|
|
Hash: params.Hash,
|
|
BatchID: params.Key.BatchID,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
slog.Debug("document created", "id", createid.String(), "client", params.Key.ClientID)
|
|
|
|
dbid = createid
|
|
} else {
|
|
slog.Debug("document exists", "id", params.ID.String(), "client", params.Key.ClientID)
|
|
|
|
dbid = *params.ID
|
|
}
|
|
|
|
err := s.cfg.GetDBQueries().AddDocumentEntry(ctx, &repository.AddDocumentEntryParams{
|
|
Documentid: dbid,
|
|
Bucket: params.Bucket,
|
|
Key: params.Key.String(),
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
id = dbid
|
|
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return uuid.Nil, err
|
|
}
|
|
|
|
return id, nil
|
|
}
|