You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.2 KiB
54 lines
1.2 KiB
package loaders
|
|
|
|
import (
|
|
"context"
|
|
"git.aiterp.net/stufflog/server/database/repositories"
|
|
"git.aiterp.net/stufflog/server/internal/slerrors"
|
|
"git.aiterp.net/stufflog/server/models"
|
|
"time"
|
|
)
|
|
|
|
// go run github.com/vektah/dataloaden IssueLoader string \*git.aiterp.net/stufflog/server/models.Issue
|
|
|
|
var issueLoaderCtxKey = "ctx.stufflog.IssueLoader"
|
|
|
|
func IssueLoaderFromContext(ctx context.Context) *IssueLoader {
|
|
return ctx.Value(issueLoaderCtxKey).(*IssueLoader)
|
|
}
|
|
|
|
func NewIssueLoader(ctx context.Context, issueRepo repositories.IssueRepository) *IssueLoader {
|
|
return &IssueLoader{
|
|
fetch: func(keys []string) ([]*models.Issue, []error) {
|
|
results := make([]*models.Issue, len(keys))
|
|
errors := make([]error, len(keys))
|
|
|
|
issues, err := issueRepo.List(ctx, models.IssueFilter{IssueIDs: keys})
|
|
if err != nil {
|
|
for i := range errors {
|
|
errors[i] = err
|
|
}
|
|
|
|
return results, errors
|
|
}
|
|
|
|
for i, key := range keys {
|
|
found := false
|
|
for _, issue := range issues {
|
|
if issue.ID == key {
|
|
found = true
|
|
results[i] = issue
|
|
break
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
errors[i] = slerrors.NotFound("Issue")
|
|
}
|
|
}
|
|
|
|
return results, errors
|
|
},
|
|
wait: time.Millisecond / 2,
|
|
maxBatch: 16,
|
|
}
|
|
}
|