Implement filtering feature to negate selected labels

This commit is contained in:
zebrapurring
2024-06-28 16:45:43 +02:00
parent e929c38e37
commit e79905b608
4 changed files with 27 additions and 3 deletions

View File

@@ -57,6 +57,7 @@ func (ctrl *V1Controller) HandleItemsGetAll() errchain.HandlerFunc {
Search: params.Get("q"),
LocationIDs: queryUUIDList(params, "locations"),
LabelIDs: queryUUIDList(params, "labels"),
NegateLabels: queryBool(params.Get("negateLabels")),
ParentItemIDs: queryUUIDList(params, "parentIds"),
IncludeArchived: queryBool(params.Get("includeArchived")),
Fields: filterFieldItems(params["fields"]),

View File

@@ -36,6 +36,7 @@ type (
AssetID AssetID `json:"assetId"`
LocationIDs []uuid.UUID `json:"locationIds"`
LabelIDs []uuid.UUID `json:"labelIds"`
NegateLabels bool `json:"negateLabels"`
ParentItemIDs []uuid.UUID `json:"parentIds"`
SortBy string `json:"sortBy"`
IncludeArchived bool `json:"includeArchived"`
@@ -365,10 +366,17 @@ func (e *ItemsRepository) QueryByGroup(ctx context.Context, gid uuid.UUID, q Ite
if len(q.LabelIDs) > 0 {
labelPredicates := make([]predicate.Item, 0, len(q.LabelIDs))
for _, l := range q.LabelIDs {
labelPredicates = append(labelPredicates, item.HasLabelWith(label.ID(l)))
if !q.NegateLabels {
labelPredicates = append(labelPredicates, item.HasLabelWith(label.ID(l)))
} else {
labelPredicates = append(labelPredicates, item.Not(item.HasLabelWith(label.ID(l))))
}
}
if !q.NegateLabels {
andPredicates = append(andPredicates, item.Or(labelPredicates...))
} else {
andPredicates = append(andPredicates, item.And(labelPredicates...))
}
andPredicates = append(andPredicates, item.Or(labelPredicates...))
}
if len(q.LocationIDs) > 0 {