72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package document
|
|
|
|
import (
|
|
"context"
|
|
"gotemplate/internal/database"
|
|
"gotemplate/internal/database/repository"
|
|
"log"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type Service struct {
|
|
db *repository.Queries
|
|
}
|
|
|
|
type Document struct {
|
|
ID uuid.UUID `json:"id"`
|
|
JobID uuid.UUID `json:"jobId"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
func New(ctx context.Context, db *repository.Queries) *Service {
|
|
return &Service{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (s *Service) Sync(ctx context.Context, doc Document) error {
|
|
jobID, err := database.ToDBUUID(doc.JobID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
queries, err := s.db.GetQueriesFromJobID(ctx, jobID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
docID, err := database.ToDBUUID(doc.ID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// TODO grab only the results with the latest clean and text version
|
|
results, err := s.db.ListResultsByDocumentID(ctx, docID, minCleanVersion, minTextVersion)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
for _, query := range queries {
|
|
isSynced := false
|
|
for _, result := range results {
|
|
if result.Queryid != query.ID && result.Queryversion != query.Queryversion {
|
|
continue
|
|
}
|
|
if result.Cleanversion > query.Mincleanversion && result.Textversion > query.Mintextversion {
|
|
isSynced = true
|
|
}
|
|
break
|
|
}
|
|
if isSynced {
|
|
continue
|
|
}
|
|
log.Print("TODO sync me pls")
|
|
// Check if exists and synced
|
|
// IF NOT synced then add to queue, as well as all dependents recursively
|
|
}
|
|
// TODO what to do with results that are not used
|
|
|
|
return nil
|
|
}
|