refactor(hash-conflicts): one usage-counts reader for the API and SSR surfaces
The pending-hash-conflict walk existed twice (JSON admin API emitting numeric counts, SSR page emitting a formatted summary) — deliberately different shapes, but both duplicated the counts-or-zero lookup and the five-field total. MediaItemUsageCountsTotal is the shared read; the per-surface shaping stays local on purpose.
This commit is contained in:
1 parent
010c0cb287
commit
2ba1e0960b
2 files changed
+17
-9
No files matched your search
@@ -2,6 +2,7 @@ package handlers
|
||||
|
||||
import (
|
||||
"bookhoard/internal/database"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -48,6 +49,20 @@ type HashConflictResponse struct {
|
||||
Items []HashConflictItem `json:"items"`
|
||||
}
|
||||
|
||||
// MediaItemUsageCountsTotal reads an item's usage tally for the
|
||||
// hash-conflict surfaces, treating a lookup failure as "no usage"
|
||||
// (these surfaces only use it to warn about data loss), and returns
|
||||
// the counts with their precomputed total.
|
||||
func MediaItemUsageCountsTotal(ctx context.Context, db *database.Queries, id pgtype.UUID) (database.GetMediaItemUsageCountsRow, int) {
|
||||
counts, err := db.GetMediaItemUsageCounts(ctx, id)
|
||||
if err != nil {
|
||||
counts = database.GetMediaItemUsageCountsRow{}
|
||||
}
|
||||
total := counts.ProgressCount + counts.HighlightsCount + counts.BookmarksCount +
|
||||
counts.NotesCount + counts.CollectionsCount
|
||||
return counts, int(total)
|
||||
}
|
||||
|
||||
// ListHashConflicts returns all pending hash conflict groups with their member
|
||||
// items and usage counts.
|
||||
// GET /api/admin/hash-conflicts
|
||||
@@ -80,10 +95,7 @@ func (h *HashConflictsHandler) ListHashConflicts(c *echo.Context) error {
|
||||
}
|
||||
|
||||
for _, mi := range items {
|
||||
counts, err := h.db.GetMediaItemUsageCounts(ctx, mi.ID)
|
||||
if err != nil {
|
||||
counts = database.GetMediaItemUsageCountsRow{}
|
||||
}
|
||||
counts, _ := MediaItemUsageCountsTotal(ctx, h.db, mi.ID)
|
||||
resp.Items = append(resp.Items, HashConflictItem{
|
||||
ID: uuid.UUID(mi.ID.Bytes),
|
||||
Title: mi.Title,
|
||||
|
||||
Reference in new issue
Block a user