53 lines
1.6 KiB
Go
53 lines
1.6 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// A registered title is public presentation metadata iff its Page has a live
|
|
// publication grant. Other registration fields are deliberately not selected.
|
|
func (s *Store) PublicPeriodicalTitles(ctx context.Context, domain, kind string, pages []PublishedNavigationPage) (result map[string]string, resultErr error) {
|
|
defer func() { resultErr = s.sqlError("periodicals repository", resultErr) }()
|
|
if len(pages) > MaxNavigationEntries {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
|
defer cancel()
|
|
titles := map[string]string{}
|
|
paths := map[string][]string{}
|
|
args := []any{domain, kind}
|
|
for _, p := range pages {
|
|
if _, exists := paths[p.Revision.ObjectUUID]; !exists {
|
|
args = append(args, p.Revision.ObjectUUID)
|
|
}
|
|
paths[p.Revision.ObjectUUID] = append(paths[p.Revision.ObjectUUID], p.Path)
|
|
}
|
|
if len(paths) == 0 {
|
|
return titles, nil
|
|
}
|
|
params := make([]string, len(paths))
|
|
for i := range params {
|
|
params[i] = s.sqlParam(i + 3)
|
|
}
|
|
rows, err := s.DB.QueryContext(ctx, `SELECT page_uuid,title FROM periodical_entries WHERE domain_uuid=`+s.sqlParam(1)+` AND kind=`+s.sqlParam(2)+` AND page_uuid IN (`+strings.Join(params, ",")+`)`, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
for rows.Next() {
|
|
var object, title string
|
|
if err := rows.Scan(&object, &title); err != nil {
|
|
return nil, err
|
|
}
|
|
for _, path := range paths[object] {
|
|
titles[path] = title
|
|
}
|
|
if len(titles) > MaxNavigationEntries {
|
|
return nil, ErrNavigationBudget
|
|
}
|
|
}
|
|
return titles, rows.Err()
|
|
}
|