Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

feat: backend changes for tags

tonyaellie6b35c8cd3135
diff --git a/backend/internal/data/repo/repo_items.go b/backend/internal/data/repo/repo_items.go
index 439ffa81..ace99f0a 100644
--- a/backend/internal/data/repo/repo_items.go
+++ b/backend/internal/data/repo/repo_items.go
@@ -405,14 +405,26 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
var andPredicates []predicate.Item
{
if len(q.TagIDs) > 0 {
+ // Get descendant tags for both positive and negative filtering
+ tagRepo := &TagRepository{e.db, e.bus}
+ descendants, err := tagRepo.GetDescendantTagIDs(ctx, q.TagIDs)
+ if err != nil {
+ log.Warn().Err(err).Msg("failed to get descendant tags, using only direct tags")
+ descendants = q.TagIDs
+ } else if len(descendants) == 0 {
+ descendants = q.TagIDs
+ }
+
var tagPredicates []predicate.Item
if !q.NegateTags {
- tagPredicates = lo.Map(q.TagIDs, func(l uuid.UUID, _ int) predicate.Item {
+ // Include items with any of the selected tags or their descendants
+ tagPredicates = lo.Map(descendants, func(l uuid.UUID, _ int) predicate.Item {
return item.HasTagWith(tag.ID(l))
})
andPredicates = append(andPredicates, item.Or(tagPredicates...))
} else {
- tagPredicates = lo.Map(q.TagIDs, func(l uuid.UUID, _ int) predicate.Item {
+ // Exclude items with any of the selected tags or their descendants
+ tagPredicates = lo.Map(descendants, func(l uuid.UUID, _ int) predicate.Item {
return item.Not(item.HasTagWith(tag.ID(l)))
})
andPredicates = append(andPredicates, item.And(tagPredicates...))
diff --git a/backend/internal/data/repo/repo_tags.go b/backend/internal/data/repo/repo_tags.go
index 75b3683d..718a1bfe 100644
--- a/backend/internal/data/repo/repo_tags.go
+++ b/backend/internal/data/repo/repo_tags.go
@@ -160,6 +160,55 @@ func (r *TagRepository) GetAll(ctx context.Context, groupID uuid.UUID) ([]TagSum
)
}
+// GetDescendantTagIDs retrieves all descendant tag IDs for the given parent tag IDs.
+// Returns all tags that are direct or indirect children of any of the provided tag IDs.
+// Uses recursive in-memory traversal since Ent doesn't support recursive CTEs directly.
+func (r *TagRepository) GetDescendantTagIDs(ctx context.Context, tagIDs []uuid.UUID) ([]uuid.UUID, error) {
+ if len(tagIDs) == 0 {
+ return []uuid.UUID{}, nil
+ }
+
+ // Start with the provided tag IDs
+ result := make(map[uuid.UUID]bool)
+ for _, id := range tagIDs {
+ result[id] = true
+ }
+
+ // Queue of parent IDs to process
+ queue := make([]uuid.UUID, len(tagIDs))
+ copy(queue, tagIDs)
+
+ for len(queue) > 0 {
+ // Get next parent ID
+ parentID := queue[0]
+ queue = queue[1:]
+
+ // Get all direct children of this parent
+ children, err := r.db.Tag.Query().
+ Where(tag.HasParentWith(tag.ID(parentID))).
+ All(ctx)
+ if err != nil {
+ return nil, err
+ }
+
+ // Add children to result and queue
+ for _, child := range children {
+ if !result[child.ID] {
+ result[child.ID] = true
+ queue = append(queue, child.ID)
+ }
+ }
+ }
+
+ // Convert map to slice
+ descendantIDs := make([]uuid.UUID, 0, len(result))
+ for id := range result {
+ descendantIDs = append(descendantIDs, id)
+ }
+
+ return descendantIDs, nil
+}
+
// getSubtreeDepth calculates the maximum depth of the subtree rooted at the given tag ID.
// Uses a recursive CTE to traverse the entire subtree and find the deepest level.
// Returns 1 for a tag with no children, and increases by 1 for each level.